Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Assembly and Reverse Engineering Basics

The Stack and Calling Conventions

Understand the stack, stack frames, cdecl, stdcall and the x64 Windows convention so you can find function arguments and return values in disassembly.

The most useful question you can ask of a function in a disassembler is "what are its arguments?" The answer depends on the calling convention: an agreement between caller and callee about where arguments live, who cleans up, and where the result goes. Once you know the convention, a pile of push and mov lines becomes a readable call such as CreateFileW(path, access, ...).

Everything here is explained with made-up listings. The runnable stand-ins model a stack as a Python list and show how arguments are pushed and read back; they are teaching models, not real memory.

What a call needs

Arguments Where does the caller put them?
Return address Where does the callee go back to?
Result Which register holds the answer?
Cleanup Who removes the arguments?

The convention answers all four.

1 / 2

The stack

The stack is a region of memory used for temporary data: return addresses, arguments and local variables. It grows downward on x86: push decreases esp and writes a value; pop reads a value and increases esp. call target pushes the address of the next instruction (the return address) and jumps; ret pops that address into eip.

TEXT
push  0x20         ; second argument
push  0x10         ; first argument
call  add_numbers  ; pushes the return address, jumps
add   esp, 8       ; caller removes both arguments (cdecl)

Arguments are pushed right to left, so the first argument ends up closest to the top of the stack. Inside the callee, with a classic frame, they sit at fixed offsets from ebp.

The stack frame

A function that needs local variables builds a frame with a standard prologue and epilogue:

TEXT
add_numbers:
push  ebp            ; save the caller's frame pointer
mov   ebp, esp       ; our frame starts here
sub   esp, 8         ; reserve 8 bytes for local variables
mov   eax, [ebp+8]   ; first argument
add   eax, [ebp+12]  ; second argument
mov   esp, ebp       ; release locals
pop   ebp            ; restore the caller's frame pointer
ret                  ; pop the return address and go back

The offsets are the key: [ebp+4] is the return address, [ebp+8] the first argument, [ebp+12] the second, and [ebp-4], [ebp-8] are locals. Disassemblers recognise the pattern and rename these as arg_0, var_4 and so on, which is the first thing you improve by renaming them to meaningful names.

Compilers sometimes omit the frame pointer in optimised code and address everything through esp; then offsets shift as the function pushes and pops, and the disassembler's stack-depth tracking does the bookkeeping for you.

Conventions on 32-bit Windows

Convention Arguments Cleanup Typical use
cdecl stack, right to left caller (add esp, N) C functions, varargs like printf
stdcall stack, right to left callee (ret N) most Windows API functions
fastcall first two in ecx, edx, rest on stack callee some compiler-specific code
thiscall this in ecx, rest on stack callee C++ member functions

The cleanup rule gives you a quick clue. If a function ends with ret 8, it takes two 4-byte stack arguments and cleans them itself, so it is probably stdcall. If a caller runs add esp, 8 right after a call, the callee was cdecl. In every case the return value comes back in eax (and edx:eax for 64-bit values).

The 64-bit Windows convention

x64 Windows uses one convention: the first four integer or pointer arguments travel in rcx, rdx, r8 and r9; the rest go on the stack. The caller also reserves 32 bytes of "shadow space" above the return address for the callee to spill those four registers. The return value is in rax. Floating-point arguments use xmm0 to xmm3.

TEXT
lea   rdx, [rip+0x2f10]   ; second arg: address of a string
xor   ecx, ecx            ; first arg: 0
xor   r8d, r8d            ; third arg: 0
xor   r9d, r9d            ; fourth arg: 0
call  qword ptr [rip+0x1c40]   ; call an imported function

This is why x64 code has fewer push instructions before calls: you read the setup of rcx, rdx, r8 and r9 instead. Linux x86-64 uses a different register order (rdi, rsi, rdx, rcx, r8, r9), so check which operating system the sample targets.

Modelling a call

The first stand-in simulates the add_numbers call above using a list as the stack. Index 0 is the bottom; the end of the list is the top. It prints the stack at three moments.

Python Push arguments, call, read them back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The saved frame pointer sits at the top; the return address is one slot below it, then the first and second arguments. That ordering, reading outward from the frame pointer, is exactly the [ebp+4], [ebp+8], [ebp+12] pattern.

Recovering arguments from a call site

When you only have the caller's listing, you rebuild the call by collecting the pushes (or the register setup) before the call. The second stand-in does this for a tiny read-only listing made of plain strings, converting push lines into a call description.

Python Rebuild a call from its setup instructions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading calls this way turns a listing of 15 instructions into one line of intent. Many analysis tools do it automatically once a function's prototype is known, which is why importing type information for the Windows API saves so much time.

Whenever you see call followed by add esp, N or ret N, write the number of arguments next to the function name in your notes. It catches mistakes early when two functions look alike.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a classic 32-bit frame, where is the first stack argument?

    [ebp+4] holds the return address, so the first argument starts at [ebp+8].
  2. 2. A function ends with ret 8. What does this suggest?

    ret N removes N bytes of arguments, which is typical of stdcall.
  3. 3. Which registers carry the first two integer arguments in the x64 Windows convention?

    x64 Windows uses rcx, rdx, r8 and r9; rdi and rsi are used on Linux.
  4. 4. What does the call instruction push onto the stack?

    The return address is pushed so that ret knows where to resume.

Summary

  • The stack holds return addresses, arguments and locals, and grows downward.
  • A classic frame uses push ebp; mov ebp, esp; arguments are at [ebp+8] and up, locals below.
  • cdecl is cleaned by the caller, stdcall by the callee (ret N); return values go in eax.
  • x64 Windows passes the first four arguments in rcx, rdx, r8, r9.
  • Rebuilding calls from setup instructions turns assembly into readable intent.
  • The Python models are simplified stand-ins; real stacks live in process memory.

Next: Control Flow in Disassembly