Skip to main content
Be Bitwise

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.

Key Concept

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

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

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