This question is about cmp instructions in the assembly. I canβt understand how my books talk about the SF and OF flags.
cmp vleft, vright
According to my book: for signed integers, three flags are important: the flag (ZF) , the overflow flag (OF) and the sign (SF) . The overflow flag is set if the result of the operation is overflowed (or overflowed). The sign flag is set if the result of the operation is negative. If vleft = vright , ZF set (as for unsigned integers). If vleft > vright , ZF not specified and SF = OF . If vleft < vright , ZF not specified and SF != OF . Remember that other instructions can also change the case of FLAGS, not just cmp .
First, consider the case vleft > vright . My book says the following:
Why is SF = OF if vleft> vright? If overflow does not occur, the difference will have the correct value and should be non-negative. Thus, SF = OF = 0. However, if there is an overflow, the difference will not have the correct value (and will actually be negative). Thus, SF = OF = 1.
In the first part, I understand that SF = OF = 0 . It could be, for example:
0111 - 0101 = 0111 + 1010 + 1 = 10010 = 7 - 5 = 2
This would not set the OF or SF flag.
A case may also be:
1100 - 0101 = 1100 + 1010 + 1 = 10111 = -4 - 5 = 7 (-9 if we had more bits)
This would not set the SF flag (since the answer is affirmative 7), but would set OF = 1 , thus SF != OF . This is clearly contrary to the explanation of my book, which states that they should be equal.
What am I missing here?
Thanks!
source share