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

Group: Processor
URL: https://digiwleea.wleeaf.dev/learn/alu/

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](https://digiwleea.wleeaf.dev/learn/subtract/) does the arithmetic. The ALU wraps that together with a little bitwise logic and a [multiplexer](https://digiwleea.wleeaf.dev/learn/mux/) that picks which result comes out.

## One unit, several operations

Compute several candidate results in parallel, then use a [mux](https://digiwleea.wleeaf.dev/learn/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 |

_The function code selects the operation. 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](https://digiwleea.wleeaf.dev/learn/subtract/) from the last lesson, with `F0` driving its `SUB` line. The logic path is a column of [AND](https://digiwleea.wleeaf.dev/learn/and/) gates and a column of [OR](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/mux/) 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](https://digiwleea.wleeaf.dev/learn/nor/) across all eight output bits, which you can build as an [OR](https://digiwleea.wleeaf.dev/learn/or/) tree feeding one [NOT](https://digiwleea.wleeaf.dev/learn/not/). Later, comparing two numbers is just subtracting them and watching this flag: if `A - B` is zero, then `A` equals `B`.

```
ZERO = NOT (O0 OR O1 OR O2 OR O3 OR O4 OR O5 OR O6 OR O7)
```

_Circuit diagram: An 8-bit ALU (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._

**Q (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`.

**A:** 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)`.

> **TIP:** 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.

> **KEY:** You now have storage (the [register](https://digiwleea.wleeaf.dev/learn/register8/)) 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](https://digiwleea.wleeaf.dev/learn/counter/), [memory](https://digiwleea.wleeaf.dev/learn/ram/), the [instruction register](https://digiwleea.wleeaf.dev/learn/instruction-register/), and the [control unit](https://digiwleea.wleeaf.dev/learn/control/), is what turns these blocks into a [CPU](https://digiwleea.wleeaf.dev/learn/cpu/) that runs a program.

### FAQ

**Q:** What is an ALU?

**A:** 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.

**Q:** How does an ALU choose which operation to perform?

**A:** It computes several candidate results in parallel, then a [multiplexer](https://digiwleea.wleeaf.dev/learn/mux/) 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.

**Q:** What is the zero flag in an ALU?

**A:** The zero flag is a status bit that is `1` exactly when every output bit is `0`. It is a single [NOR](https://digiwleea.wleeaf.dev/learn/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`.

**Q:** What is the difference between an ALU and an adder?

**A:** An adder only adds (and, with the [XOR](https://digiwleea.wleeaf.dev/learn/xor/) trick, subtracts). An ALU bundles that [adder/subtractor](https://digiwleea.wleeaf.dev/learn/subtract/) together with bitwise logic behind a [multiplexer](https://digiwleea.wleeaf.dev/learn/mux/), so one unit offers several operations plus status flags.
