15.1 ALU: The Arithmetic Logic Unit
Describe the ALU's role as the computational core of a CPU, list its core operations, and explain how it produces both a result and status flags from two operands and a control signal.
The Arithmetic Logic Unit (ALU) is the combinational circuit that actually performs computation inside a CPU — every add, subtract, comparison, and bitwise operation a processor executes ultimately passes through an ALU. It takes two operands and a control signal that selects which operation to perform, and produces both a numeric or logical result and a set of status flags describing properties of that result (such as whether it was zero, negative, or produced a carry). Understanding the ALU is the gateway to understanding how a CPU is actually built: every arithmetic circuit, shifter, and comparator covered in this module is either a component of the ALU or a peer block that shares its role in the larger datapath (Section 15.7).
Core ALU Operations
An ALU is built to perform a fixed repertoire of operations, selected by a control input rather than hard-wired to just one. The standard operation set includes ADD and SUB (the arithmetic operations, elaborated in Section 15.2), the bitwise logic operations AND, OR, and XOR (elaborated in Section 15.3), COMPARE (determining relationships like equal-to, greater-than, or less-than between the two operands), and SHIFT (repositioning bits within an operand, elaborated in Section 15.4). Each of these is implemented as its own dedicated sub-circuit inside the ALU, and a multiplexer selects which sub-circuit's output actually reaches the ALU's result output, based on the operation-select control signal.
This design — many parallel sub-circuits feeding a single output multiplexer — is a deliberate trade-off: it costs more silicon area than building one reconfigurable circuit that somehow does everything, but it lets every operation's dedicated hardware run at full speed with no shared-resource contention, and it keeps each sub-circuit's design simple and independently verifiable.
• ADD, SUB — arithmetic operations (Section 15.2). • AND, OR, XOR — bitwise logic operations (Section 15.3). • COMPARE — determines equality/magnitude relationships between operands. • SHIFT — repositions bits within an operand (Section 15.4). • A control signal (opcode/function select) chooses which operation's result reaches the ALU output.
Operands In, Result and Flags Out
Conceptually, an ALU's data flow is simple: Operands flow in, the ALU processes them according to the selected operation, and Result + Status Flags flow out. The two operands are typically both taken from CPU registers (via the register file, Section 15.6) or one operand may come from a register and the other from an immediate value encoded directly in the instruction. The selected operation determines how those operands combine, and the numeric or logical outcome becomes the ALU's primary result output, which is then typically written back to a destination register or used to compute a memory address.
Status flags are a second, equally important output alongside the numeric result: a Zero flag indicates the result was exactly zero (crucial for conditional branch instructions like 'branch if equal'), a Carry flag indicates an addition produced a carry-out beyond the operand width (or a subtraction required a borrow), an Overflow flag indicates a signed arithmetic operation produced a result outside the representable signed range, and a Negative/Sign flag mirrors the result's most significant bit. These flags let the rest of the CPU — particularly its branch and conditional logic — make decisions based on the ALU's most recent computation without needing to re-inspect the full result value itself.
• Operands → ALU → Result + Status Flags — the ALU's fundamental data flow. • Zero flag: result equals zero (used for equality branches). • Carry flag: unsigned carry-out (addition) or borrow (subtraction). • Overflow flag: signed arithmetic result exceeded the representable range. • Negative/Sign flag: mirrors the result's most significant bit.
Practical and Real-World Applications
Every instruction a CPU executes that does arithmetic, logic, or comparison — from a high-level language's '+' operator down to a compiled ADD instruction — ultimately routes through the ALU. Conditional branches ('if', 'while', loop bounds checks) depend on ALU status flags set by a preceding compare or arithmetic instruction, which is why the ALU sits at the very center of a CPU's execute stage in a classic datapath, feeding directly into both the register file write-back path and the branch-decision logic that determines what instruction executes next.
ALU operation set and data flow
ALUOp (the control signal) is typically decoded from the instruction's opcode/function field by the CPU's control unit, not chosen directly by the programmer. Status flags are usually latched into a dedicated flags/status register that persists until the next flag-setting operation.
Operations: ADD, SUB, AND, OR, XOR, COMPARE, SHIFT
Operand A Operand B
│ │
└──────┬──────┘
▼
┌─────────┐
ALUOp─►│ ALU │
└─────────┘
│
┌───────┴───────┐
▼ ▼
Result Status Flags
(Zero, Carry, Overflow, Negative)Code Examples
With ALUOp set to ADD, the ALU's adder sub-circuit computes 13 + 5 = 18 and that path is selected by the output multiplexer. Alongside the numeric result, the ALU evaluates each status condition: since the result is nonzero, positive, within range, and produced no carry out of the top bit, all four flags come out as 0 (false) in this case. Expected result: Result = 18 (0001_0010), all flags = 0
ALUOp selects which sub-circuit's result reaches the output; flags are computed alongside it, not instead of it.
ALUOp = ADD
Operand A = 0000_1101 (13)
Operand B = 0000_0101 (5)
Result = 0001_0010 (18)
Flags: Zero = 0 (result is nonzero)
Carry = 0 (no carry out of bit 7)
Overflow = 0 (result fits in signed range)
Negative = 0 (MSB of result is 0)ALU as CPU execute-stage core
The ALU's essential role: two operands and an operation-select signal in, one numeric result and a set of status flags out.
Operands
↓
ALU
↓
Result + Status FlagsImportant Rules
An ALU's operation set includes ADD, SUB, AND, OR, XOR, COMPARE, and SHIFT.
A control signal (ALUOp) selects which internal sub-circuit's output becomes the ALU's result.
The ALU always produces both a Result and a set of Status Flags (Zero, Carry, Overflow, Negative) from every operation.
Status flags feed the CPU's branch/condition logic independently of the numeric result value itself.
Where the ALU shows up
Every general-purpose CPU, microcontroller, and GPU core contains one or more ALUs as the computational heart of its execute stage; compiled arithmetic, comparisons, and the condition checks behind every 'if' and loop in a high-level program ultimately reduce to ALU operations and the flags they set.
Treating the ALU's 'result' as its only output, and forgetting that status flags are computed and available every cycle as well.
Remember the ALU always produces Result + Status Flags together — conditional branches and carry-dependent arithmetic (like multi-word addition) depend on those flags being available every cycle, not just the numeric result.
The ALU's fundamental data flow is Operands → ALU → Result + Status Flags; omitting the flags misses half of what the ALU actually contributes to a working CPU, since branch instructions and multi-precision arithmetic rely directly on them.
What two categories of output does an ALU produce for every operation? A. A result and a clock signal B. A result and a set of status flags C. Two separate numeric results D. An opcode and a memory address
B. A result and a set of status flags — The ALU's fundamental data flow is Operands → ALU → Result + Status Flags. Alongside the numeric or logical result, it always produces status flags such as Zero, Carry, Overflow, and Negative that describe properties of that result.
Which of the following is NOT part of the standard ALU operation set covered in this course? A. ADD B. XOR C. COMPARE D. MULTIPLY
D. MULTIPLY — The ALU's core operations are ADD, SUB, AND, OR, XOR, COMPARE, and SHIFT. Multiplication is typically handled by a separate, dedicated multiplier circuit (Section 15.5) rather than being part of the basic ALU operation set, since multiplication hardware is significantly more complex than the ALU's core operations.
An 8-bit ALU computes SUB with Operand A = 0000_0011 (3) and Operand B = 0000_0101 (5). What should the Zero and Negative flags read for an unsigned-vs-signed interpretation, and why does subtraction typically use a Carry/Borrow flag to signal an unsigned underflow?
Result = 3 − 5 = −2, which in 8-bit two's complement is 1111_1110. Zero flag = 0 (result isn't zero); Negative flag = 1 (MSB is 1, signed result is negative). Because subtraction is commonly implemented as A + (two's complement of B), a Carry-out of 0 from that addition signals that a borrow was needed — i.e., unsigned A was less than unsigned B — which is exactly the case here (3 < 5).
3 − 5 is negative in a signed interpretation; think about what happens when a subtraction 'runs out' of magnitude in unsigned arithmetic and how that maps to the Carry flag using two's-complement subtraction (A + ~B + 1).