Think about how you cook in a small kitchen. You could keep everything in one bowl: every time you mix something new, you first empty the bowl somewhere else. You could have many bowls on the counter, so several half-done items stay within reach. Or you could stack plates: you always work on the top plate, and when you finish it, the plate below becomes the new top.
A CPU faces the same question: where do the operands of an operation live while the processor works on them? The three classic answers are exactly these three kitchens: a single accumulator, a set of general-purpose registers, and a stack.
CPU organization describes how the processor's registers are arranged and how the ALU gets its operands and stores its result. The three basic organizations are single-accumulator, general-register and stack organization.
The choice is not just a hardware detail. It decides how many addresses an instruction needs, how long programs are, how often memory is touched, and how easy the machine is to build. In this chapter we will look at all three organizations, encode real control words for a general-register CPU, trace PUSH and POP on a register stack, evaluate expressions in reverse Polish notation, and compare all three on the same expression. Every count and trace in this chapter was checked with a small simulator.
Why the Organization Matters
An instruction like "add" needs three things: two source operands and a place to put the result. The question is how many of these the instruction must name explicitly.
Accumulator: one operand and the result are always in a special register called the accumulator (AC). The instruction names only the other operand. These are one-address instructions.
General register: operands are in any of several registers. The instruction names two or three registers (or a register and a memory address). These are two-address or three-address instructions.
Stack: operands are always the top items of a stack, and the result goes back on top. Arithmetic instructions name nothing. These are zero-address instructions.
So the organization and the instruction format go hand in hand. The format itself (fields, opcode size, address bits) is covered in Instruction Format & Instruction Cycle; here we focus on the hardware behind each style.
1. Single Accumulator Organization
This is the oldest and simplest organization. Early machines such as the EDSAC and many 8-bit microprocessors were built around it.
In a single-accumulator organization, one register (the accumulator, AC) is the implied source of one operand and the implied destination of every ALU result. The other operand comes from memory (or an immediate value).
+-----------------+
memory --->| DR (data reg) |----+
+-----------------+ |
v
+---------+
+------->| ALU |
| +---------+
| |
| v
| +---------+
+--------| AC |----> to memory (STORE)
+---------+
The ALU always combines AC with the incoming operand,
and the result always goes back into AC.Typical instructions:
Instruction | Meaning |
|---|---|
| AC ← M[X] |
| M[X] ← AC |
| AC ← AC + M[X] |
| AC ← AC − M[X] |
| AC ← AC × M[X] |
Besides AC, such a CPU still needs the usual support registers: the program counter (PC), instruction register (IR), an address register (AR) to hold the memory address, and a data register (DR) to hold the word coming from memory. Mano's "basic computer" is a classic textbook example of this design.
Example: X = (A + B) × (C + D)
LOAD A ; AC ← A
ADD B ; AC ← A + B
STORE T ; T ← AC (park the partial result in memory)
LOAD C ; AC ← C
ADD D ; AC ← C + D
MUL T ; AC ← (C + D) × T
STORE X ; X ← ACSeven instructions, and every one of them touches memory. Notice the STORE T: because there is only one accumulator, the first partial result has to be saved in a temporary memory location before we can compute the second one. That is the main weakness of this organization.
Pros and Cons
Pro: very simple hardware and short instructions (only one address field).
Pro: easy to design the control unit.
Con: AC is a bottleneck; intermediate results must be stored in memory.
Con: more instructions and many more memory accesses, and memory is much slower than registers.
2. General Register Organization
The obvious fix for the single-bowl problem is to have many bowls. If the CPU has several registers, intermediate results can stay inside the CPU instead of going back to memory. Register access takes a fraction of a clock cycle, while a memory access takes many cycles, so this is a big win.
In a general-register organization, the CPU contains a set of general-purpose registers, any of which can supply an ALU operand or receive the ALU result. Instructions specify registers explicitly, usually two or three of them.
Almost every modern processor uses this organization: RISC-V and MIPS have 32 integer registers, ARMv8 (AArch64) has 31 general-purpose registers, and x86-64 has 16.
The Bus System Inside the CPU
A standard textbook design (from Mano) has seven registers R1 to R7 connected to an ALU through two multiplexers:
R1 R2 ... R7 (+ external input) R1 R2 ... R7 (+ external input)
| | | | | |
+--v--v------v--+ +--v--v------v--+
| MUX A |<-- SELA (3 bits) | MUX B |<-- SELB (3 bits)
+-------+-------+ +-------+-------+
| A bus | B bus
+---------------+ +-------------------+
v v
+-------------+
OPR ------>| ALU | (5-bit operation code)
+------+------+
| output bus
v
+-------------+
SELD ----->| 3x8 decoder|---> load enable of R1..R7
+-------------+
The output bus goes to the inputs of all registers; only the one
enabled by the decoder actually loads the value at the clock edge.One clock cycle performs one micro-operation such as R1 ← R2 − R3:
MUX A puts R2 on the A bus (SELA = R2).
MUX B puts R3 on the B bus (SELB = R3).
The ALU performs A − B (OPR = SUB).
The decoder enables only R1's load input (SELD = R1), so R1 captures the result at the clock edge.
The Control Word
The four selection inputs together form a 14-bit control word:
bits: 3 3 3 5
+------+------+------+-----------+
| SELA | SELB | SELD | OPR |
+------+------+------+-----------+Register selection codes: 000 means "external input" for SELA/SELB and "no destination" for SELD; 001 to 111 select R1 to R7. The ALU operation codes in this textbook design are:
OPR | Symbol | Operation |
|---|---|---|
00000 | TSFA | Transfer A |
00001 | INCA | Increment A |
00010 | ADD | A + B |
00101 | SUB | A − B |
00110 | DECA | Decrement A |
01000 | AND | A AND B |
01010 | OR | A OR B |
01100 | XOR | A XOR B |
01110 | COMA | Complement A |
10000 | SHRA | Shift right A |
11000 | SHLA | Shift left A |
The exact OPR codes are a design choice of that book; another CPU would use different codes. What exams test is the method: pick each field and concatenate.
Worked Example: Encoding Control Words
Micro-operation | SELA | SELB | SELD | OPR | Control word |
|---|---|---|---|---|---|
R1 ← R2 − R3 | 010 | 011 | 001 | 00101 |
|
R5 ← R1 + R7 | 001 | 111 | 101 | 00010 |
|
R3 ← R3 − 1 | 011 | 000 (unused) | 011 | 00110 |
|
R2 ← 0 (R2 XOR R2) | 010 | 010 | 010 | 01100 |
|
R6 ← shl R4 | 100 | 000 (unused) | 110 | 11000 |
|
Two things to notice:
For unary operations (INCA, DECA, shifts, TSFA) SELB is a don't-care; it is conventionally written as 000.
Clearing a register with XOR works because x XOR x = 0 for every bit, so no "clear" operation is needed in the ALU.
Decoding works the same way in reverse. The control word 100 101 110 01000 splits into SELA = 100 (R4), SELB = 101 (R5), SELD = 110 (R6), OPR = 01000 (AND), so it performs R6 ← R4 AND R5.
Example: X = (A + B) × (C + D)
With three-address instructions, the registers hold the partial results:
ADD R1, A, B ; R1 ← M[A] + M[B]
ADD R2, C, D ; R2 ← M[C] + M[D]
MUL X, R1, R2 ; M[X] ← R1 × R2With two-address instructions (the first operand is both a source and the destination):
MOV R1, A ; R1 ← M[A]
ADD R1, B ; R1 ← R1 + M[B]
MOV R2, C ; R2 ← M[C]
ADD R2, D ; R2 ← R2 + M[D]
MUL R1, R2 ; R1 ← R1 × R2
MOV X, R1 ; M[X] ← R1In both versions there is no temporary memory variable: the second register replaces the STORE T that the accumulator machine needed.
Pros and Cons
Pro: intermediate results stay in fast registers, so fewer memory accesses.
Pro: very flexible; compilers can keep frequently used variables in registers (register allocation).
Con: instructions are longer because they must name registers (3 address fields of 5 bits each with 32 registers).
Con: more registers means more state to save on a function call or context switch.
3. Stack Organization
A stack is a last-in, first-out (LIFO) list. In a stack-organized CPU, the ALU always takes its operands from the top of the stack and pushes the result back on top. Because the operand locations are implied, arithmetic instructions need no address field at all.
In a stack organization, operands are held in a LIFO stack whose top is pointed to by a stack pointer (SP). PUSH and POP move data between memory and the stack; arithmetic instructions pop their operands from the top of the stack and push the result.
For example, ADD pops the top two items, adds them, and pushes the sum. For non-commutative operations the order matters: SUB computes (second item) − (top item), because the second item was pushed first.
A stack can be built in two ways: from a small set of dedicated registers (register stack) or in a region of main memory (memory stack).
Register Stack
Consider a register stack of 64 words. The stack pointer SP needs 6 bits (26 = 64), and two 1-bit flags record the state: FULL and EMTY (empty). Data is pushed from and popped into a data register DR.
In the classic textbook version, SP starts at 0 with EMTY = 1, and SP is incremented before a write. So the first item goes into word 1, the second into word 2, and so on. The 64th item goes into word 0, because the 6-bit SP wraps from 63 back to 0; that is exactly how the hardware detects that the stack is full.
PUSH: SP ← SP + 1 ; increment the stack pointer
M[SP] ← DR ; write the item at the new top
if (SP = 0) then FULL ← 1 ; wrapped around: stack is full
EMTY ← 0 ; stack cannot be empty after a push
POP: DR ← M[SP] ; read the top item
SP ← SP − 1 ; decrement the stack pointer
if (SP = 0) then EMTY ← 1 ; back at the start: stack is empty
FULL ← 0 ; stack cannot be full after a popA PUSH is allowed only when FULL = 0, and a POP only when EMTY = 0. Pushing onto a full stack is an overflow; popping an empty stack is an underflow.
Worked Example: Tracing a Small Register Stack
To keep the trace short, take an 8-word register stack with a 3-bit SP. Push A, B, ..., H and then pop twice:
Operation | SP after | Item stored / read at | FULL | EMTY |
|---|---|---|---|---|
start | 000 | - | 0 | 1 |
PUSH A | 001 | word 1 | 0 | 0 |
PUSH B | 010 | word 2 | 0 | 0 |
PUSH C ... PUSH G | 011 ... 111 | words 3 ... 7 | 0 | 0 |
PUSH H | 000 | word 0 | 1 | 0 |
POP (reads H) | 111 | word 0 | 0 | 0 |
POP (reads G) | 110 | word 7 | 0 | 0 |
After eight pushes SP wraps to 000 and FULL becomes 1. A ninth PUSH would now be refused. Popping all 8 items brings SP back to 000 with EMTY = 1.
Memory Stack
Most real processors keep the stack in main memory. A register SP holds the address of the top. In the common convention (used by Mano's textbook, x86 and ARM), the stack grows toward lower addresses:
PUSH: SP ← SP − 1 ; move the top down
M[SP] ← DR ; then write
POP: DR ← M[SP] ; read the top
SP ← SP + 1 ; then move the top upFor example, if SP = 3000 initially, the first PUSH writes the item at 2999, the second at 2998. A POP reads 2998 and sets SP back to 2999. (On byte-addressed machines SP moves by the word size, e.g. 8 on x86-64, not by 1.)
A memory stack has no FULL/EMTY flags in hardware. Instead, two limit values (an upper and a lower limit) can be compared with SP after each push or pop, or the operating system protects the pages around the stack so an overflow causes a fault.
Reverse Polish Notation
A stack machine evaluates expressions written in postfix form, also called reverse Polish notation (RPN). In postfix, each operator comes after its operands, so no parentheses are needed:
Infix | Postfix (RPN) |
|---|---|
A + B | A B + |
(A + B) × (C + D) | A B + C D + × |
A × B + C × D | A B × C D × + |
A − B / C + D × E | A B C / − D E × + |
(A + B) × C − (D − E) × (F + A) | A B + C × D E − F A + × − |
The evaluation rule is simple: scan left to right; push every operand; on every operator, pop two operands, apply the operator, and push the result.
Worked Example: Evaluating RPN on a Stack
Evaluate (5 + 3) × 2 − (9 − 1) / 4. In postfix this is 5 3 + 2 × 9 1 − 4 / −.
Token | Action | Stack (bottom → top) |
|---|---|---|
5 | push | 5 |
3 | push | 5, 3 |
+ | pop 3, 5; push 8 | 8 |
2 | push | 8, 2 |
× | pop 2, 8; push 16 | 16 |
9 | push | 16, 9 |
1 | push | 16, 9, 1 |
− | pop 1, 9; push 9 − 1 = 8 | 16, 8 |
4 | push | 16, 8, 4 |
/ | pop 4, 8; push 8 / 4 = 2 | 16, 2 |
− | pop 2, 16; push 16 − 2 = 14 | 14 |
The answer is 14, and the stack never held more than 3 items. The maximum depth tells you how big the stack must be for this expression.
Example: X = (A + B) × (C + D) on a Stack Machine
PUSH A ; stack: A
PUSH B ; stack: A, B
ADD ; stack: A+B
PUSH C ; stack: A+B, C
PUSH D ; stack: A+B, C, D
ADD ; stack: A+B, C+D
MUL ; stack: (A+B)×(C+D)
POP X ; X ← top; stack emptyEight instructions, but only PUSH and POP carry an address. The program is simply the postfix form A B + C D + × with a PUSH for each operand. With A = 2, B = 3, C = 4, D = 5 our simulator leaves X = 45 in every version of this program.
Pros and Cons
Pro: very short instructions (zero-address arithmetic) and compact code.
Pro: expression evaluation is trivial to compile: generate postfix and emit it.
Pro: natural support for nested subroutine calls and interrupts (return addresses are pushed and popped).
Con: only the top of the stack is accessible, so a value needed again must be pushed again (or duplicated).
Con: the stack top is a single point of traffic; it is hard to execute several instructions in parallel.
Stack machines are rare as physical CPUs today (the Burroughs B5000 and the x87 floating-point unit with its 8-register stack ST(0) to ST(7) are famous examples), but they are everywhere as virtual machines: Java bytecode (JVM), .NET CIL, CPython bytecode and WebAssembly are all stack-based.
All Three on One Expression
Here is X = (A + B) × (C + D) once more, now side by side. "Data memory accesses" counts reads and writes of the variables A, B, C, D, X and the temporary T (instruction fetches are not counted).
Organization | Instruction style | Instructions | Data memory accesses |
|---|---|---|---|
General register | Three-address | 3 | 5 |
General register | Two-address | 6 | 5 |
Single accumulator | One-address | 7 | 7 (includes STORE T and its reuse) |
Stack | Zero-address | 8 | 5 |
(Instruction Format & Instruction Cycle works through another expression, Y = (A − B) / (C + D × E), in all four styles.) The trend to remember: fewer addresses per instruction → shorter instructions but more of them. The accumulator machine also pays extra memory traffic for the temporary.
Comparison Table
Feature | Single accumulator | General register | Stack |
|---|---|---|---|
Where operands live | AC + memory | Any register (or memory) | Top of stack |
Typical instruction |
|
|
|
Addresses per ALU instruction | 1 | 2 or 3 | 0 |
Instruction length | Short | Longest | Shortest |
Program length (instruction count) | Long | Short | Long |
Intermediate results | Stored in memory | Kept in registers | Kept on the stack |
Hardware complexity | Lowest | Highest (register file, muxes) | Moderate |
Examples | EDSAC, Mano's basic computer, 6502 | MIPS, RISC-V, ARM, x86-64 | Burroughs B5000, x87 FPU, JVM, WebAssembly |
Real processors often mix the styles. The Intel 8085 is accumulator-based but also has six general registers (B, C, D, E, H, L) and a memory stack for subroutine calls. Every modern general-register CPU still has a memory stack for function calls.
Solved Problems
Problem 1: Control Word Size
A general-register CPU has 32 registers and its ALU supports 50 operations. Each micro-operation selects two source registers, one destination register and one ALU operation. How many bits are in the control word?
Solution. Selecting one of 32 registers needs log2 32 = 5 bits, so SELA, SELB and SELD need 5 bits each. Choosing one of 50 operations needs ⌈log2 50⌉ = 6 bits (25 = 32 is too few, 26 = 64 is enough). Total = 5 + 5 + 5 + 6 = 21 bits.
(If a code must also be reserved for "external input" or "no destination", as in Mano's design, you would need one more selection code; with exactly 32 registers that would force 6-bit select fields. Read the question carefully.)
Problem 2: Decode a Control Word
Using the 14-bit format above, what does 001 000 111 00001 do?
Solution. SELA = 001 (R1), SELB = 000 (unused), SELD = 111 (R7), OPR = 00001 (INCA). So R7 ← R1 + 1.
Problem 3: Stack Depth
How many PUSH instructions, how many arithmetic instructions, and what maximum stack depth are needed to evaluate (A + B) × C − (D − E) × (F + A) on a zero-address machine?
Solution. The postfix form is A B + C × D E − F A + × −. It has 7 operands (7 PUSHes; A is pushed twice because a stack cannot "reuse" a value without pushing it again) and 6 operators (6 arithmetic instructions). Tracking the depth: 1, 2, 1, 2, 1, 2, 3, 2, 3, 4, 3, 2, 1. Maximum depth = 4. Add one POP to store the result: 14 instructions in all.
Problem 4: Register Stack Flags
A 64-word register stack starts empty (SP = 0). After 64 PUSH operations, what are SP, FULL and EMTY? Where is the 64th item stored?
Solution. Each PUSH increments SP before writing. After 63 pushes SP = 63. The 64th push makes SP = 63 + 1 = 64, which wraps to 0 in 6 bits, and the item is written to word 0. Since SP = 0 after a push, FULL ← 1 and EMTY ← 0. Answer: SP = 0, FULL = 1, EMTY = 0, 64th item in word 0.
Problem 5: Memory Stack Addresses
A memory stack grows downward, SP = 1000, word-addressable memory. The program executes PUSH P, PUSH Q, POP, PUSH R. Where is R stored and what is SP at the end?
Solution. PUSH P: SP = 999, P at 999. PUSH Q: SP = 998, Q at 998. POP: reads Q, SP = 999. PUSH R: SP = 998, R at 998 (overwriting the old Q). Final SP = 998.
Common Mistakes
Getting the operand order wrong for SUB and DIV on a stack. The result is (second from top) op (top). For
PUSH A, PUSH B, SUBthe result is A − B, not B − A.Mixing up the PUSH order in register and memory stacks. In the textbook register stack SP is incremented then written; in a downward-growing memory stack SP is decremented then written. Always state the convention.
Forgetting the temporary in accumulator code. With only one AC, a partial result must be stored before computing another one.
Counting PUSH of a reused variable once. In (A + B) × C − (D − E) × (F + A), A must be pushed twice.
Forgetting to round up log2. 50 operations need 6 bits, not 5.6 or 5.
Thinking "zero-address" means no addresses anywhere. PUSH and POP still carry an address; only the arithmetic instructions are zero-address.
Interview Questions
What is an accumulator?
A special register that is the implied source of one ALU operand and the implied destination of the result. Instructions in an accumulator machine name only the other operand, so they are one-address instructions.
Why do modern CPUs use a general-register organization?
Registers are much faster than memory. Keeping operands and intermediate results in many registers reduces memory traffic, lets compilers allocate variables to registers, and makes it easier to execute independent instructions in parallel (pipelining, superscalar issue).
What are the fields of the control word in a general-register CPU?
SELA and SELB choose the registers that drive the two ALU inputs through multiplexers, SELD chooses the destination register through a decoder, and OPR chooses the ALU operation. In Mano's 7-register design this is 3 + 3 + 3 + 5 = 14 bits.
How does a stack machine evaluate an arithmetic expression?
The expression is converted to postfix (reverse Polish) notation. Operands are pushed; each operator pops its two operands, computes, and pushes the result. At the end, the result is on top of the stack.
What are stack overflow and underflow?
Overflow is a push onto a full stack; underflow is a pop from an empty stack. A register stack detects them with FULL and EMTY flags; a memory stack uses limit registers or protected guard pages.
Name some stack-based architectures in use today.
The Java Virtual Machine, .NET CIL, CPython bytecode and WebAssembly are stack-based virtual machines. In hardware, the x87 floating-point unit uses an 8-register stack.
Which organization gives the shortest programs, and which the shortest instructions?
Three-address general-register code gives the fewest instructions; zero-address stack code gives the shortest individual instructions but needs more of them.
Summary
CPU organization is about where the operands live: in an accumulator, in general registers, or on a stack.
Single accumulator: one-address instructions, simplest hardware, but intermediate results must go to memory.
General register: two- or three-address instructions; a register file, two source multiplexers, an ALU and a destination decoder, driven by a control word (SELA, SELB, SELD, OPR; 14 bits in Mano's 7-register design). Used by almost all modern CPUs.
Stack: zero-address arithmetic; register stack with SP, FULL and EMTY, or memory stack that usually grows downward. Expressions are evaluated in postfix (RPN).
For X = (A + B) × (C + D): 3 instructions (three-address), 6 (two-address), 7 (one-address), 8 (zero-address).
Next, ALU & Datapath looks inside the ALU and at how registers, buses and the ALU are wired together to execute complete instructions.