What is the ARM64 calling convention?
Short answer: AAPCS64 passes ordinary integer/pointer arguments in X0–X7 and returns simple integer results in X0. X19–X28 are callee-saved; SP must be 16-byte aligned at public interfaces.
add_two:
ADD X0, X0, X1
RET
1. Register rules (AAPCS64)
| Registers | Role |
|---|---|
| X0–X7 | Ordinary integer/pointer arguments |
| X0 | Typical scalar result |
| X19–X28 | Callee-saved |
| X29 / X30 | Frame pointer convention / link register |
| SP | 16-byte aligned at public interfaces |
2. Example: Calling add_two(10, 20) passes 10 in X0 and 20 in X1. ADD leaves 30 in X0 and RET returns to the caller.
3. Extra arguments: Additional ordinary arguments may be passed on the stack. Floating-point and aggregate arguments follow additional ABI rules. X18 is platform-specific.
Interview takeaway: The ABI lets independently compiled functions agree on arguments, results and register preservation.
Caller-saved and callee-saved example
// long wrapper(long x) { return helper(x) + 1; }
wrapper:
STP X29, X30, [SP, #-16]!
MOV X29, SP
BL helper
ADD X0, X0, #1
LDP X29, X30, [SP], #16
RET
BL overwrites X30, so this non-leaf function preserves its original return address. Ordinary integer arguments and temporary values in caller-saved registers may be overwritten by helper. If wrapper needs a value after the call, it must save it or use a callee-saved register while preserving that register's incoming value.
AAPCS64 also defines X8 for certain indirect result locations, X16/X17 as intra-procedure-call temporaries, and X18 as platform-specific. X29 is a callee-saved register conventionally used as frame pointer. Floating-point and SIMD arguments/results typically use V0–V7, and parts of V8–V15 have preservation requirements. Aggregates and variadic functions have additional rules.
Do not confuse the C function-call ABI with the Linux syscall ABI, which uses X8 for the syscall number and X0–X5 for its arguments.