The ALU Flags
This article completes Section 2.2 on ALU design.
Every conditional statement in software (such as if (A > B)) evaluates processor condition flags.
When a microprocessor executes a comparison instruction (CMP A, B), it computes $A - B$, evaluates four 1-bit status flags, and discards the numerical result.
This article analyzes the hardware generation of the Zero ($Z$), Negative ($N$), Carry ($C$), and Overflow ($V$) flags and derives their Boolean branch condition equations.
Role of Condition Flags in Microprocessors
An Arithmetic Logic Unit produces two parallel outputs:
- An $N$-bit data result.
- A 4-bit status vector ($Z, N, C, V$).
Processor execution pipelines store these flag bits in a dedicated status register:
- ARM Architecture: Current Program Status Register (CPSR).
- x86 Architecture: EFLAGS / RFLAGS register.
Figure 1 shows the signal flow from the ALU to the branch evaluation logic:
Branch execution units evaluate combinational logic equations on these four bits to resolve program control flow.
Hardware Implementation of the Primary Flags
1. Zero Flag ($Z$)
The Zero flag asserts high when every bit of the output result vector equals 0:
$$Z = \overline{R_{N-1} + R_{N-2} + \dots + R_1 + R_0}$$
Hardware implementation requires an $N$-input NOR reduction tree. For a 32-bit datapath, the tree requires five logic levels. The Zero flag indicates arithmetic equality ($A = B$) during comparison operations.
2. Negative Flag ($N$)
The Negative flag connects directly to the most significant bit (MSB) of the result:
$$N = R_{N-1}$$
In two’s complement arithmetic, $R_{N-1} = 1$ denotes a negative quantity. In unsigned arithmetic, $R_{N-1}$ represents a positive magnitude weight ($2^{N-1}$). The Negative flag is valid only when interpreting results as signed numbers.
3. Carry Flag ($C$)
The Carry flag connects to the carry-out line of the most significant adder stage:
$$C = C_{\text{out}}$$
- During Addition ($A + B$): $C = 1$ indicates unsigned arithmetic overflow ($A + B \ge 2^N$).
- During Subtraction ($A - B$): Under the standard addition identity ($A + \overline{B} + 1$), $C = 1$ indicates no borrow occurred ($A \ge B$ unsigned). $C = 0$ indicates that an unsigned borrow occurred ($A < B$).
Instruction set architectures handle subtraction carry polarities differently:
- ARM: Retains $C = 1$ as “No Borrow” ($A \ge B$).
- x86: Inverts the bit into a dedicated Borrow Flag ($CF = 1$ indicates a borrow occurred).
4. Overflow Flag ($V$)
The Signed Overflow flag detects when an arithmetic result exceeds the representable range of an $N$-bit signed integer ($-2^{N-1}$ to $+2^{N-1} - 1$):
$$V = c_{N-2} \oplus C_{\text{out}}$$
An XOR gate compares the carry entering the most significant full adder stage ($c_{N-2}$) with the carry leaving it ($C_{\text{out}}$). Signed overflow occurs when two positive operands produce a negative result pattern, or when two negative operands produce a positive result pattern.
Validity of Flags Across ALU Operations
Table 1 summarizes flag validity across different ALU operational modes:
| Operation Class | Zero Flag ($Z$) | Negative Flag ($N$) | Carry Flag ($C$) | Overflow Flag ($V$) |
|---|---|---|---|---|
Addition (ADD) |
Valid | Valid | Unsigned overflow | Signed overflow |
Subtraction (SUB / CMP) |
Valid | Valid | No-Borrow ($A \ge B$) | Signed overflow |
Bitwise Logic (AND, OR, XOR) |
Valid | Valid | Undefined / Unaffected | Undefined / Cleared to 0 |
Pass-Through (PASSA, PASSB) |
Valid | Valid | Undefined / Unaffected | Undefined / Cleared to 0 |
Architecture Case Studies: ARM CPSR Versus x86 EFLAGS
ARM Architecture
In the ARM instruction set, bits 31 through 28 of the CPSR register hold flags $N$, $Z$, $C$, and $V$.
Standard data processing instructions update flags only when specified with an S suffix (such as ADDS or SUBS).
Comparison instructions (CMP, CMN, TST, TEQ) always update flags without writing to destination registers.
x86 Architecture
In x86 processors, the EFLAGS register stores $CF$ (bit 0), $ZF$ (bit 6), $SF$ (bit 7, equivalent to $N$), and $OF$ (bit 11, equivalent to $V$).
Most arithmetic and logical instructions update condition flags automatically.
The CMP instruction executes subtraction without write-back, while TEST executes bitwise AND without write-back.
Mathematical Derivation of Comparison Conditions
A comparison instruction evaluates $A - B$ to set flags $Z, N, C, V$.
Unsigned Comparisons
Unsigned magnitude comparison relies on flags $C$ and $Z$:
- Unsigned Greater Than ($A > B$): Requires no borrow ($C = 1$) AND non-zero difference ($Z = 0$): $$\text{HI} = C \cdot \overline{Z}$$
- Unsigned Greater Than or Equal ($A \ge B$): Requires no borrow ($C = 1$): $$\text{HS} = C$$
- Unsigned Less Than ($A < B$): Requires an unsigned borrow ($C = 0$): $$\text{LO} = \overline{C}$$
- Unsigned Less Than or Equal ($A \le B$): Requires a borrow OR a zero result: $$\text{LS} = \overline{C} + Z$$
Signed Comparisons and Overflow Inversion
Signed comparisons must account for the Overflow flag ($V$). If signed overflow occurs ($V = 1$), the sign bit ($N$) inverts:
- If No Overflow Occurs ($V = 0$): The sign bit is mathematically valid. $A - B \ge 0 \iff N = 0$.
- If Overflow Occurs ($V = 1$): The sign bit is inverted. A true positive result wraps to a negative bit pattern ($N = 1$). Therefore, $A - B \ge 0 \iff N = 1$.
In both cases, condition $A \ge B$ holds if and only if $N$ equals $V$:
$$\text{Signed } (A \ge B) \iff N = V$$
Similarly, signed strict inequality requires a non-zero result:
$$\text{Signed } (A > B) \iff \overline{Z} \cdot (N \odot V)$$
$$\text{Signed } (A < B) \iff N \oplus V$$
Complete ARM Condition Code Mapping
Table 2 lists standard condition codes evaluated by branch execution logic:
| Condition Mnemonic | Hardware Test Logic | Mathematical Comparison Meaning | High-Level Software Equivalent |
|---|---|---|---|
| EQ | $Z = 1$ | Equal | == |
| NE | $Z = 0$ | Not Equal | != |
| CS / HS | $C = 1$ | Unsigned Higher or Same | Unsigned >= |
| CC / LO | $C = 0$ | Unsigned Lower | Unsigned < |
| MI | $N = 1$ | Negative (Minus) | Signed < 0 |
| PL | $N = 0$ | Positive or Zero (Plus) | Signed >= 0 |
| VS | $V = 1$ | Overflow Set | Error Trap |
| VC | $V = 0$ | Overflow Clear | Normal Execution |
| HI | $C = 1 \ & \ Z = 0$ | Unsigned Higher | Unsigned > |
| LS | $C = 0 \ | \ Z = 1$ | Unsigned Lower or Same | Unsigned <= |
| GE | $N = V$ | Signed Greater Than or Equal | Signed >= |
| LT | $N \ne V$ | Signed Less Than | Signed < |
| GT | $Z = 0 \ & \ N = V$ | Signed Greater Than | Signed > |
| LE | $Z = 1 \ | \ N \ne V$ | Signed Less Than or Equal | Signed <= |
| AL | True | Always Executed | Unconditional Jump |
A branch execution unit implements each condition using two to three logic gates connected to status register outputs.
Hardware Summary
Table 3 summarizes the logic expressions and hardware delays for the condition flags:
| Flag Identifier | Logic Equation | Implementation Hardware | Propagation Delay After Result |
|---|---|---|---|
| Zero ($Z$) | $\overline{\sum_{i=0}^{N-1} R_i}$ | $N$-input NOR reduction tree | $\lceil \log_2 N \rceil$ gate levels |
| Negative ($N$) | $R_{N-1}$ | Direct wire connection | 0 gate delays |
| Carry ($C$) | $C_{\text{out}}$ | Direct wire from adder MSB | 0 gate delays |
| Overflow ($V$) | $c_{N-2} \oplus C_{\text{out}}$ | Single 2-input XOR gate | 1 gate delay |
Practice Exercises
Problem 1: A 4-bit ALU receives operands $A = 1100_2$ and $B = 0101_2$.
- Interpret $A$ and $B$ as unsigned integers and as signed two’s complement integers.
- Trace subtraction ($A - B$) and determine output $R[3:0]$ and flags $Z, N, C, V$.
- Evaluate unsigned condition
HI($A > B$) and signed conditionGT($A > B$).
View Solution
Step 1: Operand Interpretation:
- Unsigned: $A = 12_{10}, B = 5_{10}$.
- Signed: $A = -4_{10}, B = +5_{10}$.
Step 2: Subtraction Trace: Compute $1100_2 + \overline{0101_2} + 1 = 1100_2 + 1010_2 + 1_2$:
- Bit 0: $0 + 0 + 1 \implies S_0 = 1, C_1 = 0$.
- Bit 1: $0 + 1 + 0 \implies S_1 = 1, C_2 = 0$.
- Bit 2: $1 + 0 + 0 \implies S_2 = 1, C_3 = 0$.
- Bit 3: $1 + 1 + 0 \implies S_3 = 0, C_4 = 1$.
- Result: $R = 0111_2$.
- Flags:
- $Z = 0$ (non-zero result).
- $N = R_3 = 0$.
- $C = C_4 = 1$ (no unsigned borrow occurred).
- $V = c_2 \oplus C_4 = 0 \oplus 1 = 1$ (signed overflow occurred).
Step 3: Condition Evaluation:
- Unsigned Comparison (
HI: $C = 1 \ & \ Z = 0$): $1 \cdot \overline{0} = 1 \implies A > B$ is TRUE ($12 > 5$). - Signed Comparison (
GT: $Z = 0 \ & \ N = V$): $N = 0, V = 1 \implies N \ne V \implies A > B$ is FALSE ($-4 > +5$ is false).
The condition flag logic resolves both unsigned and signed comparisons correctly.
Problem 2: Express ARM signed condition GT ($Z = 0 \ & \ N = V$) as a Boolean logic gate equation.
View Solution
Translate the algebraic conditions into logic functions:
- Condition $Z = 0 \implies \overline{Z}$.
- Condition $N = V \implies \overline{N \oplus V} = N \odot V$ (XNOR).
Combining terms with an AND operation yields:
$$\text{GT} = \overline{Z} \cdot \overline{N \oplus V}$$
The hardware implementation requires one 2-input XNOR gate, one inverter, and one 2-input AND gate.
This concludes Section 2.2 on ALU design. In Section 2.3, we design multi-bit combinational multipliers and high-speed barrel shifters.