Quiz Entry - updated: 2026.07.10
What is the function prologue and epilogue in x86-64?
The prologue saves the old frame pointer and any callee-saved registers and reserves local space; the epilogue restores them in reverse and returns — leaving the caller's state untouched.
This bookkeeping is what lets functions nest cleanly: each one promises to give the stack and callee-saved registers back exactly as it found them.
Standard prologue (with frame pointer):
pushq %rbp # 1. save caller's frame pointer
movq %rsp, %rbp # 2. establish this frame's base
subq $N, %rsp # 3. reserve space for locals
pushq %rbx # 4. save any callee-saved regs used
Standard epilogue — undo it in reverse order:
popq %rbx # restore callee-saved regs
movq %rbp, %rsp # release locals
popq %rbp # restore caller's frame pointer
ret
The leave instruction is a one-byte shortcut for the two middle steps — it's exactly movq %rbp,%rsp; popq %rbp:
leave
ret
Optimized variant: when no frame pointer is needed, the compiler drops the %rbp save entirely and brackets the body with just subq $N,%rsp … addq $N,%rsp.
Go deeper:
Function prologue and epilogue (Wikipedia) — describes the prologue/epilogue bookkeeping directly.
Felix Cloutier — LEAVE — LEAVE as the one-instruction epilogue shortcut.