Sub topic 9
Inside the ALU: operations and structure
This chapter assumes you know two's complement representation, basic gates, multiplexers, and the idea of registers feeding a datapath. It covers what an arithmetic logic unit (ALU) computes, how it is built from one-bit slices, and the errors GATE questions exploit.
What the ALU does
The ALU is the combinational circuit that performs arithmetic and logic on operands. It has no memory of its own. Its inputs are two operands (A and B), a function-select code from the control unit, and sometimes a carry-in. Its outputs are a result and status bits (flags). The result is usually latched into a register such as the accumulator or a general register at the end of a clock cycle.
Because the ALU is combinational, its output settles after a propagation delay. The clock period must be longer than the register delay, plus the ALU delay, plus setup time. The ALU is often on the critical path, so its delay affects the processor's clock rate.
Typical operations fall into three groups:
| Group | Examples | Notes |
|---|---|---|
| Arithmetic | add, subtract, increment, decrement, negate | Built around one adder |
| Logic | AND, OR, XOR, NOT | Bitwise, with no carries between bit positions |
| Shift | logical, arithmetic, rotate | Often a separate shifter or barrel shifter |
One-bit slice structure
An n-bit ALU is n copies of a one-bit slice. Each slice contains:
- A full adder for bit i.
- Gates that compute AND, OR, and XOR of A[i] and B[i].
- A multiplexer that selects which of these results appears at the output F[i].
The carry-out of slice i connects to the carry-in of slice i+1. Logic operations ignore the carry, and arithmetic operations use it.
The full adder computes, for each bit position:
- sum = A xor B xor Cin
- Cout = A·B + Cin·(A xor B)
Subtraction with the same adder
The ALU does not need a separate subtractor. Since A − B = A + (~B) + 1 in two's complement, a control line named SUB does two things:
- It XORs every bit of B with SUB. When SUB = 0, B passes unchanged. When SUB = 1, B is inverted.
- It feeds SUB into the first slice's carry-in, which supplies the "+1".
This adds one XOR gate per bit and reuses the adder. Increment is the same adder with B = 0 and Cin = 1.
def alu(a, b, op, bits=8):
"""Simple n-bit ALU. Returns (result, carry, overflow)."""
mask = (1 << bits) - 1
sign = 1 << (bits - 1)
a &= mask
b &= mask
if op in ("add", "sub"):
sub = 1 if op == "sub" else 0
bb = (b ^ mask) if sub else b # XOR B with SUB
total = a + bb + sub # SUB is also carry-in
result = total & mask
carry = total >> bits
overflow = int(((a ^ result) & (bb ^ result) & sign) != 0)
return result, carry, overflow
if op == "and":
return a & b, 0, 0
if op == "or":
return a | b, 0, 0
if op == "xor":
return a ^ b, 0, 0
raise ValueError(op)
print(alu(5, 3, "sub")) # (2, 1, 0)
print(alu(3, 5, "sub")) # (254, 0, 0)
print(alu(127, 1, "add")) # (128, 0, 1)
print(alu(0x80, 1, "sub")) # (127, 1, 1)The overflow test works for add and sub alike. It checks that the two operands seen by the adder (A and the possibly inverted B) share a sign that differs from the result's sign.
Carry flag in subtraction
On many architectures (ARM, for example) the carry flag after a subtraction means "no borrow". In the example alu(5, 3, "sub") the carry is 1, because 5 ≥ 3 and no borrow occurred. Other architectures (x86) store the borrow itself, which is the inverse. Check which convention a question states. When it says nothing, derive the flag from the adder's carry-out as above.
Shifts
Shifts move bits within a word and are built from multiplexers.
- Logical shift left or right fills vacated positions with 0.
- Arithmetic shift right copies the sign bit into vacated positions. This divides a two's complement number by 2 and rounds toward negative infinity.
- Arithmetic shift left by one multiplies by 2 and can overflow, which is detected when the sign bit changes.
- Rotate moves the bit shifted out into the vacated position.
A barrel shifter shifts by any amount in one operation. It uses log2(n) multiplexer stages, where stage k shifts by 2^k or passes the value through. A 32-bit barrel shifter therefore needs 5 stages with 32 two-input multiplexers each, 160 in total.
Delay and the carry chain
In a ripple-carry arrangement, slice i must wait for the carry from slice i−1. The worst-case delay therefore grows linearly with n. If each carry stage takes tc, the delay for the final carry is about n·tc. Later chapters on adders show how carry-lookahead reduces this. For the ALU, the point is that the arithmetic path is far slower than the logic path, and the clock period is set by the arithmetic path.
Flags produced by the ALU
The ALU typically reports four flags. Detailed use of these flags comes in a later chapter. For now, here is where each comes from.
| Flag | Source |
|---|---|
| Zero (Z) | NOR of all result bits |
| Negative (N) | Most significant bit of the result |
| Carry (C) | Carry-out of the top slice |
| Overflow (V) | Carry into the top slice XOR carry out of it |
The last row gives a hardware shortcut. Signed overflow equals C(n−1) xor C(n), where C(n−1) is the carry into the sign position and C(n) is the carry out of it.
Common errors
- Treating carry and overflow as the same thing. Carry concerns unsigned results and overflow concerns signed ones.
0xFF + 0x01carries but does not overflow. - Forgetting that subtraction adds the carry-in of 1. Inverting B alone gives A − B − 1.
- Expecting overflow when adding operands with opposite signs. This cannot overflow.
- Using an arithmetic shift for unsigned values, which corrupts values with the top bit set.
- Counting the clock period from the ALU alone. Register clock-to-output and setup times also count.
Exercise
An 8-bit ALU computes A − B with A = 0x50 and B = 0xB0, using the invert-and-add method. Give the result, the carry-out, and the overflow flag. Interpret the operands as signed numbers.
Show answer
Invert B: ~0xB0 = 0x4F. Add A, the inverted B, and carry-in 1: 0x50 + 0x4F + 1 = 0xA0 (160). The 8-bit result is 0xA0 and there is no carry-out, so the carry-out is 0. For overflow, A = 0x50 is positive (+80), and the inverted B (0x4F) is positive, but the result 0xA0 has its sign bit set. The operands as seen by the adder share a sign and the result differs, so overflow = 1. Check: 0xB0 is −80, so 80 − (−80) = 160, which exceeds +127.
Exercise
A 32-bit barrel shifter is built from 2-to-1 multiplexers arranged in stages. How many multiplexers does it need?
Show answer
Shifts of 0 to 31 need 5 stages (shift by 1, 2, 4, 8, 16). Each stage has 32 two-input multiplexers, so the total is 5 × 32 = 160.