Course contentsShow
← Computer Science
Lesson 1102 of 2,87225. Computer Architecture FoundationsFree lesson

The Stack: PUSH and POP Operations

Understanding the stack data structure and using PUSH/POP for temporary data storage.

The Stack: PUSH and POP Operations

What you'll learn: How the stack data structure works in assembly language and how PUSH and POP instructions manage temporary data.


What is the Stack?

The stack is a special region of memory that operates like a stack of plates: you can only add or remove items from the top. In assembly programming, the stack is critical for temporarily storing register values, passing function parameters, and managing local variables.

The CPU maintains a stack pointer (SP) register that always points to the top of the stack. As you add or remove data, the stack pointer automatically adjusts.

PUSH Operation

The PUSH instruction adds data to the top of the stack:

PUSH AX    ; Save the value in register AX onto the stack

What happens internally:

  1. The stack pointer decrements (moves to a lower memory address)
  2. The value from AX is written to the new stack top

Analogy: Like adding a plate to the top of a stack — the stack grows taller (though in memory, it typically grows "downward" toward lower addresses).

POP Operation

The POP instruction removes data from the top of the stack and loads it into a register:

POP AX     ; Restore the value from the stack into register AX

What happens internally:

  1. The value at the stack top is read into AX
  2. The stack pointer increments (moves to a higher memory address)

Common Use Case: Preserving register values before using them for other operations, then restoring them afterward:

PUSH AX    ; Save original value
MOV AX, 5  ; Use AX for something else
ADD AX, 3
POP AX     ; Restore original value

Key Takeaway: The stack uses PUSH to save data temporarily and POP to retrieve it in Last-In-First-Out (LIFO) order, essential for managing register state in assembly programs.