Assembly & the CPU

the stack at the instruction level (push/pop, call/ret)

When you call a function and it later returns to exactly where you left off, something must remember 'where you left off'. The stack at the instruction level is that memory. It is a region of process memory used as a last-in-first-out scratchpad, with the stack pointer (rsp) marking its top, and it is the machinery that makes calling and returning from functions work.

Two instruction pairs run it. push value subtracts from rsp and writes the value at the new top; pop dest reads the top value and adds back to rsp — together they save and restore values around code that would otherwise clobber a register. The call/return mechanism builds on this: call target pushes the address of the instruction right after the call (the return address) onto the stack and then jumps to target; ret pops that return address off the stack into the program counter, so execution resumes exactly where it left off. Because each call pushes its own return address, nested and recursive calls just nest on the stack, unwinding in reverse order on each ret.

This is why the call stack works and why it has a depth limit. Every active function call has its return address (and usually its locals) sitting on the stack; the deepest call is on top, the outermost at the bottom. Infinite recursion keeps pushing without ever popping until the stack runs out of room — a stack overflow. It is also why a corrupted return address is so dangerous: overwrite the saved return address on the stack (for example via a buffer overflow) and ret will jump wherever the corrupted value points, the classic mechanism behind stack-smashing attacks.

call f pushes the return address and jumps into f; when f finishes, its ret pops that address back into rip and execution continues at the instruction after the original call.

call saves a return address on the stack; ret reads it back — that is the entire call/return mechanism.

This is the stack memory region behaving like a LIFO, not the abstract stack data structure itself — and on x86-64 it grows downward, so push lowers rsp. The return address lives on the stack as ordinary writable memory, which is precisely why overwriting it is the heart of stack-smashing exploits.

Also called
the call stack at the machine levelcall/return mechanism返回機制