Salta al contenuto
Note per Studenti Stack and Function Calls in ARM Report

Stack and Function Calls in ARM

In questa pagina 7
In questa pagina 4

Call and return

Definition (BL and BX LR)

  • BL function (branch with link): saves in LR (r14) the address of the next instruction and jumps.
  • BX LR (or MOV pc, lr): returns to the caller.

Example. If BL f is at address 0x1000, LR receives 0x1004 (the next instruction) and BX lr in f returns control there.

Problem: if function in turn calls another function, BL overwrites LR. It must be saved somewhere: on the stack.

Calling convention (AAPCS)

Caller and callee must agree on where arguments and result are and on which registers may be modified.

Definition (AAPCS calling convention)

Registers Rule
r0-r3 first 4 arguments; r0 (and r1 for 64-bit values) holds the result. The called function may modify them freely (caller-saved: if the caller needs them after the call, it saves them)
r4-r11 callee-saved: if the function uses them, it must save them at the start and restore them before returning
r12 temporary
sp must have the same value on entry and exit; 8-byte aligned at calls
lr return address

From the fifth argument on, arguments are passed on the stack.

Example. For sum3(1, 2, 3): r0=1, r1=2, r2=3, and the result comes back in r0=6.

The stack

Definition (full-descending stack: PUSH and POP). A memory area managed as a pile. In ARM it is full descending: it grows toward lower addresses and sp points to the last element inserted.

  • PUSH {r4, lr} = STMDB sp!, {r4, lr}: sp←sp−8sp \leftarrow sp - 8, then saves r4 at [sp][sp] and lr at [sp+4][sp+4].
  • POP {r4, pc} = LDMIA sp!, {r4, pc}: reloads r4 and puts the old lr in pc (so it returns), sp←sp+8sp \leftarrow sp + 8.

Example. With sp=0x8000, PUSH {r4, lr} takes sp to 0x7FF8, saves r4 at [0x7FF8] and lr at [0x7FFC].

The activation record (stack frame) of a function contains: saved registers, return address, local variables that do not fit in registers, the arguments beyond the fourth for the functions it calls.

Leaf function

It calls no other function: if it uses only r0-r3 and r12 it saves nothing.

c
int sum3(int a, int b, int c) { return a + b + c; }
armasm
sum3:   ADD r0, r0, r1
        ADD r0, r0, r2
        BX  lr

Call: MOV r0, #1 / MOV r1, #2 / MOV r2, #3 / BL sum3 gives r0 = 6.

Recursive function: factorial

c
int fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); }
armasm
fact:   CMP   r0, #1
        MOVLE r0, #1            @ base case: n <= 1, returns 1
        BXLE  lr
        PUSH  {r4, lr}          @ saves r4 (we use it) and lr (BL overwrites it)
        MOV   r4, r0            @ r4 = n, must survive the call
        SUB   r0, r0, #1
        BL    fact              @ r0 = fact(n - 1)
        MUL   r0, r4, r0        @ r0 = n * fact(n - 1)
        POP   {r4, pc}          @ restores r4 and returns

Evolution for fact(3) called from main with sp = 0x8000:

Moment sp Content added to the stack
entering fact(3), after PUSH 0x7FF8 r4 of main, lr toward main
fact(2), after PUSH 0x7FF0 r4 = 3, lr toward fact(3)
fact(1) 0x7FF0 none: base case, returns 1
return into fact(2) 0x7FF8 after POP none; r0 = 2 * 1 = 2
return into fact(3) 0x8000 after POP none; r0 = 3 * 2 = 6

The stack returns to its initial value: every PUSH has its POP. Each recursion level takes 8 bytes of stack; a recursion that is too deep exhausts the stack (stack overflow).

Local variables on the stack

armasm
f:      PUSH {r4, lr}
        SUB  sp, sp, #16        @ space for a local array of 4 integers
        STR  r0, [sp, #0]       @ local[0] = argument
        ...
        ADD  sp, sp, #16        @ releases the space
        POP  {r4, pc}

Typical mistakes

  • Not saving lr in a function that calls other functions: the return ends up in the wrong place (often an endless loop).
  • Relying on r0-r3 after a BL: they may have been modified.
  • PUSH and POP with different register lists: the stack stays unbalanced.

Versione ripasso

Call and return

BL f saves the next address in LR and jumps; BX lr returns. Nested calls overwrite LR: save it on the stack.

AAPCS

r0-r3: first 4 arguments, result in r0 (and r1 for 64 bit), free to modify (caller-saved). r4-r11: callee-saved, save at entry and restore. r12 temporary. sp equal on entry and exit, 8-byte aligned. More than 4 arguments: stack.

Stack

Full descending: grows toward lower addresses, sp points to the last element. PUSH {r4,lr} == STMDB sp!,{r4,lr} (sp←sp−8sp \leftarrow sp-8); POP {r4,pc} == LDMIA sp!,{r4,pc} (returns). With sp = 0x8000, PUSH →\to 0x7FF8, r4 at [0x7FF8], lr at [0x7FFC]. Activation record: saved registers, return address, locals, extra arguments. Locals: SUB sp,sp,#16 ... ADD sp,sp,#16.

Examples

  • Leaf: sum3: ADD r0,r0,r1; ADD r0,r0,r2; BX lr; sum3(1,2,3) gives 6.
  • Recursive factorial: CMP r0,#1; MOVLE r0,#1; BXLE lr; PUSH {r4,lr}; MOV r4,r0; SUB r0,r0,#1; BL fact; MUL r0,r4,r0; POP {r4,pc}. For fact(3) from sp = 0x8000: sp goes 0x7FF8, 0x7FF0, base case, back to 0x7FF8, 0x8000; result 6. 8 bytes per level; too deep means stack overflow.

Typical mistakes: not saving lr in a non-leaf function; relying on r0-r3 after BL; unbalanced PUSH and POP.

Teoria collegata