What is the x86_64 calling convention for passing arguments and returning values?
The first 6 integer/pointer arguments go in %rdi, %rsi, %rdx, %rcx, %r8, %r9 (in that order); any extras go on the stack; the return value comes back in %rax.
* First six int/pointer args in rdi, rsi, rdx, rcx, r8, r9; extras on the 16-byte-aligned stack; return in rax. *
x86_64 prefers registers over the stack for speed. There's a fixed sequence of registers for the first six integer-class arguments; argument seven onward spills onto the stack.
| Argument | Register |
|---|---|
| 1st | %rdi |
| 2nd | %rsi |
| 3rd | %rdx |
| 4th | %rcx |
| 5th | %r8 |
| 6th | %r9 |
| 7th+ | Stack |
| Return | %rax |
The stack frame is referenced through the stack pointer %rsp (no dedicated frame pointer is required), and the ABI mandates the stack be 16-byte aligned at each call.
Mnemonic: "Diane's silk dress costs 8 to 9 dollars" → rdi, rsi, rdx, rcx, r8, r9.
Tip: When reading disassembly, if you see values loaded into %rdi/%rsi/… right before a call, those are the function's arguments being set up.
Go deeper:
x86 calling conventions (Wikipedia) — the x86-64 System V convention.
Eli Bendersky — Stack frame layout on x86-64 — a worked walk-through of x86-64 stack frames.
Calling convention (Wikipedia) — calling conventions in general.