Sub topic 11
Status flags: zero, negative, carry and overflow
This chapter assumes you can do two's complement addition by hand and know how a ripple-carry adder passes carries from bit to bit. It also assumes you know that a register is a group of flip-flops. The goal is to know what each flag means, how the ALU computes it, and which flag answers which question.
What the status register holds
After most arithmetic and logic instructions, the ALU writes a few single-bit results into a status register, also called the flag register or program status word. Later instructions read these bits. A conditional branch such as "branch if equal" does not compare anything itself. It tests flags that an earlier instruction set.
The four flags in this chapter are:
| Flag | Name | Set to 1 when |
|---|---|---|
| Z | Zero | every bit of the result is 0 |
| N (or S) | Negative (sign) | the most significant bit of the result is 1 |
| C | Carry | the addition produced a carry out of the top bit |
| V (or O) | Overflow | the signed result does not fit in the register width |
Z and N describe the result. C and V describe whether the result is trustworthy, and they answer that question for different interpretations of the same bits.
Z and N: reading the result
Z is a NOR across all result bits. In hardware, it is a wide NOR gate on the ALU output. N is simply a copy of the top result bit. N is meaningful as "negative" only if you treat the value as signed two's complement. For unsigned data, the same bit is just a large number's top bit.
C: unsigned correctness
The adder produces a carry out of the top bit. For an n-bit unsigned addition, C = 1 means the true sum is at least 2^n, so the n-bit result is wrong by 2^n. For example, 0xFF + 0x01 gives 0x00 with C = 1, because 255 + 1 = 256 does not fit in 8 bits.
For subtraction, the ALU computes A − B as A + (NOT B) + 1. The carry out then has an inverted meaning:
- On many designs (ARM, and most textbook ALUs built this way), C = 1 means no borrow occurred, so A ≥ B unsigned.
- On x86, the hardware inverts the carry for subtraction, so CF = 1 means a borrow occurred, so A < B unsigned.
A GATE question will normally state its convention. If it does not, say which one you assume.
V: signed correctness
Overflow means the signed result is outside the range −2^(n−1) to 2^(n−1) − 1. Two equivalent rules detect it:
- For addition, the operands have the same sign and the result has a different sign.
- The carry into the top bit differs from the carry out of the top bit (V = Cin(MSB) XOR Cout(MSB)).
Adding operands of opposite signs can never overflow. For subtraction, apply rule 1 to A and the inverted B, since the ALU really adds A and NOT B.
Computing all four flags in code
This function models an 8-bit ALU that adds or subtracts and returns the flags. Subtraction is done as A + NOT B + 1, with C using the "1 means no borrow" convention.
def alu8(op, a, b):
"""8-bit add or sub. Returns (result, flags). Carry on sub: 1 = no borrow."""
if op == "add":
b_eff, cin = b, 0
elif op == "sub":
b_eff, cin = (~b) & 0xFF, 1
else:
raise ValueError(op)
total = a + b_eff + cin
result = total & 0xFF
carry = total >> 8
sign_a = (a >> 7) & 1
sign_b = (b_eff >> 7) & 1
sign_r = (result >> 7) & 1
overflow = int(sign_a == sign_b and sign_r != sign_a)
flags = {"Z": int(result == 0), "N": sign_r, "C": carry, "V": overflow}
return result, flags
print(alu8("add", 0x7F, 0x01)) # (128, {'Z': 0, 'N': 1, 'C': 0, 'V': 1})
print(alu8("add", 0xFF, 0x01)) # (0, {'Z': 1, 'N': 0, 'C': 1, 'V': 0})
print(alu8("sub", 0x05, 0x03)) # (2, {'Z': 0, 'N': 0, 'C': 1, 'V': 0})
print(alu8("sub", 0x03, 0x05)) # (254, {'Z': 0, 'N': 1, 'C': 0, 'V': 0})
print(alu8("sub", 0x80, 0x01)) # (127, {'Z': 0, 'N': 0, 'C': 1, 'V': 1})Read the results as follows.
- 0x7F + 0x01: signed 127 + 1 = 128 does not fit, so V = 1. Unsigned 127 + 1 = 128 fits, so C = 0. N = 1 is misleading here because the sign bit is wrong.
- 0xFF + 0x01: unsigned 255 + 1 overflows, so C = 1. Signed −1 + 1 = 0 is correct, so V = 0.
- 0x03 − 0x05: the result 0xFE is −2 signed, which is correct, so V = 0. Unsigned, a borrow occurred, so C = 0.
- 0x80 − 0x01: this is −128 − 1 in signed terms, which needs −129. That does not fit, so V = 1.
Using flags for comparisons
A compare instruction is a subtraction whose result is discarded and whose flags are kept. Conditional branches then combine flags:
| Condition after compare A, B | Flag test (C = 1 means no borrow) |
|---|---|
| A equals B | Z = 1 |
| A ≥ B, unsigned | C = 1 |
| A < B, unsigned | C = 0 |
| A < B, signed | N XOR V = 1 |
| A ≥ B, signed | N XOR V = 0 |
| A > B, signed | Z = 0 and N = V |
Signed less-than uses N XOR V instead of N alone because overflow flips the meaning of the sign bit. If V = 1, the true sign is the opposite of N, and XOR with V corrects it.
Which instructions affect flags
This varies by ISA, which is a common source of mistakes.
- Arithmetic instructions (add, subtract, compare, negate) usually update all four flags.
- Logic instructions (AND, OR, XOR) usually update Z and N, and often clear C and V.
- Data movement instructions may or may not update flags. Some ISAs leave flags untouched, and others set Z and N on every load.
- RISC designs such as ARM only update flags when the instruction has an "S" suffix. MIPS has no flag register at all. It uses compare-and-branch instructions and special set-on-less-than instructions instead.
Common mistakes
- Treating carry as overflow. For a signed addition, use V. For an unsigned addition, use C.
- Forgetting that subtraction adds the complement of B, so the signs compared for V are those of A and NOT B.
- Reading the sign of the result as the true sign when V = 1.
- Mixing the borrow convention between x86 and ARM-style carry on subtraction.
Exercise
An 8-bit ALU computes 0x9C + 0x74. Give the result and the flags Z, N, C and V.
Show answer
0x9C is 156 unsigned (−100 signed) and 0x74 is 116 (+116). The sum is 0x110, so the 8-bit result is 0x10 and the carry out is 1, so C = 1. Z = 0 because the result is nonzero. N = 0 because the top bit of 0x10 is 0. V = 0 because the operands have opposite signs, so signed overflow is impossible (−100 + 116 = 16 is correct). The unsigned result is wrong (156 + 116 = 272 does not fit), which C reports.
Exercise
An 8-bit ALU computes 0x50 + 0x50. State the result and flags, then say whether the result is correct as unsigned and as signed.
Show answer
0x50 + 0x50 = 0xA0 (160), so no carry out: C = 0. Z = 0. N = 1 because the top bit of 0xA0 is 1. Both operands are positive and the result looks negative, so V = 1. As unsigned, 80 + 80 = 160 fits in 8 bits, so the result is correct (C = 0). As signed, 80 + 80 = 160 exceeds 127, so the result is wrong (V = 1).