What is a stack frame and what does it contain?
A stack frame is the slice of the stack belonging to one function invocation — holding its return address, saved registers, locals, and space to build arguments for functions it calls.
* One frame stacks (high to low) caller args 7+, the return address, an optional saved %rbp, saved registers, locals, and the next call's argument-build area down to %rsp. *
Each active function owns one frame; together they stack up to mirror the chain of in-progress calls. Laid out high to low addresses:
| arguments 7+ | ← pushed by the caller
| return address | ← pushed by call
| saved %rbp | ← optional in x86-64
| saved registers | ← callee-saved regs this function uses
| local variables |
| argument build | ← space for the next call's stack args
| | ← %rsp
The frame pointer %rbp gives a stable anchor for accessing locals even as %rsp moves during the function:
- IA-32 almost always uses it.
- x86-64 frequently omits it (an optimization), addressing locals from
%rspinstead.
When %rbp is needed: for variable-length arrays, and for debuggers that walk the call stack by following the saved-%rbp chain.
pushq %rbp # save caller's frame pointer
movq %rsp, %rbp # set this frame's base
subq $N, %rsp # allocate locals
Go deeper:
Call stack (Wikipedia) — defines a stack frame per in-progress call.
Eli Bendersky — Stack frame layout on x86-64 — a detailed x86-64 frame-layout walkthrough.