The ALU
The arithmetic and logic core
An ALU (arithmetic logic unit) performs one of several operations on two input bytes, selected by a function code, and reports status such as a zero flag. It bundles the adder/subtractor with bitwise logic behind a multiplexer to form the CPU's compute unit.
An ALU (arithmetic logic unit) is the part that actually computes. It takes two operands,
A and B, a small function code that says which operation to perform, and produces a result plus some status flags. You already have the expensive pieces: the adder/subtractor does the arithmetic. The ALU wraps that together with a little bitwise logic and a multiplexer that picks which result comes out.One unit, several operations
Compute several candidate results in parallel, then use a mux steered by the function code to forward just the one you asked for. Our ALU takes a 2-bit function code
F1 F0 and offers four operations:| F1 | F0 | operation |
|---|---|---|
| 0 | 0 | A + B |
| 0 | 1 | A - B |
| 1 | 0 | A AND B |
| 1 | 1 | A OR B |
F0 doubles as the SUB control on the arithmetic side and as the AND-versus-OR pick on the logic side; F1 chooses arithmetic versus logic.The arithmetic path is exactly the adder/subtractor from the last lesson, with
F0 driving its SUB line. The logic path is a column of AND gates and a column of OR gates, one per bit, with F0 selecting between them. Finally F1 selects, per bit, between the arithmetic result and the logic result. Eight little multiplexers do that final pick, so the output bus O0-O7 carries whichever operation F1 F0 named.Flags: the zero detector
The result alone is not enough; the CPU also needs to know *about* the result so it can make decisions. The simplest and most useful status bit is the zero flag:
ZERO = 1 exactly when every output bit is 0. That is a single NOR across all eight output bits, which you can build as an OR tree feeding one NOT. Later, comparing two numbers is just subtracting them and watching this flag: if A - B is zero, then A equals B.ZERO = ¬(O0 ∨ O1 ∨ O2 ∨ O3 ∨ O4 ∨ O5 ∨ O6 ∨ O7)
ALU8): operands A and B, a 2-bit function code F0/F1, an 8-bit result O0-O7, and the ZERO flag. Open it in the lab, set two bytes, and step the function code through 00/01/10/11 to watch it add, subtract, AND, and OR. Try A = B with subtract and watch ZERO go high.Try it
You want the ALU to tell you whether
A equals B. Which function code do you select, and which output do you watch? Try it with A = B = 7 and then A = 7, B = 9.Answer
Select subtract (
F1 F0 = 01) and watch the ZERO flag. With A = B = 7, A - B = 0, so ZERO = 1: equal. With A = 7, B = 9, the result is nonzero, so ZERO = 0: not equal. Comparing is just subtracting and checking the zero flag, which is how a CPU implements if (a == b).Real ALUs offer more operations (shifts, XOR, compare) and more flags (carry, sign, overflow), but the shape never changes: compute candidates in parallel, mux the chosen one out, and derive flags from the result. Add an operation by adding a candidate and widening the function code and its mux.
You now have storage (the register) and compute (this ALU). What is missing is orchestration: something to hold instructions, fetch them in order, and pull the right control levers each step. That machinery, the program counter, memory, the instruction register, and the control unit, is what turns these blocks into a CPU that runs a program.
Frequently asked
What is an ALU?
An ALU (arithmetic logic unit) performs one of several operations on two input bytes, selected by a function code, and reports status such as a zero flag. It is the CPU's compute unit.
How does an ALU choose which operation to perform?
It computes several candidate results in parallel, then a multiplexer steered by the function code forwards just the one you asked for. This ALU's 2-bit code
F1 F0 selects add, subtract, AND, or OR.What is the zero flag in an ALU?
The zero flag is a status bit that is
1 exactly when every output bit is 0. It is a single NOR across all the result bits. Comparing two numbers is just subtracting them and checking this flag: if A - B = 0, then A equals B.What is the difference between an ALU and an adder?
An adder only adds (and, with the XOR trick, subtracts). An ALU bundles that adder/subtractor together with bitwise logic behind a multiplexer, so one unit offers several operations plus status flags.
Every lesson here builds toward one thing: a working CPU, from the transistor up.
Open the free lab →