← again tap: answer good → ↑ swipe up at the end: next
Question
What do push and pop actually do internally?
Answer
push subtracts 8 from %rsp then writes the value there; pop reads from %rsp then adds 8 back.
* push = rsp minus 8 then store; pop = load then rsp plus 8, because the stack grows toward lower addresses. *
push %rbx
is equivalent to:
sub $8, %rsp
mov %rbx, (%rsp)
pop %rbx
is equivalent to:
mov (%rsp), %rbx
add $8, %rsp
Key detail: The stack grows downward (toward lower addresses). So push subtracts and pop adds.
On x86_64, push/pop always move 8 bytes. On 32-bit x86, they move 4 bytes.
Go deeper:
PUSH instruction reference — confirms PUSH decrements the stack pointer then stores the operand at the top of the stack.
Call stack (Wikipedia) — how push/pop build the call stack and why growth direction is architecture-dependent.
Stack frame layout on x86-64 (Eli Bendersky) — shows where pushed values sit relative to rsp within a real frame.
Question
What does call and ret actually do internally?
Answer
call pushes the address of the next instruction onto the stack and jumps to the target; ret pops that address and jumps back to it.
* call ≡ push return-addr ; jmp target (%rsp −= 8). ret ≡ pop → %rip ; %rsp += 8. The value at (%rsp) on entry to a callee is its return address. *
call <function>
is equivalent to:
push %rip
jmp <function>
(Pushes the address of the instruction after call, then jumps)
ret
is equivalent to:
pop %rip
(Pops the return address from the stack into the program counter)
Key consequence: After call, %rsp is 8 bytes lower. The value at (%rsp) inside the called function is the return address.
Note: This is why functions that call other functions must keep the stack 16-byte aligned — call pushes 8 bytes, so the function prologue often does sub $8, %rsp or push %rbx to re-align.
Go deeper:
CALL instruction (felixcloutier) — Confirms call saves procedure-linking info (the return address) on the stack and branches to the target.
RET instruction (felixcloutier) — Confirms ret transfers control to the return address at the top of the stack.
Call stack (Wikipedia) — Big-picture view of how return addresses on the stack let nested calls resume correctly.