Skip to main content
Be Bitwise

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).

SY (output)
0I0
1I1

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.
Key Concept

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:

A1A0O3O2O1O0
000001
010010
100100
111000

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:

  1. 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.
  2. A logic unit — a bank of gates computing AND, OR, XOR, and NOT in parallel across every bit position.
  3. 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 1 when 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 1 when 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 for A + ~B + 1 is 1 when 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 1 when 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

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded
Key Concept

The 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.