Search code examples
assemblyx86carryflageflagssigned-overflow

Assembly - Carry flag VS overflow flag


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.


Solution

  • In unsigned arithmetic, you have added 0xFB to 0x84, i.e. 251 + 132, which indeed is larger than 8-bit, and so the carry flag is set.

    In the second case, you are adding +127 to 1, which indeed exceeds a signed 8-bit range, and so the overflow flag is set.