8-bit ALU from eight 1-bit slices, live
An 8-bit ALU (arithmetic logic unit) applies one of four operations to two 8-bit numbers A and B, chosen by F1 F0: 00 add, 01 subtract, 10 AND, 11 OR. This one is eight copies of the 1-bit ALU slice in a row, with the carry rippling from slice to slice, plus a zero flag Z that is 1 when the result is 0.
Each slice has a full adder whose B input passes through an XOR with F0, so F0 = 1 inverts B, and the carry into bit 0 is F0 as well: A + NOT B + 1 is A minus B in two's complement. F1 picks the adder's sum or the logic result, which F0 switches between AND and OR. Cout is the carry out of the top slice; in a subtraction it is 1 when A is at least B (as unsigned numbers). The Z flag is what a CPU's conditional jump tests. The 1-bit ALU slice and the 4-bit ALU show the same design smaller. Before the page is built, 632 operations are checked, every operation on 158 operand pairs (edge cases such as 255 + 1 and 128 + 128, plus pseudo-random values), against ordinary 8-bit arithmetic.
Open this circuit in the lab →
What you need
- 8× 1-bit ALU slice: each a full adder with B XOR F0, an AND, an OR and two 2-to-1 muxes
- 7× OR: OR of the eight result bits
- 1× NOT: Z = NOT (that OR)
Step by step
- Place inputs
A7toA0,B7toB0,F1andF0, and probesR7toR0,CoutandZ. - Place eight 1-bit ALU slices. Wire Ai and Bi to slice i, and F1 and F0 to every slice.
- Chain the carries: slice 0's carry-in is F0, and each slice's Cout feeds the next slice's Cin. Slice 7's Cout is the ALU's Cout.
- Zero flag: OR the eight R outputs together in a tree and invert the result.
- Try F1 F0 = 01 with A = B: the result is 0, so Z = 1, and Cout = 1 because A is at least B.
Operations
| F1 F0 | Operation | R | Cout |
|---|---|---|---|
| 00 | ADD | A + B (low 8 bits) | carry out of bit 7 |
| 01 | SUB | A - B, as A + NOT B + 1 | 1 when A is at least B (unsigned) |
| 10 | AND | A AND B, bit by bit | the adder's carry (ignored) |
| 11 | OR | A OR B, bit by bit | the adder's carry (ignored) |
Z is 1 whenever R is 00000000, in any operation. Signed overflow is not flagged here; it is the XOR of the carries into and out of bit 7.
You can also read the full theory in the lesson on the ALU. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.
Build more parts from gates
Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.
Open the 8-bit ALU in the lab →Frequently asked
What is an 8-bit ALU?
The part of a CPU that performs arithmetic and logic on 8-bit numbers: here add, subtract, AND and OR, chosen by two control bits, with a carry out and a zero flag.
How does an ALU subtract?
It adds the two's complement: it inverts every bit of B and sets the carry into the lowest bit to 1, so A + NOT B + 1 equals A minus B. The same adder does both jobs.
What is the zero flag used for?
It tells the CPU whether the last result was 0, so instructions such as jump-if-zero can branch on it; comparing two numbers is a subtraction followed by a look at the flags.
How many 1-bit ALUs make an 8-bit ALU?
Eight, one per bit, chained by their carries like a ripple-carry adder, with the control bits shared by all of them.