Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does cmp 0x84,0x30 trigger the overflow flag?

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.

like image 826
Maciek Avatar asked Aug 31 '11 18:08

Maciek


People also ask

How does CMP affect carry flag?

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.

How do you trigger an overflow flag?

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).

What flag does CMP use?

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.

What is overflow flag example?

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)


1 Answers

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:

  1. 0x84 interpreted as a signed 8-bit value is -124
  2. 0x30 interpreted as a signed 8-bit value is 48
  3. -124 - 48 = -172

-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.

like image 71
Carl Norum Avatar answered Sep 17 '22 16:09

Carl Norum