Status Registers: Zero, Carry, and Overflow Flags
A status register, often called a flags register or condition code register, is a dedicated hardware register in a central processing unit (CPU) that records the outcome of arithmetic, logical, and comparison operations. This article explores the architecture of the status register and details how three critical flag bits—the Zero Flag (Z), the Carry Flag (C), and the Overflow Flag (V)—interpret and report state changes in binary arithmetic.
What Is a Status Register?
A status register consists of individual, independent 1-bit memory cells known as flags. Unlike general-purpose registers that hold data values or memory addresses, the status register holds metadata about the most recently executed instruction in the Arithmetic Logic Unit (ALU).
Each bit within the register is automatically updated by the CPU
hardware based on predefined criteria. The processor then evaluates
these bits to make decisions, such as executing conditional branch
instructions (JUMP IF ZERO,
BRANCH IF GREATER THAN), enabling loops, and handling
multi-word arithmetic.
The Zero Flag (Z)
The Zero Flag indicates whether the result of the previous operation evaluated to zero.
Binary Reporting:
- The Z flag is set to
1(true) if every bit of the resulting binary word is0. - The Z flag is cleared to
0(false) if at least one bit in the result is1.
- The Z flag is set to
Operational Example: In an 8-bit register, computing
00000101 (5)minus00000101 (5)yields00000000 (0). The ALU detects that all output lines are low and assertsZ = 1.Primary Use: The Zero Flag is the primary mechanism for testing equality. When a CPU executes a compare instruction (
CMP A, B), it internally computesA - Bwithout altering the values inAorB. IfAandBare identical, the result is zero, settingZ = 1.
The Carry Flag (C)
The Carry Flag monitors carry-out and borrow operations during unsigned binary arithmetic. It tracks whether an operation has exceeded the fixed bit-width of the target register.
Binary Reporting:
- In addition, the C flag is set to
1if the addition of the Most Significant Bits (MSBs) produces a carry-out that cannot fit into the result register. - In subtraction, the C flag is typically set to
1(or acts as a borrow flag) if the minuend is smaller than the subtrahend, requiring a borrow from beyond the MSB.
- In addition, the C flag is set to
Operational Example: Using an 8-bit unsigned system (range: 0 to 255):
11111111 (255) + 00000001 (1) ---------- (1)00000000 (Result: 0, Carry: 1)The 8-bit register stores
00000000, and the CPU setsC = 1to indicate that the true arithmetic result required a 9th bit.Primary Use: The Carry Flag allows processors to chain multiple smaller registers together to execute arbitrary-precision arithmetic (e.g., adding two 64-bit integers on a 32-bit architecture via “Add with Carry” instructions).
The Overflow Flag (V or OF)
The Overflow Flag monitors boundary violations during signed binary arithmetic, which is represented using Two’s Complement notation. It alerts the system that an operation has produced a result with an incorrect mathematical sign because the value fell outside the representable signed range.
Binary Reporting:
- The V flag is set to
1if an addition or subtraction yields a result that exceeds the maximum positive or minimum negative value for the given bit width. - In hardware logic, the V flag is generated using an Exclusive-OR (XOR) operation between the carry coming into the MSB and the carry going out of the MSB: \[\text{Overflow} = C_{\text{in to MSB}} \oplus C_{\text{out of MSB}}\]
- The V flag is set to
Operational Example: Using an 8-bit signed system (range: -128 to +127):
01111111 (+127) + 00000001 (+1) ---------- 10000000 (-128 in Two's Complement)Adding two positive numbers produced a negative result (indicated by the MSB sign bit becoming
1). The CPU recognizes this logical impossibility and setsV = 1.Primary Use: The Overflow Flag prevents software errors caused by silent mathematical truncation in signed computations.
Summary of Differences
While both Carry and Overflow signal that a result does not fit within a fixed bit-width, they serve distinct number representations:
- Carry Flag (C): Applied to unsigned integers. It detects when an operation wraps around modulo \(2^n\).
- Overflow Flag (V): Applied to signed Two’s Complement integers. It detects when the sign bit is corrupted by numeric magnitude overflow.