I'm trying to wrap my head around overflow & carry flags in x86.
As I understand it, for addition of signed 2's complement numbers, the flags can only be generated in one of four ways (my examples are 4-bit numbers):
- pos+pos = neg (overflow)
- 0111 + 0001 = 1000 (7 + 1 = -8)
- pos+neg = pos (carry)
- 0011 + 1110 = 0001 (3 + -2 = 1)
- neg+neg = neg (carry)
- 1111 + 1111 = 1110 (-1 + -1 = -2)
- neg+neg = pos (overflow & carry)
- 1000 + 1001 = 0001 (-8 + -7 = 1)
So, in x86 assembly, does subracting B from A generate the same flags as adding A and -B?
Here's a reference table that might help. This shows an example of every possible combination of the 4 arithmetic flags that can result from the ADD and SUB instructions on x86. 'h' 'ud' and 'd' stand for hex, unsigned decimal and signed decimal representations of each value. For example, the first row for SUB says 0xFF - 0xFE = 0x1 with no flags set.
But, I think the short story is that Alex's answer is correct.