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.
rspThe 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:
1push rax ; rsp -= 8, then store rax at [rsp]
2pop rbx ; load [rsp] into rbx, then rsp += 8It 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.
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:
break _startBreakpoint 1 at 0x401000runBreakpoint 1, 0x0000000000401000 in _start ()p $rsp$1 = (void *) 0x7fffffffe2f0si0x0000000000401005 in _start ()si0x0000000000401006 in _start ()p $rsp$2 = (void *) 0x7fffffffe2e8x/gx $rsp0x7fffffffe2e8: 0x00000000deadbeefbreak *0x401017Breakpoint 2 at 0x401017continueThe 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.
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:
1push rbp ; save the caller's frame pointer
2mov rbp, rsp ; rbp now anchors this frame
3sub rsp, 0x20 ; reserve 32 bytes for locals
4rbp 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.
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):
| Argument | 1 | 2 | 3 | 4 | 5 | 6 | 7+ |
|---|---|---|---|---|---|---|---|
| Register | rdi | rsi | rdx | rcx | r8 | r9 | the 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.