Loading lessons...
CPU Organisation: General Register Organisation, Stack Organisation & Reverse Polish Notation
CPU Organisation: General Register Organisation, Stack Organisation & Reverse Polish Notation
The basic computer has a single accumulator. Real CPUs are organised in one of three ways: a single accumulator, general registers, or a stack. This lesson builds the general register CPU and its control word, then the register stack and the memory stack, and shows why stacks are perfect for evaluating expressions written in reverse Polish notation.
1. The Three CPU Organisations
| Organisation | Where operands are | Typical instruction | Address fields |
|---|---|---|---|
| Single accumulator | AC and memory | ADD X (AC ← AC + M[X]) | one |
| General register | several processor registers | ADD R1, R2, R3 (R1 ← R2 + R3) | two or three |
| Stack | the top of a stack | ADD (pop two, push the sum) | zero |
Most processors mix these: general registers for data, plus a stack in memory for subroutine calls and interrupts.
2. General Register Organisation
Registers are much faster than memory, so keeping intermediate results in a set of general-purpose registers speeds up programs. Mano's example has seven registers R1 to R7 connected through two multiplexers to an ALU:
General register organisation
- MUX A and MUX B each pick one register (or the external input) as an ALU operand, selected by SELA and SELB.
- The ALU performs the operation selected by OPR.
- The result goes to the output bus and back to the registers; a 3 × 8 decoder driven by SELD enables the load input of exactly one destination register (000 loads none).
To do R1 ← R2 + R3 in one clock: SELA = R2, SELB = R3, OPR = ADD, SELD = R1.
2.1 The control word
Control word format
| Binary | SELA | SELB | SELD |
|---|---|---|---|
| 000 | Input | Input | None |
| 001 to 111 | R1 to R7 | R1 to R7 | R1 to R7 |
| OPR | Symbol | Operation | OPR | Symbol | Operation |
|---|---|---|---|---|---|
| 00000 | TSFA | transfer A | 01010 | OR | A ∨ B |
| 00001 | INCA | A + 1 | 01100 | XOR | A ⊕ B |
| 00010 | ADD | A + B | 01110 | COMA | complement A |
| 00101 | SUB | A − B | 10000 | SHRA | shift right A |
| 00110 | DECA | A − 1 | 11000 | SHLA | shift left A |
| 01000 | AND | A ∧ B |
Give the 14-bit control words for: R1 ← R2 - R3, R4 ← R4 vee R5, R6 ← R6 + 1, R7 ← R1, Output ← R2, R4 ← shl R4, R5 ← 0.
- R1 ← R2 - R3: 010 011 001 00101.
- R4 ← R4 vee R5: 100 101 100 01010.
- R6 ← R6 + 1: 110 000 110 00001 (SELB is unused: 000).
- R7 ← R1: 001 000 111 00000 (TSFA).
- Output ← R2: 010 000 000 00000 (SELD = None).
- R4 ← shl R4: 100 000 100 11000.
- R5 ← 0: 101 101 101 01100 (XOR of R5 with itself).
What does 001 010 011 00010 do? And 111 000 000 10000?
- SELA = 001 (R1), SELB = 010 (R2), SELD = 011 (R3), OPR = 00010 (ADD): R3 ← R1 + R2.
- SELA = R7, SELD = none, OPR = SHRA: Output ← shr R7 (no register changes).
The model below implements the organisation, loads 10, 20, ..., 70 into R1 to R7 from the input bus, and then applies the control words of Example 1 (F is the ALU output, then the registers after the clock):
// General register organization: 7 registers, two source MUXes, an ALU and a
// destination decoder, driven by a 14-bit control word SELA SELB SELD OPR
module gr_cpu (
input clk,
input [13:0] cw,
input [7:0] inbus,
output [7:0] outbus
);
reg [7:0] R [1:7];
wire [2:0] sela = cw[13:11], selb = cw[10:8], seld = cw[7:5];
wire [4:0] opr = cw[4:0];
wire [7:0] a = (sela == 3'd0) ? inbus : R[sela]; // MUX A
wire [7:0] b = (selb == 3'd0) ? inbus : R[selb]; // MUX B
reg [7:0] f;
always @(*)
case (opr)
5'b00000: f = a; // TSFA
5'b00001: f = a + 1'b1; // INCA
5'b00010: f = a + b; // ADD
5'b00101: f = a - b; // SUB
5'b00110: f = a - 1'b1; // DECA
5'b01000: f = a & b; // AND
5'b01010: f = a | b; // OR
5'b01100: f = a ^ b; // XOR
5'b01110: f = ~a; // COMA
5'b10000: f = a >> 1; // SHRA
5'b11000: f = a << 1; // SHLA
default: f = 8'h00;
endcase
assign outbus = f;
always @(posedge clk)
if (seld != 3'd0) R[seld] <= f; // 3 x 8 decoder: SELD = 000 loads nothing
endmodule
module tb;
reg clk = 0;
reg [13:0] cw;
reg [7:0] inbus;
wire [7:0] outbus;
integer i;
reg [7:0] f;
gr_cpu u (clk, cw, inbus, outbus);
always #5 clk = ~clk;
task step(input [13:0] w, input [8*16:1] name);
begin
cw = w;
#1 f = outbus; // ALU output, before the clock edge
@(posedge clk);
#1 $display("%b %b %b %b F=%h R1..R7 = %h %h %h %h %h %h %h %0s",
w[13:11], w[10:8], w[7:5], w[4:0], f,
u.R[1], u.R[2], u.R[3], u.R[4], u.R[5], u.R[6], u.R[7], name);
end
endtask
initial begin
// load R1..R7 with 10, 20, ..., 70 from the input bus: Ri <- Input
for (i = 1; i <= 7; i = i + 1) begin
inbus = 10 * i;
cw = {3'b000, 3'b000, i[2:0], 5'b00000};
@(posedge clk);
end
#1 inbus = 8'h00;
step(14'b010_011_001_00101, "R1 <- R2 - R3");
step(14'b100_101_100_01010, "R4 <- R4 | R5");
step(14'b110_000_110_00001, "R6 <- R6 + 1");
step(14'b001_000_111_00000, "R7 <- R1");
step(14'b100_000_100_11000, "R4 <- shl R4");
step(14'b101_101_101_01100, "R5 <- 0");
step(14'b010_000_000_00000, "Output <- R2");
$finish;
end
endmoduleR1 becomes 20 − 30 = −10 (F6), R4 becomes 28 ∨ 32 = 3A and then shifts to 74, and the XOR clears R5. The last word only drives the output (14 = R2) and loads nothing:
010 011 001 00101 F=f6 R1..R7 = f6 14 1e 28 32 3c 46 R1 <- R2 - R3
100 101 100 01010 F=3a R1..R7 = f6 14 1e 3a 32 3c 46 R4 <- R4 | R5
110 000 110 00001 F=3d R1..R7 = f6 14 1e 3a 32 3d 46 R6 <- R6 + 1
001 000 111 00000 F=f6 R1..R7 = f6 14 1e 3a 32 3d f6 R7 <- R1
100 000 100 11000 F=74 R1..R7 = f6 14 1e 74 32 3d f6 R4 <- shl R4
101 101 101 01100 F=00 R1..R7 = f6 14 1e 74 00 3d f6 R5 <- 0
010 000 000 00000 F=14 R1..R7 = f6 14 1e 74 00 3d f6 Output <- R23. Stack Organisation
A stack is a last-in, first-out (LIFO) list. The register that holds the address of the top item is the stack pointer (SP). The two operations are PUSH (insert on top) and POP (remove from the top). Nothing is physically moved: only SP changes.
3.1 Register stack
A stack can be a set of fast registers. Mano's register stack has 64 words, a 6-bit SP, and two flags: FULL (all 64 words used) and EMTY (no items).
Register stack
Initially SP = 0, EMTY = 1, FULL = 0. The first item goes into word 1, and word 0 is used last (SP wraps from 63 to 0).
PUSH (if FULL = 0):
POP (if EMTY = 0):
A 64-word register stack holds 63 items (SP = 63). What happens on one PUSH and then 64 POPs?
- PUSH: SP ← 63 + 1 = 0 (6-bit wrap), M[0] ← DR, SP = 0 so FULL ← 1. Now 64 items.
- Further PUSHes are refused (FULL = 1).
- POP 1: DR ← M[0], SP ← 63, FULL ← 0. POPs continue down to SP = 1.
- POP 64 takes M[1]: SP ← 0, so EMTY ← 1.
3.2 Memory stack
A stack can also live in main memory, in a segment of its own next to the program and the data. PC points into the program, AR into the data, and SP to the top of the stack. In Mano's example the stack grows towards lower addresses: it starts at 4001 and its first item goes to 4000.
Memory stack
Most computers have no hardware check for overflow of a memory stack; two processor registers can hold the upper and lower limits, and SP is compared with them after each push or pop.
SP = 4001 initially. A, B, C are pushed, then one item is popped. Give SP and the address of each item.
- PUSH A: SP = 4000, M[4000] = A. PUSH B: SP = 3999. PUSH C: SP = 3998.
- POP: DR ← M[3998] = C, SP = 3999 (top is B).
4. Reverse Polish Notation
A stack evaluates arithmetic expressions most easily when they are written in postfix form.
| Notation | Form | Example |
|---|---|---|
| Infix | operator between operands | A + B |
| Prefix (Polish) | operator before operands | + A B |
| Postfix (reverse Polish, RPN) | operator after operands | A B + |
RPN needs no parentheses. To convert, apply the precedence rules (multiply and divide before add and subtract, parentheses first) and write each operator after its two operands.
Convert (a) A ∗ B + C ∗ D, (b) (A + B) ∗ [C ∗ (D + E) + F], (c) A + B ∗ C − D / E.
- (a) A B ∗ and C D ∗, then add: A B ∗ C D ∗ +.
- (b) (A + B) → A B +; (D + E) → D E +; C ∗ (D E +) → D E + C ∗ (or C D E + ∗); + F → D E + C ∗ F +; multiply: A B + D E + C ∗ F + ∗.
- (c) B ∗ C first: A B C ∗ +, then D / E: A B C ∗ + D E / −.
4.1 Evaluating RPN with a stack
Scan the expression from left to right:
- operand: push it;
- operator: pop the top two items, apply the operator (second-from-top op top), push the result.
At the end the stack holds exactly one item: the value.
RPN: 3 4 ∗ 5 6 ∗ +. Show the stack after each symbol.
- 3 → [3]; 4 → [3, 4]; ∗ → [12].
- 5 → [12, 5]; 6 → [12, 5, 6]; ∗ → [12, 30].
- + → [42].
Evaluate 5 1 2 + 4 ∗ + 3 −.
- 5, 1, 2 pushed: [5, 1, 2]. + → [5, 3]. 4 → [5, 3, 4]. ∗ → [5, 12].
- + → [17]. 3 → [17, 3]. − → [14].
A stack computer does this in hardware: PUSH and POP move data, and an arithmetic instruction with no address field (a zero-address instruction, next lesson) operates on the top two words. The model below follows Mano's register-stack rules (SP points at the top, the first push goes to word 1) and combines the top two words in one clock for each operator:
// A register stack of 8 words (Mano's rules: SP points at the top word, the
// first push goes to word 1) used to evaluate a reverse Polish expression
module stack_alu (
input clk,
input rst,
input [1:0] op, // 00 push din, 01 add, 10 subtract, 11 multiply
input [15:0] din,
output reg [2:0] sp,
output reg full,
output reg emty,
output [15:0] tos
);
reg [15:0] M [0:7];
wire [2:0] below = sp - 1'b1;
assign tos = M[sp];
always @(posedge clk)
if (rst) begin
sp <= 3'd0;
full <= 1'b0;
emty <= 1'b1;
end else if (op == 2'b00 && !full) begin // PUSH
M[sp + 1'b1] <= din; // SP <- SP + 1, M[SP] <- DR
sp <= sp + 1'b1;
full <= (sp + 1'b1 == 3'd0); // if SP = 0 then FULL <- 1
emty <= 1'b0;
end else if (op != 2'b00 && !emty) begin // pop two, push the result
case (op)
2'b01: M[below] <= M[below] + M[sp];
2'b10: M[below] <= M[below] - M[sp];
default: M[below] <= M[below] * M[sp];
endcase
sp <= below; // SP <- SP - 1
full <= 1'b0;
emty <= (below == 3'd0);
end
endmodule
module tb;
reg clk = 0, rst = 1;
reg [1:0] op;
reg [15:0] din;
wire [2:0] sp;
wire full, emty;
wire [15:0] tos;
integer i;
stack_alu u (clk, rst, op, din, sp, full, emty, tos);
always #5 clk = ~clk;
task token(input [1:0] o, input [15:0] v, input [8*2:1] name);
begin
op = o;
din = v;
@(posedge clk);
#1 $write("%0s SP=%0d stack:", name, sp);
for (i = 1; i <= sp; i = i + 1) $write(" %0d", u.M[i]);
$display("");
end
endtask
initial begin
op = 2'b00;
@(posedge clk);
#1 rst = 0;
// (3 * 4) + (5 * 6) in reverse Polish: 3 4 * 5 6 * +
token(2'b00, 3, "3");
token(2'b00, 4, "4");
token(2'b11, 0, "*");
token(2'b00, 5, "5");
token(2'b00, 6, "6");
token(2'b11, 0, "*");
token(2'b01, 0, "+");
$display("result = %0d, EMTY = %b", tos, emty);
$finish;
end
endmoduleThe stack contents after every symbol are exactly those of Example 6, and one item, 42, is left:
3 SP=1 stack: 3
4 SP=2 stack: 3 4
* SP=1 stack: 12
5 SP=2 stack: 12 5
6 SP=3 stack: 12 5 6
* SP=2 stack: 12 30
+ SP=1 stack: 42
result = 42, EMTY = 0Key points to remember
- CPU organisations: single accumulator, general register, stack.
- General registers: MUX A, MUX B, ALU, decoder; control word SELA (3) SELB (3) SELD (3) OPR (5) = 14 bits.
- Register stack PUSH: SP ← SP + 1, M[SP] ← DR; POP: DR ← M[SP], SP ← SP − 1; flags FULL and EMTY.
- Memory stack (growing down): PUSH SP ← SP − 1 first; POP increments SP after reading.
- RPN: operator after operands; evaluate by pushing operands and applying operators to the top two.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Draw the block diagram of a general register organisation with seven registers. Explain the control word and give the control word for R1 ← R2 - R3. (10 Marks)
- Q2. Explain the register stack with PUSH and POP microoperations and the role of FULL and EMTY. (5 Marks)
- Q3. Explain the memory stack. How is stack overflow detected? (5 Marks)
- Q4. What is reverse Polish notation? Convert (A + B) ∗ [C ∗ (D + E) + F] to RPN and show the stack evaluation of (3 ∗ 4) + (5 ∗ 6). (10 Marks)
- Write: the conversion steps, and the stack contents after each symbol.
- Q5. Give the control words for R4 ← R4 vee R5 and R5 ← 0. (2.5 Marks)