The stack
| PUSH and POP | |
| Stack layout | |
| Alignment |
PUSH and POP
PUSH stores a value at the top of the stack and moves RSP down by 8. POP loads the top value and moves RSP up by 8. The stack grows downward on x86:
push rax ; store rax, rsp = rsp - 8
push rbx
pop rcx ; rcx = rbx, rsp = rsp + 8
pop rdx ; rdx = rax
Stack layout
RSP points at the top (lowest address) of the stack. RBP conventionally points at the base of the current stack frame, so locals and saved registers sit at fixed offsets from RBP while RSP moves:
push rbp
mov rbp, rsp
sub rsp, 16 ; 16 bytes of locals
mov [rbp-8], rax ; local variable
Alignment
Keep RSP 16-byte aligned at call boundaries on Linux and macOS: the ABI expects it. Push an even number of registers, or pad with a dummy push, before any CALL.
Next: Procedures
Article author: Arthur Isaev