Skip to main content
Be Bitwise

Stack Push & Pop

Push values onto the stack and pop them off. See how the stack pointer moves and LIFO ordering works.

The stack is a region of memory that grows downward from high addresses. PUSH decrements the stack pointer (SP) and writes a value at the new SP address. POP reads the value at SP and increments it. Because the last value pushed is the first one popped, the stack follows Last-In-First-Out (LIFO) order. This mechanism underpins function calls, local variables, and saved registers in every compiled program.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Load three values

    Do this: Step through all three MOV instructions

    Watch for: R0 = 10, R1 = 20, R2 = 30

    We prepare three values in registers. The stack is empty and SP starts at 0xFF (255), the top of our 256-byte memory.

  2. 2

    Push the first value

    Do this: Step through PUSH R0

    Watch for: SP decrements from 0xFF to 0xFE. Value 10 appears in the memory grid at address 0xFE

    PUSH first decrements SP, then writes the register value to the new SP address. The stack 'grows' downward — toward lower addresses.

  3. 3

    Push two more values

    Do this: Step through PUSH R1 and PUSH R2

    Watch for: SP decrements to 0xFD then 0xFC. Values 20 and 30 appear in memory

    Each PUSH decrements SP by 1 and writes to the new address. The stack now holds three values: 10 at 0xFE, 20 at 0xFD, 30 at 0xFC.

  4. 4

    Pop values in LIFO order

    Do this: Step through all three POP instructions

    Watch for: R3 = 30, R4 = 20, R5 = 10. SP increments back to 0xFF

    POP reads the value at the current SP address, stores it in the target register, then increments SP. The last value pushed (30) is the first value popped — this is LIFO ordering. The values are read in reverse order: 30, 20, 10.

Objectives

  • •Watch SP decrement from 0xFF as values are pushed
  • •See values appear in the memory grid near address 0xFF
  • •Notice LIFO order: last pushed (30) is first popped
  • •R3=30, R4=20, R5=10 after all pops

Hints

  • Watch the SP register and the memory near the top of the address space