Exercises — ALU — operations, flags (zero, carry, overflow, negative)
Quick reminder of the four flags before we start (each is one bit, either 0 or 1):
Here is the carry that flows into the top bit (bit 7), and is the carry that flows out of bit 7. The picture below fixes these two carries in your head — every overflow question comes back to it.

Level 1 — Recognition
Exercise 1.1 (L1)
An 8-bit ADD produces result with carry-out . State Z, N, C.
Recall Solution 1.1
- Z: result is all zeros ==Z = 1==.
- N: MSB (bit 7) of is ==N = 0==.
- C: we are told carry-out ==C = 1==. WHAT this looks like: this is exactly (unsigned wrap to 0). The result is zero, but a carry escaped the top — Z and C are answering two different questions.
Exercise 1.2 (L1)
An ADD gives . Read off N and Z directly (no arithmetic needed).
Recall Solution 1.2
- N = MSB = leftmost bit = ==N = 1==.
- Z: some bits are 1, so the result is not all-zero ==Z = 0==. WHY no arithmetic: N is literally a wire from bit 7; Z is a NOR over all bits. Neither needs to know the operands.
Level 2 — Application
Exercise 2.1 (L2)
8-bit ADD: , . Find and all four flags.
Recall Solution 2.1
Add the bits: .
- R . Check: . ✓
- Z: not zero Z = 0.
- N: MSB N = 1.
- C: carry out of bit 7? , so no C = 0.
- V: operands are pos+pos () but result MSB is (looks negative). Sign flipped V = 1. WHY V = 1 here: signed, but the max signed value is . wrapped to , which is exactly what means signed. Two positives gave a "negative" — signed overflow.
Exercise 2.2 (L2)
8-bit ADD: , . Find and flags.
Recall Solution 2.2
; keep 8 bits .
- R .
- Z: all zeros Z = 1.
- N: MSB N = 0.
- C: carry out of bit 7 C = 1.
- V: signs neg+neg () but result sign is (positive). Flip V = 1. WHY: signed, , far below . The signed answer is nonsense; V flags it. Notice C = 1 and V = 1 here — both fired.
Level 3 — Analysis
Exercise 3.1 (L3)
Do the subtraction with , using the ALU's real method . Give and all flags, and state the unsigned comparison result of vs from the carry flag.
Recall Solution 3.1
Step 1 — invert B: . Step 2 — add with carry-in 1:
- R . Signed value: invert-and-add-1 gives , so . Check: . ✓
- Z: not zero Z = 0.
- N: MSB N = 1.
- C: carry out of bit 7? The top bits … the full sum produced no carry out of bit 7 C = 0. On subtract, C = 0 means a borrow happened, i.e. . Indeed . ✓
- V: signs of the addition : positive, has MSB 1 (negative). Opposite signs can never overflow V = 0. Unsigned comparison: C = 0 on subtract (unsigned). . ✓
Exercise 3.2 (L3)
Same method: with , . Find , flags, and both the unsigned and signed interpretation.
Recall Solution 3.2
. Add with carry-in 1: keep 8 bits .
- R . Unsigned: ; and . ✓ Signed: ; and . ✓ (Same bits serve both readings.)
- Z: not zero Z = 0.
- N: MSB N = 1.
- C: carry out of bit 7 C = 1 no borrow unsigned. . ✓
- V: MSB 1 (neg), MSB 1 (neg)… wait, both operands of the add are negative but the result MSB is 1 (still negative) — no sign flip V = 0. Signed is in range, correct.
Level 4 — Synthesis
Exercise 4.1 (L4)
You need to branch when signed . After computing , which combination of flags implements signed strictly-greater-than? Derive it and test on , and on , .
Recall Solution 4.1
Derivation. Signed . "Greater than zero" as flags: the result must be (a) not zero and (b) have the correct sign (positive). "Correct sign positive" is read together: the true sign of is (N is the stored sign, V says whether it lied). So:
and strict adds "not equal", i.e. Z = 0:
This is exactly the ARM condition GT. See Condition Codes and Conditional Branches.
Test 1: . . N = 0, V = 0 (in range), Z = 0. ✓ and Z = 0 ✓ branch taken. Correct: . ✓
Test 2: . , out of signed range overflow. Bits: , : , add carry-in 1: . N = 0, but V = 1 (opposite signs became add of two negatives? let's read the add : both MSB 1, result MSB 0 → sign flip → V = 1). So N = 0, V = 1 signed , branch not taken. Correct: is false. ✓ (Notice: the stored result 56 looks positive, but V corrects that lie.)
Exercise 4.2 (L4)
Design the flag settings for a logical AND on typical ISAs, and explain why C and V are cleared. Apply to , .
Recall Solution 4.2
: bitwise AND .
- Z: Z = 0.
- N: MSB N = 1.
- C = 0, V = 0. WHY: AND is not arithmetic — there is no carry chain and no notion of "exceeding a numeric range". Carry and overflow only mean something for add/subtract, so ISAs define them as cleared (0) for logic ops to keep behaviour well-defined. See Combinational vs Sequential Logic for why the ALU can produce these in one pass.
Level 5 — Mastery
Exercise 5.1 (L5)
An 8-bit ADD reports flags . Reconstruct one possible pair of signed operands and their true signed sum, and explain what the stored result's sign tells you (and why it's misleading).
Recall Solution 5.1
Decode the flags. V = 1 with a positive-looking result (N = 0) means neg + neg positive (the only way a positive stored sign can overflow). C = 1 is consistent with adding two large-magnitude negatives (their unsigned patterns are big). Z = 0 means non-zero result. Construct an example: , . Hmm, that gives MSB = 1 (N = 1) — not our target. Try , :
- R : N = 0 ✓, Z = 0 ✓.
- C: carry out of bit 7 = 1 ✓.
- V: neg + neg → result sign 0 → flip → V = 1 ✓. True signed sum: , which is far below (overflow). The stored result looks positive but is completely wrong — that's precisely what V = 1 warns you about. The stored sign is meaningless after signed overflow.
Exercise 5.2 (L5)
On a CPU whose subtract sets C = NOT borrow, prove that the unsigned branch "A < B" is taken exactly when C = 0 after CMP A, B (which computes and discards the result). Then verify with and .
Recall Solution 5.2
Proof. CMP computes . The carry-out of this 9-bit sum is . Unsigned, . If then , so the sum , producing carry-out (C = 1, no borrow). If then , so the sum , giving (C = 0, borrow occurred). Therefore:
Test 1: . ; , no carry-out C = 0 . True: . ✓
Test 2: . ; , carry-out = 1 C = 1 not (). True: . ✓
Recall One-line self-test
After CMP A, B (i.e. ): unsigned A < B is ::: C = 0; signed A < B is ::: N ≠ V; equal is ::: Z = 1.
Connections
- Parent: 4.1.06 ALU — operations, flags (zero, carry, overflow, negative) (Hinglish)
- Two's Complement Representation
- Full Adder and Ripple-Carry Adder
- Condition Codes and Conditional Branches
- Status / Flags Register (PSR / EFLAGS)
- Signed vs Unsigned Comparison Instructions
- Combinational vs Sequential Logic