Condition Codes and Status Flags in Branch Instructions
This article explains what condition codes are and how conditional branch instructions evaluate status flag combinations within the binary number system. In modern processor architectures, the arithmetic logic unit (ALU) updates hardware flags after arithmetic and logical operations. Control flow instructions then evaluate these individual bits or boolean combinations of them to determine whether a program should jump to a target address or proceed sequentially.
What is a Condition Code?
A condition code—often called a status flag—is a single binary bit stored in a dedicated processor register (commonly known as the Program Status Word, FLAGS register, or Condition Code Register). When the ALU executes an operation such as addition, subtraction, or bitwise comparisons, it sets (1) or clears (0) these bits based on the binary characteristics of the output.
The primary condition flags found in most architectures include:
- Zero Flag (Z): Set to 1 if the result of an operation is exactly zero; otherwise, set to 0.
- Sign / Negative Flag (S or N): Reflects the most significant bit (MSB) of the result. In two’s complement binary representation, an MSB of 1 indicates a negative value.
- Carry Flag (C): Set when an operation produces a carry-out or borrow from the most significant bit, primarily used for unsigned arithmetic overflow detection.
- Overflow Flag (V or O): Set when a signed arithmetic operation yields a result too large or too small to be represented in the allocated bit width, causing an erroneous sign change.
How Conditional Branch Instructions Evaluate Flags
Conditional branch instructions alter the instruction pointer if a specified logical condition is true. Rather than inspecting the entire numerical result again, the processor applies boolean logic directly to the status flags.
Simple Flag Evaluations
Simple branches check the state of a single flag:
- Branch if Equal (Zero): Evaluates \(Z = 1\). This typically follows a
comparison (
CMP) instruction that internally performs a subtraction. If both operands are identical, the result is zero. - Branch if Not Equal: Evaluates \(Z = 0\).
- Branch if Negative: Evaluates \(S = 1\).
- Branch if Positive or Zero: Evaluates \(S = 0\).
Unsigned Comparisons
Unsigned numbers range strictly from zero upward, meaning comparisons rely primarily on the Carry and Zero flags:
- Branch if Below (Unsigned Less Than): Evaluates \(C = 1\). In subtraction (\(A - B\)), a carry/borrow occurs if \(A < B\).
- Branch if Above (Unsigned Greater Than): Evaluates \(C = 0 \land Z = 0\). The result produced no borrow, and the values were not equal.
- Branch if Below or Equal: Evaluates \(C = 1 \lor Z = 1\).
Signed Comparisons
Signed values require checking combinations of the Sign and Overflow flags because an arithmetic overflow inverts the apparent sign of the result:
- Branch if Less Than: Evaluates \(S \oplus V = 1\) (Sign XOR Overflow). If no overflow occurs (\(V = 0\)), a negative result (\(S = 1\)) correctly indicates \(A < B\). If an overflow occurs (\(V = 1\)), the sign is inverted, meaning a positive result (\(S = 0\)) actually represents \(A < B\).
- Branch if Greater Than or Equal: Evaluates \(S \oplus V = 0\).
- Branch if Greater Than: Evaluates \(Z = 0 \land (S \oplus V = 0)\). The values must not be equal, and the signed comparison must indicate greater.
- Branch if Less Than or Equal: Evaluates \(Z = 1 \lor (S \oplus V = 1)\).
Execution Summary
By translating high-level comparison operators into low-level boolean expressions of \(Z\), \(S\), \(C\), and \(V\), microprocessors evaluate conditional branches in minimal clock cycles, enabling deterministic and efficient program control flow in binary computing.