← again tap: answer good → ↑ swipe up at the end: next
Question
What is the stack and how does it grow in x86-64?
Answer
The stack is a LIFO region of memory for function call data; it grows downward (toward lower addresses), with %rsp always pointing at the lowest in-use address.
* The stack grows downward: push subtracts 8 from %rsp then writes; pop reads then adds 8 back. *
The runtime stack is where functions store return addresses, saved registers, and locals. Its defining behaviors all follow from "grows down": push decreases %rsp then writes, pop reads then increases %rsp.
push %rax # %rsp -= 8 ; [%rsp] = %rax
pop %rbx # %rbx = [%rsp] ; %rsp += 8
You can do the same thing manually, which is exactly what sub $N,%rsp for local space does:
sub $8, %rsp # allocate 8 bytes (like push, without storing)
mov %rax, (%rsp) # store there
mov (%rsp), %rbx # read it back
add $8, %rsp # deallocate (like pop)
A typical frame, high to low addresses:
[ arguments 7+ ] ← pushed by caller
[ return address ] ← pushed by call
[ saved %rbp ] ← pushed by prologue
[ local variables ]
← %rsp points here
Why downward? It's convention from early systems, letting the stack and the heap grow toward each other from opposite ends of the address space.
Go deeper:
Call stack (Wikipedia) — the runtime call stack: return addresses and LIFO nesting.
Stack (Wikipedia) — the underlying LIFO push/pop abstraction.
Question
What are the exact effects of PUSH and POP instructions in x86-64?
Answer
pushq first subtracts 8 from %rsp, then writes; popq first reads, then adds 8 to %rsp — exactly because the stack grows downward.
The order of the two steps is what people get wrong, so it's worth memorizing precisely:
| Instruction | Effect |
|---|---|
pushq S |
%rsp ← %rsp − 8; then M[%rsp] ← S |
popq D |
D ← M[%rsp]; then %rsp ← %rsp + 8 |
Each is exactly equivalent to a pair of plain instructions, which is the clearest way to see what's happening:
# push %rax ==
subq $8, %rsp
movq %rax, (%rsp)
# pop %rbx ==
movq (%rsp), %rbx
addq $8, %rsp
Note: size modifiers exist (push[b,w,l,q], pop[b,w,l,q]), but in 64-bit code pushq/popq are overwhelmingly the common forms, since the stack works in 8-byte units. Good practice is to keep pushes and pops balanced and in reverse order (push a, b, c → pop c, b, a).
Go deeper:
Felix Cloutier — PUSH — PUSH's decrement-then-store order.
Felix Cloutier — POP — POP's read-then-increment order.