Course: Computer Architectures
Source lesson: ARM Subroutines and Procedure Calls
Three parameter strategies
A caller can pass data:
- Directly in registers.
- By reference, passing an address in a register.
- On the stack.
Register passing is fastest and simplest. Reference passing lets both routines share a memory object. Stack passing supports more arguments but requires careful offset calculation.
Register parameters
The source’s absolute-difference example passes inputs in r0 and r1, then returns the result in r2:
MOV r0, #0x34
MOV r1, #0xA3
BL sub1The callee compares the unsigned values and subtracts in the correct order.
Reference parameters
The caller can pass a pointer:
LDR r3, =mySpace
STMIA r3, {r0, r1}
BL sub2The callee loads the inputs from [r3] and writes the result back through the same address. The register carries the location, not the values themselves.
Stack parameters and offsets
Suppose the caller does:
PUSH {r0, r1, r2}
BL sub3The callee then saves four words:
PUSH {r4, r5, r6, LR}The callee’s SP has moved by 16 bytes. With the full descending layout, the caller’s values are now:
[SP,#16] = original r0
[SP,#20] = original r1
[SP,#24] = original r2This is why the callee can use:
LDR r4, [SP, #16]
LDR r5, [SP, #20]
STR r6, [SP, #24]The offset is measured from the callee’s current SP, after its own saved registers have been pushed.
AAPCS register agreement
The ARM Architecture Procedure Call Standard (AAPCS) defines caller and callee responsibilities:
| Registers | Role |
|---|---|
r0-r3 | First four arguments; scratch registers |
r4-r11 | Callee-saved registers |
r12 | Scratch register (IP) |
r13 | SP, stack pointer |
r14 | LR, link register |
r15 | PC, program counter |
The caller puts the first four arguments in r0-r3; additional arguments go on the stack. The callee must preserve r4-r11 and SP, while it may use r0-r3 and r12 as scratch registers. The caller is responsible for leaving the stack as it was before the call.
Return values use:
- 32-bit value:
r0 - 64-bit value:
r0-r1 - 128-bit value:
r0-r3
Retrieval questions
- Where does the fifth argument go under AAPCS? On the stack.
- Which registers must the callee preserve?
r4-r11andSP. - Why are stack offsets
#16,#20, and#24after saving four words? The callee’s four saved words occupy 16 bytes below the caller’s stack arguments.