COA Notes
ALU design, structure, operation selection, status flags, and integration with the CPU datapath.
Introduction
The ALU is the computational heart of the CPU — it's where all the actual "work" of computing happens. Every addition, comparison, logical operation, and shift that your processor performs goes through the ALU. Despite being responsible for all computation, the ALU itself is a surprisingly simple combinational circuit (no memory, no state) that takes inputs, performs an operation, and produces outputs — all within a single clock cycle.
What the ALU Does
The ALU performs three categories of operations:
- Arithmetic: Addition, subtraction, increment, decrement, negation
- Logic: AND, OR, XOR, NOT
- Shift: Left shift, right shift, rotate (sometimes in a separate shift unit)
It also produces status flags that indicate properties of the result (zero, negative, overflow, carry).
ALU Block Diagram
Inputs:
- A: First operand (from register)
- B: Second operand (from register or immediate)
- Operation Select: Which operation to perform (k bits encode 2^k operations)
- Carry In (Cin): For multi-word arithmetic and subtract operations
Outputs:
- F (Result): The computed result (n bits)
- Status Flags: Carry (C), Zero (Z), Negative (N), Overflow (V)
- Carry Out (Cout): For multi-word arithmetic
Internal Structure
A typical ALU combines the arithmetic unit, logic unit, and shifter:
| Arithmetic | Logic | Shifter | ||
|---|---|---|---|---|
| Unit | Unit |
The output multiplexer, controlled by the operation select lines, chooses which sub-unit's result becomes the final output.
Operation Encoding
A typical ALU with 3-bit operation select (8 operations):
| S2 | S1 | S0 | Operation | Description |
|---|---|---|---|---|
| 0 | 0 | 0 | A + B | Addition |
| 0 | 0 | 1 | A - B | Subtraction |
| 0 | 1 | 0 | A + 1 | Increment A |
| 0 | 1 | 1 | A - 1 | Decrement A |
| 1 | 0 | 0 | A AND B | Bitwise AND |
| 1 | 0 | 1 | A OR B | Bitwise OR |
| 1 | 1 | 0 | A XOR B | Bitwise XOR |
| 1 | 1 | 1 | NOT A | Complement |
Real processor ALUs support more operations with more select bits (4-6 bits for 16-64 operations).
Status Flags (Condition Codes)
After every ALU operation, flags are updated in the Processor Status Register (PSR):
Carry Flag (C)
- Set when arithmetic produces a carry (or borrow) out of the MSB
- Indicates unsigned overflow
- Used for multi-precision arithmetic (carry chain across multiple words)
Zero Flag (Z)
- Set when the result is exactly zero (all bits are 0)
- Hardware: Z = NOR of all result bits (Z=1 only if every bit is 0)
- Used for equality comparisons
Negative Flag (N)
- Copy of the MSB of the result
- In two's complement, MSB=1 means negative
- Hardware: N = Result[n-1]
Overflow Flag (V)
- Set when signed arithmetic produces an incorrect result
- V = Carry_into_MSB ⊕ Carry_out_of_MSB
- Example: 0111 + 0001 = 1000 (7+1=-8 in 4-bit signed — overflow!)
Flag Generation Hardware
ALU in the CPU Datapath
The ALU sits at the center of the CPU's data path:
In one clock cycle (for register-register operations):
- Register file provides operands A and B
- ALU computes result based on operation select
- Result is written back to the register file
- Flags are updated
Design Trade-offs
Speed vs Area
- Ripple-carry adder: Small but slow (carry propagates through all bits)
- Carry-lookahead adder: Fast but large (computes all carries in parallel)
- Carry-select adder: Moderate speed and size (computes both carry possibilities)
Bit Width
- Wider ALU = more bits processed per cycle = faster for large numbers
- But wider = more hardware = more power consumption
- Modern CPUs: 64-bit ALU standard; GPUs may have many smaller ALUs
Dedicated vs Shared
- Simple: One ALU handles everything sequentially
- Advanced: Separate units for integer, floating-point, SIMD, with their own ALUs
- Superscalar: Multiple ALUs executing instructions simultaneously
Key Takeaways
- The ALU is a combinational circuit that performs arithmetic, logic, and shift operations
- Operation select lines (from the control unit) determine which operation is performed
- The ALU produces both a result and status flags (C, Z, N, V) that record result properties
- Internally, separate sub-circuits handle arithmetic, logic, and shifts, with a MUX selecting the output
- Flags enable conditional branching — "branch if zero," "branch if negative," etc.
- ALU speed is largely determined by the adder design (ripple-carry vs carry-lookahead)
- Modern CPUs have multiple ALUs operating in parallel for superscalar execution
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Arithmetic Logic Unit (ALU).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, register, transfer, and, microoperations
Related Computer Organization & Architecture Topics