Skip to main content
Be Bitwise
x86 AssemblyStack & Calling Conventions

Stack Frames and Local Variables

Every function call carves out a stack frame — a tidy block on the stack that holds the function's locals, saved registers, and the return address. Reading stack frames is foundational for debugging, reverse engineering, and grokking buffer overflow exploits.

The Function Prologue

A function usually opens with a prologue that builds its stack frame:

nasm
my_function:
push rbp ; save the caller's frame pointer
mov rbp, rsp ; set our frame pointer to current stack top
sub rsp, 32 ; allocate 32 bytes for local variables

After this prologue, rbp points at the saved frame pointer, the return address sits just above it at [rbp + 8], and the 32 bytes of locals sit below it. The visualiser shows the same shape with a smaller 16-byte locals area, freshly allocated and not yet written. The saved rbp and return address values are illustrative, and only the return address's low byte is shown:

Stack Frame View

0000Buffer← SP
0100Buffer
0200Buffer
0300Buffer
0400Buffer
0500Buffer
0600Buffer
0700Buffer
0800Buffer
0900Buffer
0A00Buffer
0B00Buffer
0C00Buffer
0D00Buffer
0E00Buffer
0F00Buffer
1030Saved rbp
11E4Saved rbp
12FFSaved rbp
13FFSaved rbp
14FFSaved rbp
157FSaved rbp
1600Saved rbp
1700Saved rbp
1836Return addr
1900Return addr
1A00Return addr
1B00Return addr
1C00Return addr
1D00Return addr
1E00Return addr
1F00Return addr
Regions
Buffer16 B
Saved rbp8 B
0x00007fffffffe430
Return addr8 B
0x0000000000000036
Overflow direction
Buffer→Saved rbp→Return addr

The Function Epilogue

When the function finishes, the epilogue tears the frame back down:

nasm
; Option 1: explicit restoration
add rsp, 32 ; deallocate locals (or: mov rsp, rbp)
pop rbp ; restore caller's frame pointer
ret ; pop return address and jump to it
; Option 2: using LEAVE (does mov rsp, rbp; pop rbp)
leave ; equivalent to: mov rsp, rbp; pop rbp
ret

LEAVE does both jobs in one instruction: it copies RBP to RSP (yanking all locals at once) and pops the saved RBP. Compilers reach for it constantly.

Accessing Local Variables

With the frame pointer in place, locals live at negative offsets from RBP:

nasm
; A function with three local variables
; name size offset bytes covered
; int x 4 rbp-4 rbp-4 .. rbp-1
; (padding) 4 rbp-8 .. rbp-5 keeps y 8-byte aligned
; long y 8 rbp-16 rbp-16 .. rbp-9
; char buf[16] 16 rbp-32 rbp-32 .. rbp-17
my_function:
push rbp
mov rbp, rsp
sub rsp, 32 ; 4 + 4 padding + 8 + 16 = 32 bytes
; Access locals via RBP
mov dword [rbp - 4], 42 ; x = 42
mov qword [rbp - 16], 100 ; y = 100 (aligned to 8 bytes)
mov byte [rbp - 32], 'A' ; buf[0] = 'A'
; Use locals
mov eax, [rbp - 4] ; load x into eax
add eax, 1 ; x + 1
mov dword [rbp - 4], eax ; store back
leave
ret

Accessing Function Parameters

System V hands you the first six arguments in registers. If a function needs those registers for other work, it stashes the arguments on the stack:

nasm
; void process(int a, int b, int c)
; a in edi, b in esi, c in edx
process:
push rbp
mov rbp, rsp
sub rsp, 32
; Save parameters as local variables
mov [rbp - 4], edi ; save a
mov [rbp - 8], esi ; save b
mov [rbp - 12], edx ; save c
; Now rdi, rsi, rdx are free to use for other purposes
; Access parameters via stack: [rbp - 4], [rbp - 8], [rbp - 12]
leave
ret

Functions with more than six arguments find parameters 7+ on the stack at positive offsets from RBP:

nasm
; Parameter 7 is at [rbp + 16] (rbp+8 is return addr, rbp+16 is 7th arg)
; Parameter 8 is at [rbp + 24]
; etc.
Frame Pointer Omission (-fomit-frame-pointer)

Modern compilers often skip the frame pointer setup entirely. Instead of RBP, they access locals via direct RSP offsets, which frees up RBP as a bonus general-purpose register. GCC does this by default from -O1 onwards. The catch: stack unwinding (debuggers, exception handlers, profilers) gets harder. The compiler has to emit .eh_frame metadata describing the stack layout at every instruction. That's why backtraces in optimised code sometimes look chewed-up.

Complete Stack Frame Example

Here's a function that lays out a full frame top to bottom:

nasm
; long calculate(long a, long b)
; Returns a*a + b*b
global calculate
calculate:
push rbp ; save caller's frame pointer
mov rbp, rsp ; establish our frame
sub rsp, 16 ; space for two local qwords
; Save parameters
mov [rbp - 8], rdi ; local: a_squared (will compute later)
mov [rbp - 16], rsi ; local: b_squared
; Compute a*a
mov rax, rdi
imul rax, rdi ; rax = a * a
mov [rbp - 8], rax ; save a_squared
; Compute b*b
mov rax, rsi
imul rax, rsi ; rax = b * b
mov [rbp - 16], rax ; save b_squared
; Return a_squared + b_squared
mov rax, [rbp - 8]
add rax, [rbp - 16] ; rax = a*a + b*b
leave ; restore frame
ret

Here's that frame for calculate(3, 4), frozen just before leave. Offsets in the left column count up from rsp, which is also rbp - 16. The first eight bytes are b_squared (16, so 10 in the low byte, little-endian), the next eight are a_squared (9), and then comes the saved rbp. The values of the saved rbp and of the return address are illustrative, and the visualiser shows only the return address's low byte, 0x36 from 0x401136.

Stack Frame View

0010Buffer← SP
0100Buffer
0200Buffer
0300Buffer
0400Buffer
0500Buffer
0600Buffer
0700Buffer
0809Buffer
0900Buffer
0A00Buffer
0B00Buffer
0C00Buffer
0D00Buffer
0E00Buffer
0F00Buffer
1030Saved rbp
11E4Saved rbp
12FFSaved rbp
13FFSaved rbp
14FFSaved rbp
157FSaved rbp
1600Saved rbp
1700Saved rbp
1836Return addr
1900Return addr
1A00Return addr
1B00Return addr
1C00Return addr
1D00Return addr
1E00Return addr
1F00Return addr
Regions
Buffer16 B
Saved rbp8 B
0x00007fffffffe430
Return addr8 B
0x0000000000000036
Overflow direction
Buffer→Saved rbp→Return addr

Hover the bands. The blue "Buffer" band is the two locals ([rbp - 16] and [rbp - 8]), the grey band is the saved rbp at [rbp], and the red one is the return address at [rbp + 8].

Stack Frames with Callee-Saved Registers

When a function needs to use callee-saved registers, it pushes them in the prologue:

nasm
complex_function:
push rbp
mov rbp, rsp
push rbx ; save callee-saved registers
push r12
push r13
sub rsp, 24 ; 24 bytes for locals
; (3 pushes + 24 bytes = 48, which maintains
; 16-byte alignment with the return address)
; Use rbx, r12, r13 freely
mov rbx, rdi
mov r12, rsi
mov r13, rdx
; Access locals
mov qword [rbp - 32], 0 ; first local (after 3 saved regs * 8)
mov qword [rbp - 40], 0 ; second local
mov qword [rbp - 48], 0 ; third local
; ... do work ...
; Epilogue: deallocate locals and restore saved registers
add rsp, 24
pop r13 ; restore in reverse order
pop r12
pop rbx
pop rbp
ret
Security Concept: Stack Canaries

A stack canary (a.k.a. stack cookie, stack guard) is a random value tucked between the locals and the saved return address. The function checks it on the way out: if the value has changed, a buffer overflow trampled past the locals, and the program aborts rather than returning to a corrupted address. The layout with a canary in place:

c
[locals] [CANARY] [saved RBP] [return address]

GCC adds canaries automatically with -fstack-protector. The canary is held in a thread-local variable, checked against the on-stack copy at return time. Mismatch and __stack_chk_fail aborts the process. To exploit a stack overflow without tripping the alarm, an attacker has to know — or guess — the canary value first.

Debugging Stack Frames with GDB

GDB knows about stack frames and will walk the entire call chain for you:

bash
gdb ./program
(gdb) break calculate
(gdb) run
(gdb) info frame # show current frame details
(gdb) backtrace # show the complete call chain
(gdb) info locals # show local variables (if debug info available)
(gdb) x/8gx $rsp # examine 8 qwords starting at RSP
(gdb) x/4gx $rbp-32 # examine memory around local variables

Reading stack frames in a debugger is one of the most useful skills you can pick up. Crash dumps, exploit analysis, reverse engineering — they all live and die by stack frames.

What is Next

With stack frames mapped out, the next lesson is about writing whole functions — leaf, non-leaf, recursive. You'll put the calling convention and frame layout to work in real, reusable code.