I've been playing with assembly for a while and looking at some code. in which AL is first set to 0x84 then cmp AL, 0x30 is used. This instruction then triggers the Overflow flag.
From what I read CMP is supposed to subtract the second number from the first then set the flags, in that case it should be 0x84-0x30 the result is 0x54 and there is no overflow.
CMP and TEST instructions affect flags only and do not store a result (these instruction are used to make decisions during program execution). These instructions affect these flags only: CF, ZF, SF, OF, PF, AF.
The overflow flag is thus set when the most significant bit (here considered the sign bit) is changed by adding two numbers with the same sign (or subtracting two numbers with opposite signs).
The four flags that the CMP instruction can set - Z,O,C, and S, are known as the zero, overflow, carry, and sign flags respectively. The zero flag is set whenever the result of the subtraction is equal to zero. This, of course, only occurs when the operands are equal.
0100 + 0100 = 1000 (overflow flag is turned on) If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is turned on. 1000 + 1000 = 0000 (overflow flag is turned on)
There's only no overflow if you're interpret those values as unsigned numbers - if you interpret your 0x84
as signed, there's definitely overflow:
-172 is outside of the range of a signed 8-bit value (-128 to +127) and that's why the OF
flag gets set. You should check CF
which indicates unsigned overflow.
From the Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 2 for CMP:
The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction.
and for SUB:
The SUB instruction performs integer subtraction. It evaluates the result for both signed and unsigned integer operands and sets the OF and CF flags to indicate an overflow in the signed or unsigned result, respectively. The SF flag indicates the sign of the signed result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With