This is a discussion on Re: Overflow flag - ASM x86 ASM 370 ; Chris wrote: > I don't understand when the OF bit is set by the CMP instruction: > 1) ecx = 5; edx = 2 => ZF = 0, OF = 0, SF = 0 (got it) > 2) ecx = ...
Chris wrote:
> I don't understand when the OF bit is set by the CMP instruction:
> 1) ecx = 5; edx = 2 => ZF = 0, OF = 0, SF = 0 (got it)
> 2) ecx = 2; edx = 5 => ZF = 0, OF = ?, SF = 1
> OF must be 0 but why? When exactly is OF set?
> And when is the carry flag set?
Hello,
The overflow flag is set when the result of a signed
arithmetic operation generates a "wrong" answer due to
the size of the operands.
CMP is equivalent to a SUB that leaves the register
contents unchanged.
SUB AX,10
is like
AX = AX - 10
Or, AX is set to AX minus ten.
So for your 1), CMP ECX,EDX generates the results of 5 - 2,
which is 3. Three is an okay signed result.
2) 2 - 5 gives -3, again a proper signed result.
So, using AH and AL to use smaller numbers, try
MOV AH,-100
MOV AL,90
CMP AH,AL ; this generates an implicit subtract. Result = AH - AL
So, -100 - 90 = -190. -190 is outside the 127 to -128
range of a signed byte. It therefore generates an overflow.
Carry is somewhat similar for unsigned arithmetic. If
a result is outside the range of the unsigned number, a
carry is generated.
HTH
Steve N.