Assembly - Carry flag VS overflow flag

Tom O picture Tom O · Dec 13, 2011 · Viewed 25.8k times · Source

I have the next code:

mov al, -5
add al, 132
add al, 1

As I check it, the overflow flag and the carry flag will set in the first operation, and in the second, only the overflow will set.

But I don't understand why:

  1. In unsigned number, the result is 143 (8FH), and for that is fit 8-bit unsigned number (is smaller than 255) => the carry flag shouldn't be set. In signed number, the result is 127, It's fit to 8-bit signed, and the overflow shouldn't be set.

Whats wrong? Thanks.

Answer

Hassan picture Hassan · Nov 16, 2012

Overflow occurs when the result of adding two positive numbers is negative or the result of adding two negative numbers is positive. For instance: +127+1=?

+127=0111 1111
  +1=0000 0001
     ---------
     1000 0000 

As we look at the sign bits of the two operands and the sign bit of the result, we find out that Overflow occurred and the answer is incorrect.