Course: Computer Architectures
Source lesson: ARM Subroutines and Procedure Calls

Three parameter strategies

A caller can pass data:

  1. Directly in registers.
  2. By reference, passing an address in a register.
  3. 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  sub1

The 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   sub2

The 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   sub3

The 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 r2

This 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:

RegistersRole
r0-r3First four arguments; scratch registers
r4-r11Callee-saved registers
r12Scratch register (IP)
r13SP, stack pointer
r14LR, link register
r15PC, 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-r11 and SP.
  • Why are stack offsets #16, #20, and #24 after saving four words? The callee’s four saved words occupy 16 bytes below the caller’s stack arguments.