The calculator inside
11Arithmetic logic unit
- Stage 1
- Stage 2
- Stage 3
- Stage 4
- Stage 5
- Stage 6
- Stage 7
- Stage 8
- Stage 9
- Stage 10
- Stage 11 (this one)
- Stage 12
- Stage 13
- Stage 14
- Stage 15
Explanation
The ALU computes every operation at once (sum, difference, AND, OR, XOR, NOT) and a mux driven by the operation code picks which result leaves. It also sets flags: Z (result zero), C (carry out, or borrow on subtract), N (top bit set, negative if signed) and V (signed overflow).
Flags are how a program makes decisions: subtract two numbers and look at Z to see whether they were equal, or at C to see which was bigger.
Operands in, result and flags out
A = 5
B = 7
SUB: A − B, computed as A + NOT B + 1.
Y = 254 (signed -2, 0xFE)
1
1
1
1
1
1
1
0
- Z = 0
- C = 1
- N = 1
- V = 0
Z zero · C carry (ADD) or borrow (SUB) · N top bit set · V signed overflow
SUB gives 254; flags Z 0 C 1 N 1 V 0
Check yourself
Marked instantly in your browser. Use the interactive above to work it out.