Chapter 4 of “Malware Analysis Crash Course” (FLARE/Mandiant) ties together what we learned in Chapter 2 (The Stack) and Chapter 3 (CALL/RET) into a complete picture: how a function organizes its own parameters and local variables.
What Is a Stack Frame
A stack frame is a region of memory allocated specifically for one particular function call, holding all the data for that call. When a function calls another function, a new frame is built on top of the caller’s frame. The frame exists for as long as the function runs and is wiped clean the moment the function returns.
The standard layout of a frame (from high address to low):
- Function parameters — pushed by the caller before
CALL - Return address — automatically pushed by
CALL - Saved old EBP — pushed by the callee itself to preserve the caller’s EBP
- Local variables — space the callee allocates for its own use
Prologue and Epilogue
Prologue — always the first instructions of a function, building the new frame:
push ebp ; save the caller's EBP
mov ebp, esp ; new EBP = current top of stack -> a fixed anchor point
sub esp, N ; allocate N bytes for local variablesThis turns EBP (which stays fixed throughout the function body) into a reliable anchor point, while ESP keeps shifting with every PUSH/POP inside the function.
Epilogue — the reverse, cleaning up the frame before returning:
mov esp, ebp ; release local variables, restore ESP to the correct position
pop ebp ; restore the caller's EBP
ret [n] ; pop the return address, jump back, optionally cleaning up n extra bytesSpotting the exact pair push ebp / mov ebp, esp at the start and pop ebp / ret at the end of a block is essentially a guaranteed function boundary — IDA uses exactly this pattern to automatically determine where functions start and end.
Function Arguments and Local Variables
Since EBP stays fixed right after the prologue, offsets relative to it remain stable throughout the function body:
- Parameters sit at positive offsets: because they’re pushed before
CALL, they end up “above” both the return address and the saved EBP on the stack. The return address sits at[ebp+4], so the first parameter is at[ebp+8], the second at[ebp+0xC], and so on in increments of 4. - Local variables sit at negative offsets: because they’re allocated after the frame is built (via
sub esp, N), they sit “below” EBP. If a function allocates 8 bytes, the first variable is at[ebp-4], the second at[ebp-8].
A quick mnemonic: positive = things that already existed before the function started (parameters, supplied by the caller, read-only to the callee), negative = things the function creates for itself.
Calling Conventions: STDCALL and CDECL
Both share a few common rules: parameters are pushed right-to-left, the return value always sits in EAX, and the prologue/epilogue structure is identical. The one — but critically important — difference is who is responsible for cleaning up the stack after the call:
| STDCALL | CDECL | |
|---|---|---|
| Who cleans up | Callee cleans up itself | Caller cleans up itself |
| How | RET n (see Chapter 3) — pops the return address and adds n bytes itself | Plain RET, caller must ADD ESP, n right after CALL |
| Commonly seen in | Windows API (WINAPI) | Standard C functions, especially variadic ones (printf) |
; STDCALL
ret 8 ; callee pops the return address AND adds 8 itself (cleaning up 2 4-byte parameters)
; CDECL
call sub_401000
add esp, 8 ; caller cleans up right after the callWhy CDECL is mandatory for a variadic function like printf: at compile time, the callee has no way of knowing in advance how many parameters it will receive, so it can’t RET n with a fixed n — only the caller (which knows exactly how many it pushed) can clean up correctly.
Local Variables and Global Variables
- Local variables: live on the stack frame, accessed via
[ebp - offset], with a lifetime scoped to a single function call — the epilogue wipes them clean (mov esp, ebp) the moment the function returns, effectively making them “vanish”. - Global variables: live at a fixed location in the binary’s
.datasection, persist for the entire program’s lifetime, independent of any particular function call. Accessed via an absolute address rather than an offset from EBP — assemblers/disassemblers usually auto-assign readable labels for these addresses (e.g.dword_40CF60).
mov eax, [ebp - 4] ; read a local variable
mov ecx, dword_40CF60 ; read a global variable (fixed address, labeled)Telling these two apart helps you quickly guess their meaning: global variables usually hold state that needs to persist across the whole program (config, status flags), while local variables usually just serve temporary logic within a single function call.
Mastering stack frames + calling conventions makes Chapter 5 (Conditional Branching) and everything after it much easier to read — because from here on, seeing [ebp+8] immediately tells you you’re looking at a function’s first parameter.