Fetching every operand from memory is slow. A processor works faster if it keeps frequently used values in registers inside the CPU. Registers are organised in a bank, with the ALU and the buses, and a stack provides a convenient last-in first-out storage.
General Register Organization
In a CPU with general registers, a set of registers such as R1 to R7 is connected by two multiplexers to the ALU. Multiplexer A and multiplexer B choose the two operands. The ALU result can be loaded into any register chosen by a decoder. A control word selects everything for one micro-operation. A control word of 14 bits has three bits for SELA, three for SELB, three for SELD and five for the ALU operation OPR.
Definition: Control word. A group of bits that specifies, for a single micro-operation, which registers supply the operands, which register receives the result and which ALU operation is performed.
Stack Organization
A stack is a storage in which the last item stored is the first item removed. A stack pointer (SP) holds the address of the top item. Push adds an item and pop removes the top item. In a register stack the items are in a set of registers, and in a memory stack a part of main memory is used. In a memory stack that grows downward, push does SP ← SP - 1, M[SP] ← item, and pop does item ← M[SP], SP ← SP + 1.
Table 3.6: Push and pop operations on a memory stack
| Operation | Steps | Effect on SP |
|---|---|---|
| Push | SP = SP - 1, then M[SP] = DR | Decreases by 1 |
| Pop | DR = M[SP], then SP = SP + 1 | Increases by 1 |
Reverse Polish Notation
A stack evaluates expressions written in reverse Polish notation (RPN), in which the operator follows its operands. The expression (A + B) (C + D) is written AB+CD+. The processor pushes an operand, and when it meets an operator it pops two operands, applies the operator and pushes the result.
Example 3.7: Evaluating an RPN expression
Problem: Evaluate 3 4 + 2 5 + * using a stack.
Solution:
- Push 3 and push 4. On + pop 4 and 3, and push 7.
- Push 2 and push 5. On + pop 5 and 2, and push 7.
- On * pop 7 and 7, and push 49.
Result: The value left on the stack is 49.
stack = []
for t in "3 4 + 2 5 + *".split():
if t.isdigit():
stack.append(int(t))
else:
b, a = stack.pop(), stack.pop()
stack.append(a + b if t == "+" else a * b)
print(t, stack)
Output
3 [3]
4 [3, 4]
+ [7]
2 [7, 2]
5 [7, 2, 5]
+ [7, 7]
* [49]
Common mistakes
- Confusing the order of operands when an operator pops two values. The second value popped is the left operand.
- Increasing the stack pointer on a push in a downward-growing stack.
- Forgetting to check for an overflow or underflow of the stack.
Key takeaways
- A control word of 14 bits selects SELA, SELB, SELD and OPR.
- Last in, first out is the rule of a stack.
- SP holds the address of the top of the stack.
- RPN lets a stack evaluate an expression without brackets.
Practice questions
- What is the function of the stack pointer?
- Write (A + B) * C in reverse Polish notation.
- How many bits does the control word of the 7-register CPU have?
Answers to the practice questions
- It holds the address of the top item of the stack.
- AB+C*
- 14 bits: 3 + 3 + 3 + 5.
Sources
- Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit III
