COMPUTER ORGANIZATION & ARCHITECTURE / 5. ALU — ARITHMETIC LOGIC UNIT
ALU — Arithmetic Logic Unit
The computational heart of the CPU — every operation passes through here
EXPLANATION
The ALU (Arithmetic Logic Unit) is the circuit that performs all computation in the CPU. Every ADD, SUB, AND, OR, NOT, compare, shift — everything — is done by the ALU. It is combinational logic (no memory) — output is the result of the current operation. ALU Inputs: - A — first operand (from register file) - B — second operand (from register file or immediate value) - Operation code (ALU_op) — a few bits that select which operation to perform ALU Outputs: - Result — the computed value (goes back to register file or memory) - Status flags — bits that record properties of the result: - Z (Zero flag): Result == 0 - N (Negative flag): Result < 0 (MSB is 1 in two's complement) - C (Carry flag): Unsigned overflow occurred - V (Overflow flag): Signed overflow occurred - These flags drive conditional branches (JZ, JNZ, JGT, JLT...) Two's Complement — how CPUs represent signed integers: - N-bit two's complement represents values from -2^(N-1) to 2^(N-1)-1 - Positive numbers: same as unsigned (MSB=0) - Negative numbers: flip all bits + 1 - -1 in 8-bit: 11111111 (flip 00000001 → 11111110, add 1 → 11111111) - -128 in 8-bit: 10000000 - The magic: addition and subtraction work the same circuit! A-B = A + (-B) = A + (~B + 1) - Overflow detection: carry into MSB ≠ carry out of MSB Shifter — fast multiplication/division by powers of 2: - Logical shift left (LSL): shift bits left, fill with 0. LSL by 1 = multiply by 2 - Logical shift right (LSR): shift bits right, fill with 0. LSR by 1 = divide by 2 (unsigned) - Arithmetic shift right (ASR): shift right, fill with sign bit. Preserves sign for signed division Barrel shifter: shifts by N positions in ONE clock cycle (not N separate shifts). Uses MUXes to select the correct shifted version. Multiplication: NOT done by the adder directly. Repeated addition is too slow. CPUs use Booth's algorithm (reduces partial products) or Wallace tree (parallel compression of partial products). Result is 2N bits for N×N multiplication (important for overflow). Division: even harder. Done by long division algorithm in hardware (many cycles) or by multiply-by-reciprocal approximation. Division is ~20-40× slower than addition on modern CPUs — avoid in hot loops.
DIAGRAM
ALU BLOCK DIAGRAM:
A (64-bit) B (64-bit)
│ │
┌───────────┴──────────────┴───────────┐
│ ALU │
│ ┌─────────┐ ┌─────────┐ │
│ │ Adder │ │ Logic │ │
│ │ (A+B) │ │ AND/OR/ │ │
│ │ (A-B) │ │ XOR/NOT │ │
│ └────┬────┘ └────┬────┘ │
│ │ │ │
│ ┌────┴────────────┴───┐ │
│ │ Result MUX │← ALU_op │
│ └────────────┬────────┘ │
│ │ │
│ ┌────────────┴────────┐ │
│ │ Flag Generator │ │
│ │ Z N C V │ │
│ └─────────────────────┘ │
└───────────────┬─────────────────────┘
│
Result (64-bit)
TWO'S COMPLEMENT (8-bit):
Decimal │ Binary │ Hex
────────┼───────────┼─────
127 │ 0111 1111 │ 0x7F ← max positive
1 │ 0000 0001 │ 0x01
0 │ 0000 0000 │ 0x00
-1 │ 1111 1111 │ 0xFF
-2 │ 1111 1110 │ 0xFE
-128 │ 1000 0000 │ 0x80 ← min negativeCODE