Hardware stack and stack pointer: push and pop, live
A stack keeps values last in, first out: PUSH puts a value on top, POP takes the top one off. In hardware it is a small memory plus a stack pointer (SP), a counter that says where the next value goes. This live stack holds four 4-bit values: PUSH writes D3 to D0 at SP and adds 1, POP subtracts 1, and TOP always shows the value on top.
Here SP counts the values on the stack, 0 to 4. A push stores the data in the word SP points at and moves SP up by one in the same clock edge; a pop just moves SP down, because the old value can stay where it is until a later push overwrites it. The top of the stack is therefore the word at SP - 1, and the circuit reads it all the time through 4:1 multiplexers, so TOP changes as soon as SP does. Two flags guard the ends: EMPTY (SP = 0) blocks a pop and FULL (SP = 4) blocks a push, which in a CPU would be a stack underflow or overflow, and PUSH and POP pressed together do nothing. The memory is four 4-bit registers with load, written through a 2-to-4 decoder as in the register file; SP is a 3-bit up/down counter of D flip-flops with clear, counting up on a push and down on a pop. CPUs use the same idea for the call stack, where a CALL pushes the return address and a RET pops it, though many let the stack grow downward from the top of memory and keep it in RAM rather than in registers. A stack machine, such as the Java virtual machine's instruction set, goes further and takes every operand from the top of a stack like this one. The live circuit opens with CLR high (SP = 0, EMPTY lit), PUSH on and D = 0101: set CLR to 0 and turn CLK on and off to push 0101. Before the page is built, a 44-step sequence is checked on every output: pushes and pops, filling the stack (a fifth push is ignored), emptying it (an extra pop is ignored), and PUSH with POP at once.
Open this circuit in the lab →
What you need
- 4× 4-bit register with load: the four stack words
- 3× D flip-flop with clear: the stack pointer SP2 SP1 SP0, cleared by CLR
- 2× AND + 2 XNOR + 3 XOR + 1 OR: SP + 1 on a push and SP - 1 on a pop
- 4× AND + 1 NOR + 1 OR + 4 NOT: EMPTY (SP = 0) and FULL (SP = 4), and blocking a push when full, a pop when empty, or both at once
- 8× AND + 2 NOT: the write enables: push AND (SP selects this word)
- 4× 4:1 multiplexer + 1 XNOR: TOP: the word at SP - 1 (address bits NOT SP0 and SP1 XNOR SP0)
Step by step
- Place inputs
D3toD0,PUSH,POP,CLKandCLR, and probesTOP3toTOP0,SP2toSP0,EMPTYandFULL. - Build SP as a 3-bit counter that adds 1 on a push and subtracts 1 on a pop, with push blocked while SP = 4 and pop while SP = 0.
- Build four 4-bit registers on D, each loaded on a push when SP's low two bits select it.
- Read the word at SP - 1 through four 4:1 multiplexers onto TOP. Push three values, then pop: TOP shows them in reverse order.
Push and pop, step by step
| Operation | Before (SP) | After (SP) | TOP after |
|---|---|---|---|
| push 0011 | 0 | 1 | 0011 |
| push 1010 | 1 | 2 | 1010 |
| push 0110 | 2 | 3 | 0110 |
| pop | 3 | 2 | 1010 |
| pop | 2 | 1 | 0011 |
| pop | 1 | 0 | (empty) |
SP counts values, so the top value sits at word SP - 1. A pop does not erase the old value; the next push simply writes over it. TOP is meaningless while EMPTY is lit.
You can also read the full theory in the lesson on register files. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
Build more parts from gates
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the hardware stack with a stack pointer in the lab →Frequently asked
What is a stack pointer?
A register that holds the position of the top of a stack in memory. Pushing a value writes it at the stack pointer and moves the pointer one place; popping moves it back, so the most recent value is always the next one out.
How do push and pop work in hardware?
A push writes the data into the memory word the stack pointer selects and changes the pointer by one in the same clock cycle; a pop changes the pointer back. The top value is read from the word next to the pointer.
What happens on a stack overflow or underflow?
Overflow is a push when the stack is full, underflow a pop when it is empty. This circuit simply ignores them and lights FULL or EMPTY; a CPU would usually raise an exception or, without checks, overwrite other memory.
What is a stack machine?
A computer or virtual machine whose instructions take their operands from the top of a stack and push their results back, instead of naming registers. ADD pops two values and pushes their sum. The Java virtual machine and RPN calculators work this way.