A stack frame is the per-invocation region of the call stack that holds a function’s local variables, saved base pointer, and saved return address. The base pointer register RBP holds the address of the frame’s base and stays fixed for the call’s duration; the stack pointer RSP tracks the top of the frame and moves as values are pushed or popped. Each call pushes a new frame; each return pops it.
Layout
Frame contents, from high addresses to low
For a function called as long somefunc(long a, long b, long c) { long d; ... }, the stack frame grows downward as:
Locals are addressed as [rbp - N] for ever-larger N; arguments passed in registers are spilled to the frame as needed above the saved base pointer. The saved return address at rbp+8 is what makes a stack buffer overflow dangerous — see Buffer Overflow.
Lifecycle
Prologue and epilogue
A call to a function involves a jump to another memory location, and the function must return to the instruction after the original call. The prologue and epilogue bracket every call.
Prologue, on entry — set up the frame:
push rbp ; save the caller's base pointermov rbp, rsp ; this frame's RBP = current stack topsub rsp, 16 ; reserve space for locals
Epilogue, on exit — tear it down and return:
mov rsp, rbp ; restore stack top to the frame's basepop rbp ; restore the caller's base pointerret ; pop the saved return address and jump to it
The two-line epilogue mov rsp, rbp; pop rbp is abbreviated by the single instruction leave, so the canonical epilogue is leave; ret.
Scopes and Stack Frames
Obs
A function can access its parameters, variables in the global scope, and variables in its own stack frame — those are the variables “in scope”. When a called function is running, the caller’s frame is still on the stack but the caller’s locals are no longer addressable by name: only the active frame’s base pointer can reach them. After a function returns, its frame is destroyed and its locals are discarded, which is why returning a pointer to a local variable is a memory-safety hazard.