COA Notes
Stack-based CPU organization, stack operations, register stack vs memory stack, and stack applications.
Introduction
A stack is a Last-In-First-Out (LIFO) data structure that plays a crucial role in computer organization. Whether it's a hardware register stack inside the CPU or a memory-based stack managed by the stack pointer, stacks are everywhere — handling function calls, evaluating expressions, managing interrupts, and even forming the basis of entire CPU architectures. Understanding stack organization helps you see how programs actually execute at the hardware level.
What is a Stack?
A stack is a linear list where insertions and deletions happen at one end only (the "top"):
- PUSH: Add an item to the top of the stack
- POP: Remove the item from the top of the stack
| PUSH 'A' | [A] (stack has 1 item) |
| PUSH 'B' | [A][B] (stack has 2 items, B on top) |
| PUSH 'C' | [A][B][C] (stack has 3 items, C on top) |
| POP | [A][B] (C removed, B is new top) |
| POP | [A] (B removed, A is new top) |
Register Stack
A register stack is implemented entirely in hardware using a fixed set of registers:
PUSH Operation
POP Operation
Characteristics
- Fixed size (determined at design time)
- Very fast (register access speed)
- Limited capacity (typically 8-64 entries)
- Used for expression evaluation in some architectures
Memory Stack
A memory stack uses a region of main memory, with a register (SP) pointing to the current top:
Convention: Stack Grows Downward
In most architectures (x86, ARM, MIPS), the stack grows toward LOWER addresses:
- PUSH: SP ← SP - 1; M[SP] ← Data (decrement then store)
- POP: Data ← M[SP]; SP ← SP + 1 (read then increment)
Advantages over Register Stack
- Virtually unlimited size (constrained only by available memory)
- Flexible (can grow as needed)
- Supports deep recursion
Disadvantages
- Slower (memory access instead of register access)
- Requires memory bus bandwidth
Stack Applications in CPU Organization
1. Function Calls and Returns
When a function is called:
CALL function_address:
PUSH PC ; Save return address on stack
PC ← function_address ; Jump to function
RETURN:
POP PC ; Restore return address
; Execution continues after the CALL2. Stack Frame (Activation Record)
Each function call creates a stack frame containing:
3. Expression Evaluation (Postfix/RPN)
Stack machines evaluate expressions using postfix notation:
Infix: (3 + 4) × (5 - 2) Postfix: 3 4 + 5 2 - ×
Evaluation:
4. Interrupt Handling
When an interrupt occurs:
5. Stack-Based Architectures
Some processors use a stack as the primary computation model:
- Java Virtual Machine (JVM): Operands pushed, operations pop and push results
- Forth language: Designed around explicit stack manipulation
- HP calculators: Used Reverse Polish Notation (RPN) with hardware stack
Stack Operations in Assembly
x86 Stack Instructions
Key Takeaways
- A stack is a LIFO structure accessed only at the top via PUSH and POP
- Register stacks are fast but small (hardware registers); memory stacks are slow but large
- Most architectures grow the stack downward (toward lower addresses)
- Stacks are essential for function calls (saving return addresses and local variables)
- Stack frames organize parameters, return address, saved registers, and locals
- Expression evaluation uses postfix notation with a stack for operands
- Interrupt handling uses the stack to save and restore processor state
- Stack-based architectures (JVM, Forth) use the stack as the primary computation model
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Stack Organization.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, cpu, stack, stack organization
Related Computer Organization & Architecture Topics