The stack

PUSH and POP

PUSH stores a value at the top of the stack and moves RSP down by 8. POP loads the top value and moves RSP up by 8. The stack grows downward on x86:

push rax ; store rax, rsp = rsp - 8 push rbx pop rcx ; rcx = rbx, rsp = rsp + 8 pop rdx ; rdx = rax

Stack layout

RSP points at the top (lowest address) of the stack. RBP conventionally points at the base of the current stack frame, so locals and saved registers sit at fixed offsets from RBP while RSP moves:

push rbp mov rbp, rsp sub rsp, 16 ; 16 bytes of locals mov [rbp-8], rax ; local variable

Alignment

Keep RSP 16-byte aligned at call boundaries on Linux and macOS: the ABI expects it. Push an even number of registers, or pad with a dummy push, before any CALL.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How far does RSP move when PUSH stores a 64-bit register?

    Answer

    Down by 8

    A quadword is eight bytes, and the stack grows toward lower addresses.

  2. Which direction does the x86 stack grow?

    Answer

    Toward lower addresses

    PUSH subtracts from RSP and writes at the new address.

  3. What does POP load?

    Answer

    The value at the top of the stack

    POP reads the quadword at RSP and then raises RSP by eight.

  4. What does RBP usually point at?

    Answer

    The base of the current stack frame

    A frame keeps RBP fixed so locals have steady offsets while RSP moves.

  5. What is the stack alignment at a call boundary on Linux x86-64?

    Answer

    16 bytes

    The System V ABI expects RSP to be 16-byte aligned before a call.

  6. What does this program print? default rel section .data buf db " ", 10 section .text global _start _start: mov rax, "A" push rax mov rax, "B" push rax pop rax mov [buf], al pop rax mov [buf + 1], al mov rax, 1 mov rdi, 1 mov rsi, buf mov rdx, 2 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    BA

    The last value pushed is the first popped, so B lands before A.

  7. What exit status does this program return? section .text global _start _start: mov rax, 7 push rax pop rdi mov rax, 60 syscall

    Answer

    7

    The pushed value comes back into rdi, and rdi is the exit status.

  8. What does sub rsp, 16 do in a procedure frame?

    Answer

    It reserves 16 bytes for locals

    Moving RSP down makes room below the saved frame pointer.

  9. Why push an even number of registers before a CALL?

    Answer

    To keep RSP 16-byte aligned

    Each push moves RSP by 8, so two pushes keep the 16-byte alignment.

  10. What does mov [rbp - 8], rax address?

    Answer

    A local variable in the frame

    Negative offsets from RBP reach the locals a frame reserved.

The snippets on this page are fragments, not whole programs: they show the stack operations and the frame layout. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev