Registers
%rip(Instruction Pointer): Address of the next instruction to execute.%rsp(Stack Pointer): Points to the top of the stack (lowest currently allocated address).%rbp(Base Pointer): Optional fixed reference point for the current stack frame.%rax: Return value register.%rdi,%rsi,%rdx,%rcx,%r8,%r9: First six integer/pointer argument registers (System V AMD64 ABI).
Important
The stack grows toward lower memory addresses.
Example
long add(long a, long b) {
long local_a = a;
long local_b = b;
return local_a + local_b;
}
int main(void) {
long result = add(10, 20);
return 0;
}1. Procedure Call
caller:
movq $10, %rdi # First argument = 10
movq $20, %rsi # Second argument = 20
call add # Push return address; jump to add
movq %rax, %rbx # Continue here after return; RBX = result
call add performs:
RSP ← RSP - 8M[RSP] ← return address(address immediately aftercall)RIP ← address of add
The return address is automatically pushed by the CPU, not manually by the caller.
2. Stack Frame Setup (Prologue)
add:
pushq %rbp # Save caller's RBP on the stack
movq %rsp, %rbp # Establish current stack frame base
subq $16, %rsp # Reserve 16 bytes for local variables
pushq %rbp is equivalent to:
subq $8, %rsp # Stack grows downward; reserve 8 bytes
movq %rbp, (%rsp) # Store old RBP at the new stack top
%rbp provides a fixed reference point, while %rsp may change during function execution.
Stack Frame Layout
Higher memory addresses
────────────────────────────────────
RBP + 16 Stack arguments (if any)
────────────────────────────────────
RBP + 8 Return Address
────────────────────────────────────
RBP Saved Old RBP ← RBP
────────────────────────────────────
RBP - 8 Local Variable 1
────────────────────────────────────
RBP - 16 Local Variable 2 ← RSP
────────────────────────────────────
Lower memory addresses
In this traditional frame layout:
- Local stack space:
[RSP, RBP) - Saved RBP:
[RBP, RBP+8) - Return address:
[RBP+8, RBP+16) - Entire frame including return address:
[RSP, RBP+16)(excluding any stack arguments above it).
3. Procedure Return (Epilogue)
leave # Restore RSP and caller's RBP
ret # Pop return address into RIP
leave is equivalent to:
movq %rbp, %rsp # Discard local stack space
popq %rbp # Restore caller's RBP; RSP += 8
After leave, %rsp points to the return address, and the caller’s old rbp address (now is on the top of the stack) is popped into%rbp
ret performs:
RIP ← M[RSP]RSP ← RSP + 8
After ret, %rsp is restored to its value immediately before call (assuming balanced stack operations).
5. Complete Example
caller:
movq $10, %rdi # Argument a = 10
movq $20, %rsi # Argument b = 20
call add # Save return address and jump to add
movq %rax, %rbx # Store returned result (30) in RBX
add:
pushq %rbp # Save caller's frame pointer
movq %rsp, %rbp # Set up new frame pointer
subq $16, %rsp # Allocate 16 bytes of local stack space
movq %rdi, -8(%rbp) # Local a = 10
movq %rsi, -16(%rbp) # Local b = 20
movq -8(%rbp), %rax # RAX = a
addq -16(%rbp), %rax # RAX = a + b = 30
leave # Restore caller's RBP and stack pointer
ret # Restore RIP; return to caller