Sub topic 9
Inside the ALU: Operations and Structure
Assume you know binary numbers, logic gates, multiplexers, registers, and memory. This chapter explains how the arithmetic logic unit (ALU) combines these parts to perform operations inside a processor.
What the ALU does
The ALU is a combinational circuit that performs arithmetic and logical operations. “Combinational” means that its output depends on the current inputs, not on stored previous values.
A typical ALU receives:
- Operand A from a register
- Operand B from a register, an immediate value, or a datapath
- Operation-control bits from the control unit
- A carry-in bit for addition or subtraction
It produces a result and sometimes status outputs such as carry, zero, negative, and overflow flags.
| Control selection | Operation | Example | Column |
|---|---|---|---|
| `000` | Add | `A + B` | |
| `001` | Subtract | `A - B` | |
| `010` | AND | `A & B` | |
| `011` | OR | `A \ | B` |
| `100` | XOR | `A ^ B` |
The exact control codes depend on the processor design.
Internal structure
An ALU commonly contains parallel hardware for several operations. Adders handle addition and subtraction. Logic gates handle AND, OR, and XOR. A multiplexer selects one operation's output according to the control bits.
For subtraction, the ALU often uses the same adder used for addition:
\[ A-B=A+\overline{B}+1 \]
Each bit of B passes through an XOR gate controlled by a subtract signal. When subtract is 1, B is complemented and the adder receives a carry-in of 1.
A multi-bit ALU can be built from one-bit slices. Each slice processes one pair of operand bits and passes a carry to the next slice. The least significant slice receives the initial carry-in.
A small software model
This Python function models common ALU operations. It masks the result to eight bits, matching an 8-bit hardware ALU.
def alu(a, b, operation):
mask = 0xFF
if operation == "ADD":
result = a + b
elif operation == "SUB":
result = a - b
elif operation == "AND":
result = a & b
elif operation == "OR":
result = a | b
elif operation == "XOR":
result = a ^ b
else:
raise ValueError("unknown operation")
return result & mask
print(alu(0b1010, 0b0011, "ADD")) # 13
print(alu(0b1010, 0b0011, "XOR")) # 9The software model hides propagation delay and hardware wiring. Real circuits must wait for signals to pass through gates before the result becomes stable.
Exercise
How can one adder perform both addition and subtraction?
Show answer
It complements the second operand when subtraction is selected and sets the adder's carry-in to 1, implementing two's complement subtraction.