Calling conventions and the stack
The AAPCS: arguments, return values, saved registers and stack frames.
The contract between caller and callee
When one function calls another, both must agree where the arguments go, where the result comes back, which registers the callee may destroy, and how the stack is laid out. That agreement is the calling convention. On Arm it is defined by the AAPCS (Procedure Call Standard for the Arm Architecture), part of the Arm EABI that arm-none-eabi is named after. Every compiler, library and assembly routine on the platform follows it, which is why you can call a vendor's precompiled library or write an assembly function callable from C.
Arguments and return values
| What | Where (AAPCS, 32-bit Arm) |
|---|---|
| First four 32-bit arguments | r0, r1, r2, r3 |
| Further arguments | On the stack, in order |
64-bit arguments (uint64_t, double with soft float) | An even/odd register pair (r0:r1 or r2:r3), skipping a register if needed |
| Return value up to 32 bits | r0 |
| 64-bit return value | r0:r1 |
| Larger structs returned | Caller passes a hidden pointer in r0 to space it reserved; callee writes there |
Floating point with -mfloat-abi=hard | Arguments in s0–s15 / d0–d7, result in s0 / d0 |
Practical consequences:
- Functions with up to four word-sized arguments are cheapest: everything stays in registers.
- A
uint64_tfirst argument plus auint32_tsecond usesr0:r1andr2; put auint32_tfirst and theuint64_tsecond and it usesr0and thenr2:r3(r1 is skipped for alignment). Argument order can change cost. - Passing a large struct by value copies it; pass a
constpointer instead.
Who saves which registers
| Registers | Category | Rule |
|---|---|---|
r0–r3, r12 | Caller-saved (scratch) | The callee may overwrite them freely; a caller needing them after a call must save them itself |
r4–r11 | Callee-saved | A function that uses them must restore them before returning |
lr | Link register | Overwritten by every bl, so a non-leaf function saves it |
sp | Stack pointer | Must be restored exactly; 8-byte aligned at every public function call |
s16–s31 | Callee-saved (FPU) | As r4–r11 |
That split is why a Cortex-M interrupt handler can be an ordinary C function: on exception entry the hardware automatically stacks exactly the caller-saved set (r0–r3, r12, lr, pc, xPSR), and the compiled handler saves any callee-saved registers it uses, just like any function.
The stack frame
The stack grows downwards (towards lower addresses). A typical non-leaf function:
higher addresses
| caller's frame |
| arguments 5, 6, ... | <- placed by the caller
| saved lr | \
| saved r4..r7 | } pushed by the callee's prologue
| local variables | /
| outgoing arguments | <- for its own calls
+------------------------+ <- sp (8-byte aligned at calls)
lower addresses
The 8-byte alignment rule exists so that 64-bit values (double, uint64_t) and LDRD/STRD work on the stack; Cortex-M hardware also realigns the stack on exception entry when STKALIGN is set.
Watching the convention in action
This program reproduces what the caller and callee do with a simulated register file and stack, for a function that takes six arguments:
#include <stdio.h>
#include <stdint.h>
static uint32_t reg[16]; /* r0..r15 */
static uint32_t stack[32];
static int sp = 32; /* index; grows down */
/* The callee: arguments 1-4 in r0-r3, 5 and 6 on the stack. Result in r0. */
static void weighted_sum(void) {
uint32_t a5 = stack[sp], a6 = stack[sp + 1];
reg[0] = reg[0] * 1 + reg[1] * 2 + reg[2] * 3 + reg[3] * 4 + a5 * 5 + a6 * 6;
}
int main(void) {
/* Caller: weighted_sum(1, 2, 3, 4, 5, 6) */
reg[0] = 1; reg[1] = 2; reg[2] = 3; reg[3] = 4;
stack[--sp] = 6; /* push arguments right to left */
stack[--sp] = 5;
weighted_sum(); /* bl weighted_sum */
sp += 2; /* caller pops its stack arguments */
printf("result in r0 = %u, sp restored to %d\n", reg[0], sp);
return 0;
}
Two of six arguments cost memory traffic; with four or fewer, none would.
Variadic functions
printf-style functions (...) pass everything in core registers and on the stack, even floating-point values with the hard-float ABI, and float arguments are promoted to double. That is one reason printf with %f pulls in a lot of code on small targets.
Calling assembly from C and C to assembly
An assembly routine called from C must follow the AAPCS: take arguments in r0–r3, return in r0, preserve r4–r11 and sp, return with bx lr.
.syntax unified
.thumb
.global add_saturate
.type add_saturate, %function
@ uint32_t add_saturate(uint32_t a, uint32_t b)
add_saturate:
adds r0, r0, r1 @ r0 = a + b, carry set on unsigned overflow
it cs
movcs r0, #0xFFFFFFFF @ saturate if it carried
bx lr
uint32_t add_saturate(uint32_t a, uint32_t b); /* prototype in a header */
In C++, declare such functions extern "C" so the name is not mangled.
Other conventions you may meet
| Platform | Arguments in registers | Notes |
|---|---|---|
| AArch64 (Cortex-A, 64-bit) | x0–x7 | 16-byte stack alignment |
| RISC-V | a0–a7 | ra link register; s0–s11 callee-saved |
| x86-64 System V (Linux, macOS) | rdi, rsi, rdx, rcx, r8, r9 | Relevant when testing firmware logic on a PC |
Pitfall: An assembly routine that uses
r4without saving it works in every test until the compiler, in some caller, keeps a live value inr4across the call. The corruption then appears far from the culprit. Push and pop every callee-saved register you touch.
Interview question: Why can a Cortex-M interrupt handler be written as a normal C function?
Answer: On exception entry the hardware automatically pushes the registers the AAPCS says a called function may destroy: r0 to r3, r12, lr, pc and xPSR (plus the FPU caller-saved registers when needed). The handler, compiled as an ordinary function, then saves and restores any callee-saved registers it uses, just as any C function does. Returning with a special EXC_RETURN value in lr makes the hardware unstack the rest. So the interrupted code sees every register preserved, with no special compiler keyword needed.
Interview question: How does argument count and order affect performance on Arm?
Answer: The first four word-sized arguments travel in r0 to r3; more go on the stack, costing stores in the caller and loads in the callee. 64-bit arguments need an even/odd register pair, so a 32-bit argument before a 64-bit one can leave a register unused and push later arguments onto the stack. Keeping hot functions to four or fewer arguments, grouping many parameters into a struct passed by const pointer, and ordering 64-bit arguments first all help.
Build it: where do the arguments go?
This is step 3 of the module project. The motor maths is called 24,000 times a second, so the team wants calls that cost nothing extra. On Arm, the first arguments travel in registers r0 to r3; the rest go on the stack, which costs loads and stores. A tool that shows where each argument lands lets the team design cheap function signatures.
Lab: Place arguments the way the Arm procedure call standard does
The rules of the Arm Procedure Call Standard (AAPCS), for arguments of 4 bytes (int, pointers, float) and 8 bytes (int64_t, double), taken left to right:
- A 4-byte argument goes in the next free register
r0tor3. - An 8-byte argument needs an even/odd pair:
r0:r1orr2:r3. If the next free register is odd, skip it (it stays unused). - Once an argument does not fit in the registers, every register is considered used: no later argument goes back into a register, even a small one.
- On the stack, a 4-byte argument takes the next 4-byte-aligned offset and an 8-byte argument the next 8-byte-aligned offset.
Write place(sizes, n, out), which writes one line per argument into out (an array of strings of 16 bytes) such as r0, r2:r3, stack+0, and returns the number of bytes used on the stack, rounded up to a multiple of 8 (the stack stays 8-byte aligned at calls).
Going deeper
Interview question: How would you make a motor drive record where its firmware was when it crashed?
Answer: In the fault handler, save the stacked registers (PC, LR, xPSR) and a few words of the stack to a no-init RAM area or flash, plus a reset reason, then reset. On the next boot, report it. With the calling convention you can unwind the stack to a call chain offline using the map file, which turns rare field crashes into fixable bugs.
Interview question: Why must the stack be 8-byte aligned at function calls on Arm?
Answer: The AAPCS requires 8-byte alignment at public interfaces so that 64-bit types such as double and uint64_t, and the LDRD and STRD instructions used to access them, are naturally aligned on the stack. Code compiled assuming this may fault or misbehave if called with a misaligned stack, for example from hand-written assembly or an exception entry without stack realignment. Cortex-M hardware realigns on exception entry when the STKALIGN bit is set (the default on recent cores).
Interview question: What changes between -mfloat-abi=softfp and -mfloat-abi=hard at the calling-convention level?
Answer: Both may use FPU instructions inside functions, but softfp passes floating-point arguments and results in core registers r0 to r3 (so it stays compatible with soft-float code), while hard passes them in FPU registers s0 to s15 or d0 to d7 and returns in s0 or d0, avoiding moves between register files. Because the conventions differ, every object and library in a link must use the same choice, otherwise arguments are read from the wrong registers.
Explain it: In your own words, explain the difference between caller-saved and callee-saved registers and why the split exists.
Answer: Caller-saved (scratch) registers may be overwritten by any function call, so if the caller still needs a value in one of them after the call, it must save it first. Callee-saved registers must look unchanged after a call, so a function that wants to use one has to save and restore it itself. The split is a compromise: short leaf functions can use the scratch registers without any saving, while long-lived values in a caller can sit in callee-saved registers across many calls without being saved each time. On Arm, r0 to r3 and r12 are scratch and r4 to r11 are callee-saved.