Multiplexers, Decoders & the ALU
Digital Logic Design · Hardware Foundations track
5 min read
In this module
Routing and Selecting Data
Before we can build an ALU we need two utility circuits that crop up everywhere in processor design: multiplexers and decoders. They're how the right data gets to the right place at the right time.
The Multiplexer (MUX)
A multiplexer picks one of N inputs and forwards it to a single output. The choice is steered by select lines, and a MUX with N inputs needs log2(N) of them.
The simplest version is a 2-to-1 MUX: two data inputs (I0 and I1), one select line (S), one output (Y).
| S | Y (output) |
|---|---|
| 0 | I0 |
| 1 | I1 |
The Boolean expression is: Y = (NOT S AND I0) OR (S AND I1).
Bigger multiplexers run on the same idea:
- 4-to-1 MUX — 4 inputs, 2 select lines. The 2-bit select value picks one of four inputs.
- 8-to-1 MUX — 8 inputs, 3 select lines.
A multiplexer selects one of N inputs based on the value of its select lines. Multiplexers are critical in CPU design - they choose between register values, immediate constants, ALU results, and memory data at every stage of instruction execution.
Inside a CPU, multiplexers decide whether the ALU's second input comes from a register or an immediate, and whether writeback data flows in from the ALU or from memory.
The Decoder
A decoder swallows an N-bit binary input and activates exactly one of 2^N output lines. The result is called one-hot output: exactly one line high, all the rest low.
A 2-to-4 decoder:
| A1 | A0 | O3 | O2 | O1 | O0 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 | 0 | 0 |
Input 01 lights up output line O1, input 10 lights up O2, and so on.
Decoders pick which register the register file should read or write, and they show up again in memory address decoding and instruction decoding throughout a processor.
Inside the ALU
The Arithmetic Logic Unit is the processor's computational heart. It runs arithmetic (addition, subtraction) and logical (AND, OR, NOT, XOR) operations on two inputs. Which operation it actually performs is dictated by ALU operation select lines, often called ALUOp or the function code.
An ALU comes together from three components we've already met:
- An adder/subtractor — a ripple-carry adder (or a faster variant) handling addition. Subtraction is just addition with one input inverted and a +1 thrown in, which is two's complement negation in disguise.
- A logic unit — a bank of gates computing AND, OR, XOR, and NOT in parallel across every bit position.
- A multiplexer — picks which result, arithmetic or logical, gets sent to the output, based on the ALUOp control signal.
ALU Flags
After every operation the ALU sets status flags (also called condition codes) that record properties of the result:
- Zero Flag (Z) — set to
1when the result is exactly zero. Conditional branches lean on this flag heavily; a "branch if equal", for instance, checks whether a subtraction produced zero. - Carry Flag (C) — set to
1when the arithmetic produced a carry out of the most significant bit. For unsigned arithmetic that's the signal the result has overflowed the representable range. Subtraction is where architectures disagree. The adder's raw carry-out forA + ~B + 1is1when there was no borrow. Arm stores it as is, so C=1 means "no borrow". x86 inverts it, so CF=1 means a borrow happened. This course's simulator follows the x86 convention. - Overflow Flag (V) — set to
1when a signed result is too big or too small to fit. The dead giveaway: two positives summing to a negative, or two negatives summing to a positive. - Sign Flag (S) — copies the most significant bit of the result, telling you whether it's negative under signed two's complement.
The simulator's ALU is 8 bits wide, so its flags fire at small numbers. It labels them Z, C, V and N (N is the sign flag). The program below doubles 15 up to 120, then pushes past the signed and unsigned limits. Before each step, predict which flags will light. Adding 8 to 120 gives 128, which is fine unsigned but negative as a signed byte, so V and N should come on. Doubling 128 gives 256, which doesn't fit in 8 bits at all: the result wraps to 0, so expect Z and C together, with V too, because two negative inputs produced a non-negative result.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0The ALU combines an adder, a logic unit, and a multiplexer. The ALUOp select lines choose the operation. After each computation, the ALU sets flags (zero, carry, overflow, sign) that conditional branch instructions later test. These flags are how a processor makes decisions.
Putting It Together
When the CPU runs CMP R0, R1, the ALU subtracts and throws the numerical answer away. Only the flags matter. Zero flag set? The values were equal. A later conditional branch reads those flags and decides whether to jump. Every decision a program ever makes comes down, in the end, to the ALU computing some result and these flags reporting what happened.