0

I just started learning assembly and I'm trying to understand how the different flags work. Let's say I do this:

mov ax, 0xBFE8h
mov bx, 25DFh
add ax, bx

Shouldn't the overflow flag turn on? The value of the addition is 58,823 (in base 10), which is more then the 32,767 (a signed 16 bit value). I checked in the program and the flag doesn't turn on. Why is this?

Thanks for any help.

Ethan
  • 163
  • 2
  • 12
  • http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt. You're looking for the Carry Flag (unsigned), not signed OverFlow. – Peter Cordes Nov 27 '17 at 14:26
  • In signed math (interpretation of values) you are doing -16408 + 9695 = negative + positive value can't overflow in the principle, as the result will be surely equal/between the original two values. OF is signed-math related. – Ped7g Nov 27 '17 at 16:14

1 Answers1

4

The overflow flag turns on when signed overflow occurs. However, this is not the case in your code. 0xbfe8 is larger than 0x8000 and thus negative in two's complement, representing −16408. 0x25DF is equal to 9695, their sum is −6713 which is not outside of the range −32768 to +32767, so no signed overflow occured and the overflow flag is cleared.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 1
    Surely this question is a duplicate of something. I've been looking but haven't found an identical one with good answers yet. – Peter Cordes Nov 27 '17 at 14:29
  • @PeterCordes Maybe this one is going to be the canonical duplicate in the future. – fuz Nov 27 '17 at 14:31
  • Finally! I've been trying to understand this for a while, thanks! – Ethan Nov 27 '17 at 14:32
  • This one is quite good: reference table of inputs / output / CF / OF for 8-bit add and sub: https://stackoverflow.com/questions/8965923/carry-overflow-subtraction-in-x86 – Peter Cordes Nov 27 '17 at 14:33
  • CF should be clear here: this doesn't quite wrap. (upvoted anyway because that's an easy fix). – Peter Cordes Nov 27 '17 at 14:35