Save calling funciton's address to register

Im a new user to the ARM7 and its asm instruction set. The current division libraries return zero if a divide by zero is detected. Im trying to modify one of these division libraries to record the called function’s address to a register to return back to another C function to detect where this /0 occurred

The original library asm looks something like this

=========

U_DIV:

STMFD sp!, {dvs, lr}

MOVS dvs, r1

BEQ div_by_zero

div_by_zero:

MOV r0, #0 ;

LDMFD sp!, {dvs, pc}

==========

What I want to do is get the stack pointer - 4 saved into A1 which should be the value of the function the divide by zero occurred in. When the branch to my C function occurs, A1 then gets passed to it.

Ive tried quite a few different things with my limited knowledge of this instruction set, but nothing seems to work.

=========

U_DIV:

<save stack address - 4 to A2>

STMFD sp!, {dvs, lr}

MOVS dvs, r1

BEQ div_by_zero

div_by_zero:

MOV A1, A2

BL _DIVZERO

MOV r0, #0 ;

LDMFD sp!, {dvs, pc}

==========

This seems like it should be something pretty easy to do, but I cant figure it out. Any suggestions?

On the ARM, the return address is placed in the lr register. It’s not pushed on the stack unless the called function explicitly does so itself, and that’s normally only done if the called function expects to call other functions (which would overwrite lr).

The following code, inserted after div_by_zero:, should do the job:

MOV r1, lr

BL _DIVZERO

Luken8r:
Im a new user to the ARM7 and its asm instruction set. The current division libraries return zero if a divide by zero is detected. Im trying to modify one of these division libraries to record the called function’s address to a register to return back to another C function to detect where this /0 occurred

Software interrupts are a useful mechanism for handling runtime errors like this. Investigate the SWI instruction.