LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 7 Part 1 of 3
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

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 growing downward with %rsp at the top and push/pop rules.

* 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:

A typical call stack, storing local data and call information for multiple levels of procedure calls. This stack grows downward from its origin. The stack pointer points to the current topmost datum on the stack. A push operation decrements the pointer and copies the data to the stack; a pop operation copies data from the stack and then increments the pointer. Each procedure called in the program stores procedure return information (in yellow) and local data (in other colors) by pushing them onto the stack. This type of stack implementation is extremely common, but it is vulnerable to buffer overflow attacks (see the text).
A typical call stack, storing local data and call information for multiple levels of procedure calls. This stack grows downward from its origin. The stack pointer points to the current topmost datum on the stack. A push operation decrements the pointer and copies the data to the stack; a pop operation copies data from the stack and then increments the pointer. Each procedure called in the program stores procedure return information (in yellow) and local data (in other colors) by pushing them onto the stack. This type of stack implementation is extremely common, but it is vulnerable to buffer overflow attacks (see the text).
Vectorization: Alhadis · Public domain · Wikimedia Commons
or press any other key

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:

or press any other key