Unsigned vs Signed Overflow: Carry and Overflow Flags

Central Processing Units (CPUs) handle binary arithmetic identically for both signed and unsigned numbers, relying on status flags in the flags register to indicate when an operation exceeds hardware limits. The processor evaluates unsigned overflow using the Carry Flag (CF), which tracks whether an arithmetic operation generates a carry out of the most significant bit. Simultaneously, it evaluates signed overflow using the Overflow Flag (OF), which detects whether an operation produces a result that violates the range of two’s complement signed representation. Understanding the distinction between these two hardware mechanisms allows software to correctly interpret arithmetic results based on data types.

The Dual-Nature of CPU Binary Arithmetic

The Arithmetic Logic Unit (ALU) does not distinguish between signed and unsigned integers when performing addition or subtraction. For any given \(n\)-bit binary addition, the ALU performs standard binary addition on the bits and updates both the Carry Flag and the Overflow Flag simultaneously. The programmer or compiler determines which flag matters based on whether the data is treated as signed or unsigned.

Unsigned Overflow: The Carry Flag (CF)

Unsigned numbers represent values from \(0\) to \(2^n - 1\) for an \(n\)-bit register. An unsigned overflow occurs when the true mathematical sum of an operation exceeds this maximum capacity, causing the value to wrap around to zero.

The CPU evaluates the Carry Flag using a simple hardware condition: * Addition: The Carry Flag is set to 1 if there is a carry-out generated from the Most Significant Bit (MSB). If no carry-out occurs, it is set to 0. * Subtraction: The Carry Flag acts as a “borrow” flag. It is set to 1 if the minuend is strictly less than the subtrahend (requiring a borrow from beyond the MSB).

8-Bit Unsigned Example

Signed Overflow: The Overflow Flag (OF)

Signed numbers in modern architectures use two’s complement representation, covering a range from \(-2^{n-1}\) to \(2^{n-1} - 1\). A signed overflow occurs when an arithmetic operation on two numbers of the same sign produces a result with the opposite sign, representing an impossible mathematical outcome.

The CPU evaluates the Overflow Flag through one of two equivalent hardware methods:

  1. Sign Bit Logic:
    • Adding two positive numbers yields a negative result (e.g., (+) + (+) = (-)).
    • Adding two negative numbers yields a positive result (e.g., (-) + (-) = (+)).
    • Adding operands of different signs cannot cause signed overflow.
  2. Carry-In vs. Carry-Out XOR Logic (Internal ALU):
    • The ALU calculates the exclusive OR (XOR) between the carry going into the MSB (\(C_{in}\)) and the carry coming out of the MSB (\(C_{out}\)).
    • Formula: \(\text{OF} = C_{in} \oplus C_{out}\)
    • If a carry enters the MSB without leaving it, or leaves the MSB without entering it, the sign bit has been corrupted, setting the Overflow Flag to 1.

8-Bit Signed Example

Summary Comparison

Metric Carry Flag (CF) Overflow Flag (OF)
Data Interpretation Unsigned integers Signed two’s complement integers
Valid Range (8-bit) \(0\) to \(255\) \(-128\) to \(+127\)
Trigger Condition Carry-out from MSB (\(C_{out} = 1\)) \(C_{in} \neq C_{out}\) at the MSB
Meaning of Flag = 1 Result exceeded \(2^n - 1\) Sign bit inversion error occurred