The AAPCS Calling Convention on 32-Bit ARM
The AAPCS rules for 32-bit ARM and Cortex-M: argument and result registers, what a callee must preserve, 64-bit pairs, stack alignment and hard-float.

Assembly and C agree on nothing except a contract, and on 32-bit ARM that contract is the Procedure Call Standard for the Arm Architecture, AAPCS. It decides which registers hold the arguments, which a function may destroy, how 64-bit values travel, and what the stack has to look like at a call. Break it and the failure is quiet: a value in the wrong register, a clobbered r4 in the caller, a stack that is 4-byte aligned where the callee needs 8. This post states the rules for Cortex-M as defined by the Arm ABI document IHI 0042, the AAPCS for the Arm architecture, and works two examples through register by register.
Registers and who owns them
| Register | Role in a call |
|---|---|
r0 to r3 | Argument and result registers. Caller-saved scratch: a callee may overwrite them |
r4 to r8, r10, r11 | Callee-saved. A function that uses them must restore them before return |
r9 | Platform register. In the baseline bare-metal ABI it is an ordinary callee-saved register; some platforms reserve it as a static base |
r12 | ip, intra-procedure scratch. Caller-saved. Veneers inserted by the linker may overwrite it |
r13 (sp) | Stack pointer, must stay 8-byte aligned at a public interface |
r14 (lr) | Return address on entry. Callee must save it if it makes calls |
r15 (pc) | Program counter |
The practical reading: your function can use r0 to r3 and r12 without ceremony and anything higher costs a push and a pop. The linker's use of r12 is the reason a function can never assume it survives a bl: a call to a distant function may go through a veneer that borrows it.
Passing arguments
Arguments are assigned left to right. Each 32-bit argument takes the next free register from r0 to r3. When the registers run out, the rest go on the stack, each in a word-aligned slot, in order, starting at [sp] at the moment of entry.
Types smaller than 32 bits are passed in a full register. The AAPCS says the caller extends them to a word, zero-extending unsigned types and sign-extending signed ones, so the callee can read the whole register. Assembly that may be called from other code should still not depend on the upper bits of a narrow argument unless it controls the caller.
A 64-bit argument (uint64_t, double in soft-float) occupies two registers and must start in an even-numbered one: r0 and r1, or r2 and r3. If the next free register is odd, it is skipped. That rule is the one most often misremembered.
Take uint64_t addmix(uint32_t a, uint64_t b). The first argument goes in r0. The 64-bit b cannot start at r1, which is odd, so r1 stays unused and b takes r2 and r3, low word in r2 and high word in r3 on a little-endian system. The result, a 64-bit value, comes back in r0 (low) and r1 (high).
.syntax unified
.cpu cortex-m3
.thumb
.text
.global addmix
.type addmix, %function
.thumb_func
// uint64_t addmix(uint32_t a, uint64_t b)
// a in r0, b in r2:r3, result in r1:r0
addmix:
adds r0, r2, r0 // low word = b_lo + a, sets carry
adc r1, r3, #0 // high word = b_hi + carry
bx lr
.size addmix, .-addmix#include <stdint.h>
#include <stdio.h>
uint64_t addmix(uint32_t a, uint64_t b);
int main(void)
{
uint64_t r = addmix(1u, 0xFFFFFFFFull);
printf("%llx\n", (unsigned long long)r);
return 0;
}100000000b_lo is 0xFFFFFFFF, adding 1 gives zero with a carry out, and adc adds that carry to the zero high word. The register pairing is what makes the code short, and the empty r1 is why you cannot use r1 as a destination for a 32-bit pass-through.
Return values
A result of up to 32 bits comes back in r0. A 64-bit result uses r0 and r1. Composite types (structs, unions, arrays) of 4 bytes or fewer are returned in r0. Anything larger is returned through memory: the caller allocates space and passes its address as a hidden first argument in r0, shifting the visible arguments up one register, and the callee writes through it.
Composite arguments passed by value are copied into registers as if they were a sequence of words, up to the four argument registers. Larger ones are copied onto the stack. For this reason a function taking a 20-byte struct by value consumes stack space on every call, and C style guides on Cortex-M tell you to pass a pointer.
The stack at a call
At any public interface, sp must be 8-byte aligned. A leaf function that never calls anything and uses only a word at a time can get away with less; the standard only requires the alignment where control leaves the function through a call or return. A function that pushes an odd number of registers and then calls another one breaks it. The usual fix is to pad the push:
push {r4, r5, r6, lr} // 16 bytes: aligned if entered alignedFour registers, 16 bytes. Three would leave sp 4 modulo 8, so add a dummy register, for example push {r3, r4, r5, lr}, when you need a call from the middle of the function. On exception entry the core aligns the stack to 8 bytes itself when the STKALIGN bit in the configuration and control register is set, which the core's technical reference manual describes. Do not rely on it in code that runs before the system is configured, and keep your own frames aligned.
Floating point and the hard-float ABI
The default for arm-none-eabi-gcc -mfloat-abi=soft is that float and double travel in integer registers. -mfloat-abi=softfp uses the hardware FPU for arithmetic but still passes values in core registers. -mfloat-abi=hard is a different ABI: floating-point arguments travel in s0 to s15, and double in d0 to d7, results come back in s0 or d0, and s16 to s31 become callee-saved. Objects built with different float ABIs cannot be linked together; the linker reports an error that the .ARM.attributes section disagrees. The post on picking compiler flags for each core covers the choice.
Veneers and long calls
bl reaches plus or minus 16 MB on Armv7-M. For a farther target, or a call between Thumb and a different state, the linker inserts a veneer, a short stub. That is why r12 can change between a bl and the first instruction of the callee. The standard calls it the intra-procedure scratch register for that reason, and it is one you are best off treating as dead across any call.
What assembly writers get wrong
Treating
r4tor11as scratch because a test passed once. The caller's value is gone, and the bug shows in a different function.Not saving
lrin a function that calls another withbl. The return then branches to its own body.Forgetting that a 64-bit argument skips an odd register.
Returning a struct larger than four bytes in
r0instead of writing through the hidden pointer.Assuming
r9is free. On some platforms, such as certain RTOS ports, the linker or the OS reserves it.


