Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

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.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place inputs D3 to D0, PUSH, POP, CLK and CLR, and probes TOP3 to TOP0, SP2 to SP0, EMPTY and FULL.
  2. 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.
  3. Build four 4-bit registers on D, each loaded on a push when SP's low two bits select it.
  4. 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

OperationBefore (SP)After (SP)TOP after
push 0011010011
push 1010121010
push 0110230110
pop321010
pop210011
pop10(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

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adder4-bit Kogge-Stone adder4-bit carry-skip adder4x4 Wallace tree multiplier8-bit adderfrequency divider (by 2, 4 and 3)switch debouncer (SR latch)3-bit Gray code counterMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)8-bit ALUdecimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates4-bit LFSR74LS138 decoderBCD subtractor74181 ALUCMOS AOI gate (AOI21)D latch vs D flip-flop74LS47 BCD to 7-segment decoder74HC595 shift register7400 quad NAND gate chip7402 quad NOR gate chip7404 hex inverter chip7408 quad AND gate chip7432 quad OR gate chip7486 quad XOR gate chip7474 dual D flip-flop74HC165 shift registerCD4017 decade counter74LS283 4-bit adder74LS90 decade counter74HC161 4-bit counter74HC151 8-to-1 multiplexer74HC157 quad 2-to-1 multiplexer74HC139 dual 2-to-4 decoderCD4511 BCD to 7-segment decoder7492 divide-by-12 counter7493 4-bit binary counter74HC244 octal bufferRace-around condition (JK latch)74HC373 octal latch74HC374 octal D flip-flopTurnstile state machine1-to-2 demultiplexerhalf adder using 2:1 multiplexersregister file (4 x 4-bit)digital PWM generatorCD4013 dual D flip-flop74LS76 dual JK flip-flop7410 triple 3-input NAND chip7411 triple 3-input AND chip7420 dual 4-input NAND chipCD4011 quad 2-input NAND chipCD4001 quad 2-input NOR chipCD4081 quad 2-input AND chipCD4069 hex inverter chip74HC164 8-bit shift register7485 4-bit magnitude comparator74HC125 quad tri-state bufferCD4040 12-stage binary counterCD4060 14-stage counter and oscillator74LS193 4-bit up/down counterelectronic dice circuitmemory address register (MAR) and MDRdirect-mapped cache8-bit Brent-Kung adder4x4 Dadda multiplierbinary clock circuit

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.