The ALU
CPU Basics · Foundations track
3 min read
In this module
Where Maths and Logic Happen
The Arithmetic Logic Unit, ALU for short, is the part of the CPU that does the actual computation. When an instruction says "add these two numbers" or "compare these values," the ALU is the circuit doing the work.
If you finished the logic gates module, you already know the building blocks. An ALU is just combinations of AND, OR, NOT, and XOR gates wired together to perform higher-level operations.
What the ALU Can Do
ALU operations fall into two camps:
Arithmetic operations. Addition, subtraction, and sometimes multiplication or division. Every ALU can add and subtract at the very least. More complex maths usually gets built from those two primitives.
Logic operations. AND, OR, NOT, and XOR applied to entire binary words. Shift operations (moving all bits left or right) live here too. These are the workhorses for masking bits, testing conditions, and bit-level data manipulation.
From the outside, an ALU has three inputs and two outputs. Two operands go in, plus an operation code from the control unit saying which job to do. Out comes the result, plus a set of flags describing it:
Inside, it's the arrangement from the logic gates module: every operation circuit works on A and B at once, and a multiplexer driven by the operation code picks which answer leaves.
The ALU does two kinds of work: arithmetic (add, subtract) and logic (AND, OR, NOT, shift). It's built from the same logic gates you studied earlier, wired together into larger functional circuits.
Flags: What Just Happened?
After every operation, the ALU sets a handful of flags, single-bit indicators that describe the result. The most common ones:
- Zero (Z), set to 1 when the result is exactly zero.
- Carry (C), set to 1 when the operation produced a carry out of the highest bit (unsigned overflow).
- Overflow (V), set to 1 when the result overflowed in signed arithmetic.
- Sign / Negative (N), set to 1 when the result is negative (the highest bit is 1 in two's complement).
These flags live in a dedicated flags register, and later instructions consult them to make decisions. A "branch if zero" instruction, for instance, checks the zero flag to decide whether to jump elsewhere in the program.
Step through the program below and keep an eye on the Z, C, V and N lights under the registers. The first SUB works out 5 − 5, gets zero and sets Z, so the JZ jumps over the MOV R3 line. The second SUB works out 3 − 5. In 8 bits that's 0xFE (−2 in two's complement), so N comes on, and C comes on too because the subtraction had to borrow, which is the unsigned way of saying 3 − 5 doesn't fit. V stays off: −2 is a perfectly good signed answer.
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 0From Gates to Computation
The ALU is the bridge between raw logic gates and useful computation. Gates handle individual bits; the ALU combines them into circuits that operate on whole numbers and produce meaningful results. Every calculation your computer performs, from rendering pixels to running a spreadsheet formula, ultimately flows through the ALU.
Try the Simulations
Add Two Numbers
Load two values into registers and add them together. The simplest possible program.
Understanding CPU Flags
Run several comparisons and watch how the CPU flags change. Equal values set the zero flag, smaller-than sets the negative flag - flags are how the CPU remembers comparison results.