The Adder/Subtractor Unit with Flags
In the previous article, we established that a binary adder executes two’s complement subtraction using XOR gating and carry assertion. This article completes the arithmetic core by implementing condition status flags: Zero ($Z$), Negative ($N$), Carry ($C$), and Overflow ($V$). These status flags provide condition codes that control microarchitectural branching and multi-word arithmetic chaining. We verify the module across boundary test cases and cascade two 4-bit units to perform 8-bit arithmetic.
System Interface and Block Specification
Table 1 details the signal interface for the 4-bit adder/subtractor unit:
| Signal Identifier | Bit Width | Direction | Functional Description |
|---|---|---|---|
| $A[3:0]$ | 4 | Input | First operand |
| $B[3:0]$ | 4 | Input | Second operand |
sub |
1 | Input | Mode selection: 0 = Addition ($A + B$), 1 = Subtraction ($A - B$) |
| $S[3:0]$ | 4 | Output | 4-bit arithmetic result |
| $Z$ | 1 | Output | Zero status flag (asserts when all sum bits are 0) |
| $N$ | 1 | Output | Negative status flag (reflects the two’s complement sign bit) |
| $C$ | 1 | Output | Carry status flag (unsigned overflow for addition, no-borrow for subtraction) |
| $V$ | 1 | Output | Signed overflow status flag (asserts when the signed result exceeds bounds) |
Figure 1 illustrates the complete hardware architecture:
Signal $c_3$ denotes the carry entering the most significant full adder (FA3). Signal $c_4$ denotes the carry leaving FA3.
Hardware Implementation of Status Flags
1. Zero Flag ($Z$)
The zero flag asserts high when every bit of the output vector $S[3:0]$ equals logic 0:
$$Z = \overline{S_3 + S_2 + S_1 + S_0}$$
This equation uses a 4-input NOR gate tree. In standard CMOS cell libraries, designers implement this tree using two 2-input NOR gates feeding a 2-input NAND gate.
2. Negative Flag ($N$)
In two’s complement representation, the most significant bit ($S_3$) indicates sign polarity:
- $S_3 = 0 \implies$ Non-negative
- $S_3 = 1 \implies$ Negative
$$N = S_3$$
The negative flag connects directly to output wire $S_3$ without additional logic gates.
3. Carry Flag ($C$)
The carry flag connects directly to the final carry-out line ($c_4$):
$$C = c_4$$
- In Addition Mode (
sub= 0): $C = 1$ indicates unsigned arithmetic overflow ($A + B \ge 2^4$). - In Subtraction Mode (
sub= 1): $C = 1$ indicates no borrow occurred ($A \ge B$ unsigned); $C = 0$ indicates an unsigned borrow occurred ($A < B$).
4. Overflow Flag ($V$)
The signed overflow flag detects when the sign bit fails due to internal carry mismatch:
$$V = c_3 \oplus c_4$$
An XOR gate compares the carry entering the sign bit ($c_3$) with the carry leaving the sign bit ($c_4$).
Comprehensive Verification Across Four Test Cases
Case 1: Standard Addition ($3 + 4$)
- Inputs: $A = 0011_2$ ($+3$), $B = 0100_2$ ($+4$),
sub= 0 ($C_0 = 0$). - Output: $S = 0111_2$ ($+7$).
- Internal Carries: $c_3 = 0, c_4 = 0$.
- Flags: $Z = 0, N = 0, C = 0, V = 0$.
- Evaluation: Valid positive sum; no signed or unsigned overflow occurred.
Case 2: Addition with Signed Overflow ($6 + 4$)
- Inputs: $A = 0110_2$ ($+6$), $B = 0100_2$ ($+4$),
sub= 0 ($C_0 = 0$). - Output: $S = 1010_2$.
- Internal Carries: $c_3 = 1, c_4 = 0$.
- Flags: $Z = 0, N = 1, C = 0, V = 1 \oplus 0 = 1$.
- Evaluation: The true mathematical sum is $+10_{10}$. The maximum representable 4-bit signed integer is $+7_{10}$. The pattern $1010_2$ represents $-6_{10}$ in two’s complement. The overflow flag ($V = 1$) warns that the signed result is invalid. The carry flag ($C = 0$) confirms that the unsigned value ($10 < 16$) is valid.
Case 3: Subtraction Without Borrow ($6 - 3$)
- Inputs: $A = 0110_2$ ($+6$), $B = 0011_2$ ($+3$),
sub= 1 ($C_0 = 1$). - Inverted Operand: $\overline{B} = 1100_2$.
- Output: $S = 0011_2$ ($+3$).
- Internal Carries: $c_3 = 1, c_4 = 1$.
- Flags: $Z = 0, N = 0, C = 1, V = 1 \oplus 1 = 0$.
- Evaluation: $C = 1$ indicates no unsigned borrow occurred ($6 \ge 3$). The result is correct.
Case 4: Subtraction With Borrow ($3 - 6$)
- Inputs: $A = 0011_2$ ($+3$), $B = 0110_2$ ($+6$),
sub= 1 ($C_0 = 1$). - Inverted Operand: $\overline{B} = 1001_2$.
- Output: $S = 1101_2$ ($-3$).
- Internal Carries: $c_3 = 0, c_4 = 0$.
- Flags: $Z = 0, N = 1, C = 0, V = 0 \oplus 0 = 0$.
- Evaluation: Valid signed negative result ($N = 1, V = 0$). Flag $C = 0$ indicates that an unsigned borrow occurred ($3 < 6$).
Multi-Word Cascading: Chaining Units for 8-Bit Arithmetic
To execute 8-bit arithmetic using 4-bit building blocks, connect two units in series (Figure 2):
- Connect the carry-out of the lower unit (bit 3) directly to the carry-in of the upper unit.
- Distribute the
subcontrol signal to both units in parallel. - The global status flags reflect the combined 8-bit word:
- $Z$: 8-input NOR combining all eight sum bits ($S[7:0]$).
- $N$: Bit $S_7$ of the high unit.
- $C$: Carry-out ($c_8$) of the high unit.
- $V$: Evaluated using the sign bit of the high unit: $c_7 \oplus c_8$.
Microprocessors use this architecture to execute multi-word arithmetic (such as 64-bit addition on 32-bit CPU architectures) using the Add-With-Carry (ADC) instruction.
Critical Path Timing Analysis
Table 2 lists propagation delays along the primary operational paths:
| Signal Path | Logic Elements Traversed | Total Gate Delays |
|---|---|---|
| Operand $B$ to XOR Output | 1 XOR gate | 1 |
| Carry Ripple (FA0 to FA3) | 4 full adders ($2 \times 4$) | 8 |
| Negative Flag ($N$) and Carry Flag ($C$) | Direct wire connection | 0 |
| Overflow Flag ($V$) | 1 XOR gate after $c_4$ | 1 |
| Zero Flag ($Z$) | Most significant sum XOR $\to$ 4-input NOR tree | $1 + 2 = 3$ |
| Worst-Case Critical Path ($Z$) | $\text{XOR} \to \text{Carry Chain} \to \text{Sum XOR} \to \text{NOR Tree}$ | 12 gate delays |
The Zero flag ($Z$) represents the slowest output path because the NOR reduction tree must wait for the final sum bit to stabilize.
Practice Exercises
Problem 1: Perform addition on signed operands $A = 1001_2$ ($-7_{10}$) and $B = 0110_2$ ($+6_{10}$) with sub = 0. Determine output vector $S[3:0]$ and flags $Z, N, C, V$.
View Solution
Perform addition ($1001_2 + 0110_2 + 0$):
- Bit 0: $1 + 0 + 0 \implies S_0 = 1, C_1 = 0$.
- Bit 1: $0 + 1 + 0 \implies S_1 = 1, C_2 = 0$.
- Bit 2: $0 + 1 + 0 \implies S_2 = 1, C_3 = 0$.
- Bit 3: $1 + 0 + 0 \implies S_3 = 1, C_4 = 0$.
Outputs:
- Sum output: $S = 1111_2$.
- Internal carries: $c_3 = 0, c_4 = 0$.
- Flags:
- $Z = 0$ (result is non-zero).
- $N = S_3 = 1$ (result is negative).
- $C = c_4 = 0$ (no unsigned carry generated).
- $V = c_3 \oplus c_4 = 0 \oplus 0 = 0$ (no signed overflow).
Verification:
- Signed: $-7 + 6 = -1_{10}$. In 4-bit two’s complement, $-1_{10} = 1111_2$. Valid result.
- Unsigned: $9 + 6 = 15_{10} = 1111_2$. Valid result.
Problem 2: In an 8-bit cascaded adder/subtractor, explain the source of the global Carry and Overflow flags, and clarify the role of the intermediate carry between the two 4-bit blocks.
View Solution
- Global Carry Flag ($C$): Originates exclusively from the carry-out of the most significant stage (bit 7, $c_8$). It reflects whether the full 8-bit result generated an unsigned overflow (or borrow).
- Global Overflow Flag ($V$): Originates from the XOR of the carry entering bit 7 and the carry leaving bit 7 ($V = c_7 \oplus c_8$).
- Intermediate Carry ($c_4$): The carry-out from bit 3 is an internal carry line that propagates intermediate addition states. It does not represent an arithmetic overflow and does not drive external status flags.
In the next article, we examine Arithmetic Logic Unit (ALU) design philosophy and functional operation selection.