What are the x86_64 argument-passing registers and which are caller-saved vs callee-saved?
The first six integer/pointer arguments go in %rdi, %rsi, %rdx, %rcx, %r8, %r9; the return value comes back in %rax. %rbx, %rbp, %r12-%r15 are callee-saved, the rest caller-saved.
* System V AMD64 ABI: args in %rdi,%rsi,%rdx,%rcx,%r8,%r9; return in %rax. Argument registers are caller-saved (volatile); a push %rbx at entry signals a callee-saved register. *
| Argument | Register |
|---|---|
| 1st | %rdi |
| 2nd | %rsi |
| 3rd | %rdx |
| 4th | %rcx |
| 5th | %r8 |
| 6th | %r9 |
| Return | %rax |
Caller-saved (function may clobber — save before call if needed):
%rax, %rcx, %rdx, %rsi, %rdi, %r8-%r11
Callee-saved (function must preserve — save at entry, restore before ret):
%rbx, %rbp, %r12-%r15
Tip: If you see push %rbx at the start of a function and pop %rbx at the end, the function uses %rbx internally and must preserve it (callee-saved).
Go deeper:
x86 calling conventions — System V AMD64 ABI (Wikipedia) — States the six argument registers and the caller/callee-saved split verbatim.
Stack frame layout on x86-64 (Eli Bendersky) — Worked walk-through of the same ABI with disassembly showing where each argument register lands.