# Overflow Flag

The Overflow flag is a Processor status flag, which indicates that an arithmetic overflow has occurred.

In signed arithmetic, the range of a single byte is -128 thru 127. The highest order bit of a signed byte is evaluated as the sign rather than part of the value. If the high order bit (bit 7) is a one (1), then the value is considered negative. If bit 7 is a zero (0), then the value is considered positive.

The Overflow flag is only affected by five instructions: ADC, BIT, CLV PLP, and SBC. Of these, CLV is simply used to clear the flag. Notice there is no SEV instruction to set the flag. PLP restores the value of the flag from the stack along with all other processor status flags. And BIT has special uses, described elsewhere.

This leaves us with only ADC and SBC. And even though the Comparison instructions are technically carried out as subtractions (without storing the result), they do not affect the Overflow flag at all.

The Overflow flag is set when the value in the accumulators bit 6 "rolls over" into the sign bit (bit 7), AND changes its value. Consider, for instance, the unsigned value 127 (7F_{16} or 01111111_{2}). As a signed value 127 is the largest positive integer a single byte can store. If you add one (1) to it, an overflow occurs, because the value in bit six (1) "overflows" into the position of the sign bit (bit 7). Similarly, in negative numbers, consider the value -128 (80_{16} or 10000000_{2}). As a signed value, -128 is the smallest negative integer a single byte can store. If you subtract one (1) from it, an overflow occurs again, because the value in bit six (0) "overflows" into the position of the sign bit (bit 7).

** Using the Overflow Flag **

In single byte arithmetic, one simply executes ADC or SBC and evaluates the flag afterwards using the BVC and BVS instructions.

In multi-byte arithmetic, it is only necessary to evaluate the Overflow flag after calculating the highest order byte in a multi-byte integer. Given that the processor is unaware which byte of a multi-byte integer is the highest order byte containing the sign bit, the state of the Overflow flag will be set or cleared with each byte's calculation. Only the state after the highest order byte is valid.