Computer architecture
ALU (arithmetic logic unit)
Also called: ALU · arithmetic logic unit · status flags · zero flag carry flag
Definition
The ALU is the part of a processor that performs arithmetic (add, subtract) and logic (AND, OR, XOR, shifts) on values from registers, and sets status flags such as zero and carry.
Picture it
It is a calculator with an operation selector: a multiplexer chooses which result to output. Flags remember facts about the last result so that a branch instruction can ask 'was it zero?' or 'did it carry?'.
The maths
result = op(A, B) Z = 1 if result == 0 C = carry out of the top bit
| Quantity | Unit | Symbol |
|---|---|---|
| information | bit | bit |
Worked example
An 8-bit ALU computes 200 + 100. What result does it give, and are the zero and carry flags set?
- 200 + 100 = 300, which needs 9 bits
- 300 − 256 = 44 is kept
- Carry = 1; result 44 is not zero, so Z = 0
AnswerResult 44 with carry 1 and zero flag 0.
✓ The 2 numbers in this answer are recomputed from scratch by an automated test.
Common misconceptions
- Misconception: Subtraction is done by adding the two's complement, not by a separate subtractor in simple ALUs.
- Misconception: Flags are overwritten by the next operation; test them immediately.