ALU8

Inputs

PinTypeDescription
Abus8First operand
Bbus8Second operand (ignored by NOT, SHL, SHR)
Opcodebus8Operation code (0–7)

Outputs

PinTypeDescription
Resultbus88-bit operation result
Zerobit1 if Result = 0, otherwise 0. Used for conditional jumps (JZ).

How It Works

The ALU is a calculator with a mode dial: feed it two bytes A and B, set the operation code on the Opcode input — and the answer appears on the Result output. Inside hides a beautiful trick: there is no real "mode switching" at all. The adder and all the logic blocks compute their results simultaneously and continuously, and a cascade of multiplexers, driven by the opcode bits, simply picks whose result is shown on the output right now.

The key property: eight operations in one chip. A code of 0–7 on the Opcode input makes the ALU add, subtract, AND, OR, XOR, invert or shift. Take A = 00001010 (10) and B = 00000110 (6): opcode 0 (ADD) gives 00010000 (16), opcode 2 (AND) — 00000010 (2), and opcode 7 (SHR) — 00000101 (5). The circuit does not reconfigure — only the selected wire changes.

A nuance: the NOT, SHL and SHR operations ignore the B input — they only need A. The whole circuit is combinational, with no clock: as soon as the inputs change, the result updates immediately. Next to Result lives the Zero flag: it equals 1 when the result is zero, and it is exactly how the processor learns that a subtraction produced zero.

Examples

A (dec)B (dec)OpcodeOperationResultZero
1060ADD00010000 (16)0
1061SUB00000100 (4)0
661SUB00000000 (0)1
1062AND00000010 (2)0
1063OR00001110 (14)0
10—5NOT11110101 (245)0
10—7SHR00000101 (5)0

In the third row subtracting a number from itself gave zero — and the Zero flag jumped to 1. This is how the processor compares values. For NOT and SHR the value of B does not matter: those operations only read A.

Operation Table

The ALU8 performs one of 8 operations selected by the Opcode input (values 0–7):

OpcodeOperationDescription
0ADDResult = A + B (mod 256)
1SUBResult = A − B (mod 256)
2ANDResult = A & B (bitwise)
3ORResult = A | B (bitwise)
4XORResult = A ^ B (bitwise)
5NOTResult = ~A (bitwise, B is ignored)
6SHLResult = A << 1 (left shift by 1, B is ignored)
7SHRResult = A >> 1 (right shift by 1, B is ignored)

The Zero flag outputs 1 when Result equals 0. This flag is critical for the CPU's conditional jump instruction (JZ): when the ALU produces a zero result, the processor can branch to a different ROM address.

Usage

On level 13 you build the operation selector yourself: a cascade of multiplexers that picks between the adder's result and the logic blocks' results using the opcode bits. In the finished computer (levels 13–18 and 25) the ALU is the heart of the action: the accumulator feeds input A, RAM feeds input B, the instruction decoder sets the Opcode, and the result returns to the accumulator or to memory.

Concrete tricks: comparing two numbers is a SUB plus a glance at the Zero flag (zero means equal); the JZ instruction branches on exactly that flag; multiplying and dividing by 2 are replaced with the SHL and SHR shifts; masking individual bits goes through AND and OR. In effect, every "smart" instruction of the processor boils down to one of the ALU's eight operations.

Build it yourself →

Related components

Related articles

Frequently Asked Questions

What can the ALU8 do?

Eight operations — ADD, SUB, AND, OR, XOR, NOT, SHL and SHR — selected by the operation code input.

How does the ALU pick an operation?

All the compute blocks work in parallel all the time, and a cascade of multiplexers connects exactly one wire — the one where the answer is already ready — to the Result output, driven by the opcode bits.

What is the Zero flag for?

It equals 1 when the result is zero. The JZ instruction branches on this flag: subtract one value from another and check Zero — that is how the processor compares numbers and builds loops.