As you know, the overflow flag is applicable only for signed integer arithmetic. On processors in which the ALU has both overflows and carry flags (for example, x86), both of these flags are set in accordance with the result of a binary arithmetic operation, but it is up to the programmer to decide how to interpret them. Signed arithmetic uses the overflow flag; unsigned arithmetic uses the carry flag. Looking at the wrong one gives you meaningless data.
There are two cases where the overflow flag is turned on during a binary arithmetic operation:
- The inputs of both have sign bits that are off, and the result has a sign bit that is on.
- The inputs both have signed bits that are turned on, while the result has a signed bit that is turned off.
, , . .
:
- 0100 + 0001 = 0101 ( )
- 0100 + 0100 = 1000 ( )
- 0110 + 1001 = 1111 ( )
- 1000 + 1000 = 0000 ( )
- 1000 + 0001 = 1001 ( )
- 1100 + 1100 = 1000 ( )
, ; , . . , , , . , , . , , , . , .
(, ).
, , ALU . , , , , , - . , .
C- :
bool GetOverflowFlagForAddition(int op1, int op2, int result)
{
return (~(op1 ^ op2) & (op1 ^ result)) < 0;
}
bool GetOverflowFlagForSubtraction(int op1, int op2, int result)
{
return ((op1 ^ op2) & (op1 ^ result)) < 0;
}
( , , .)
, , Iwillnotexist Idonotexist : " XOR ." , ( ) .
, XOR . 8- : O = C 6 ^ C 7, O "" C "". , : , ( 7).
. , ( 6502, 8- ). 6502.
, ? . , :
. , :
- 1111 + 0001 = 0000 ( )
- 0111 + 0001 = 1000 ( )
- 0000 - 0001 = 1111 ( )
- 1000 - 0001 = 0111 ( )
, , , , , :
- 0000 + 1111 = 1111 ( )
- 1000 + 1111 = 0111 ( )
... , .
, . , ( ). , , , . OF == CF ^ SF, - , XORed , . , .
, , 8085 Ken Shirriff , , , XORing - C 6 ^ C 7, ALU. ( , , "K", "V" , , , .)