Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. stack
4 min read

Stack & Calls

The stack is a region of memory every program uses as scratch space — for saving registers, holding local variables, and, above all, remembering where to return when a function call finishes. It is the single most important structure in this course for what comes later: the Binary Exploitation track lives almost entirely here, because controlling the stack means controlling where a program goes next.

The Stack and rsp

The stack grows downward — toward lower addresses — as shown in the Memory map. The rsp register always points at the top: the most recently added item. Two instructions do the work, and each adjusts rsp for you:

nasm
1push rax        ; rsp -= 8, then store rax at [rsp]
2pop  rbx        ; load [rsp] into rbx, then rsp += 8

It is last-in, first-out: the value you push most recently is the one the next pop returns. Because the stack grows down, a push subtracts 8 from rsp before writing, and a pop adds 8 after reading.

The Call Stack

A call is where the stack earns its name. It does two things in one instruction: it pushes the return address — the address of the instruction right after the call — onto the stack, then jumps to the target. When the function finishes, ret pops that address back into rip, so execution resumes exactly where it left off. That saved address is the only thing that tells the CPU how to get home. Watch it happen — a value is pushed, then a function is called, and we inspect the top of the stack each time:

gdb
gef➤ break _startBreakpoint 1 at 0x401000gef➤ runBreakpoint 1, 0x0000000000401000 in _start ()gef➤ p $rsp$1 = (void *) 0x7fffffffe2f0gef➤ si0x0000000000401005 in _start ()gef➤ si0x0000000000401006 in _start ()gef➤ p $rsp$2 = (void *) 0x7fffffffe2e8gef➤ x/gx $rsp0x7fffffffe2e8: 0x00000000deadbeefgef➤ break *0x401017Breakpoint 2 at 0x401017gef➤ continue

The push dropped rsp by 8 (…e2f0 → …e2e8) and left 0xdeadbeef sitting on top. Then call ran, and inside func the top of the stack holds 0x40100b — which, checking the disassembly, is exactly the instruction right after the call. That value on the stack is the return address, and the fact that it lives in writable memory next to your data is precisely what a stack buffer overflow abuses: overwrite it, and ret sends the program wherever you choose.

Stack Frames

A function needs room for its own local variables, and it gets it by carving out a stack frame. The conventional pattern wraps the body in a prologue and epilogue:

nasm
1push rbp            ; save the caller's frame pointer
2mov  rbp, rsp       ; rbp now anchors this frame
3sub  rsp, 0x20      ; reserve 32 bytes for locals
4

rbp is set once and left alone, so every local sits at a fixed offset from it ([rbp-8], [rbp-0x10]) even as rsp keeps moving when the body pushes and pops. leave is the one-instruction shorthand that tears the frame back down before ret. Modern compilers often optimize the frame pointer away and address locals straight off rsp, but you will meet this prologue constantly in disassembly, so learn to recognize it.

The Calling Convention

For calls to work across code written by different people, everyone must agree on where arguments go. Linux uses the System V AMD64 ABI: the first six integer or pointer arguments travel in registers, in this exact order, and anything beyond the sixth is pushed onto the stack (right to left):

Argument1234567+
Registerrdirsirdxrcxr8r9the stack

The return value comes back in rax. This is the same convention behind the caller/callee-saved split in Registers — and the same register order the kernel reuses, with one change, for system calls.

This is the lesson binary exploitation leans on most. The return address on the stack, the frame layout, and the argument registers are the raw material of nearly every stack-based attack.
Breakpoint 2, 0x0000000000401017 in func ()
gef➤ x/gx $rsp
0x7fffffffe2e0: 0x000000000040100b
; ... body: locals live at [rbp-8], [rbp-0x10], ...
5leave ; undo it: mov rsp, rbp ; pop rbp
6ret