-2

Possible Duplicate:
about assembly CF(Carry) and OF(Overflow) flag

I'm trying to see if carry occurs after adding two unsigned dwords.

like this:

       add edx, eax           ; edx will hold the numeric value
       wait                   ; not really needed
       jc bad                 ; jmp to bad if carry bit is set
       jo bad                 ; jmp to bad if overflow bit is set (Yes for signed)

However, neither the overflow flags or carry flags are set accordingly. For testing purposes I do make sure that the addition will result in a value too big for 32bit reg. (after adding, the values wrap around)

How do I check if the two numbers added together will result in overflow?

Community
  • 1
  • 1
Dacto
  • 2,901
  • 9
  • 45
  • 54
  • 1
    What are the values of edx and eax before executing the add instruction? If they're both 0, then no, I suspect that neither carry nor overflow will be set. – Multimedia Mike Mar 15 '12 at 03:56
  • @Mike, in the question I note that I make sure that the values in edx and eax are large enough to cause and "overflow" or carry. But regardless, carry flag is not set. – Dacto Mar 15 '12 at 04:19
  • 2
    Remove `wait`. Then see examples of adding and subtracting integers and how carry and overflow are set in [this answer](http://stackoverflow.com/a/8037485/968261) to a related question. Most likely your numbers are too small to cause CF=1 or OF=1. It's also possible that you've messed up reading and writing registers (your edx or eax isn't loaded with the data you need or you fail to differentiate between the overflow and non-overflow cases due to some other code bug). Tell us more about your code or show it. – Alexey Frunze Mar 15 '12 at 05:24
  • 3
    And you really, really want to execute the above in a debugger and see what's in `edx` and `eax` before `add edx, eax` and what's in `eflags` after `add edx, eax`. – Alexey Frunze Mar 15 '12 at 05:28
  • Yes, I am using a debugger. the value of edx = 4294967295. eax > 0. – Dacto Mar 15 '12 at 05:45
  • How do I show eflags in visual studio? Nevermind, found it. – Dacto Mar 15 '12 at 05:53
  • 1
    -1. If eax=0xffffffff and edx!=0 as you write, `add edx,eax` _will_ cause the carry flag to be set. If it seems otherwise, you are either not reading the CF correctly, or not capable of making sure eax and edx actually holding the values you are claiming they hold. – Gunther Piez Mar 15 '12 at 12:36

1 Answers1

1

The overflow is used for signed addition and subtraction.

You should look at just the carry flag for unsigned addition and subtraction.

Eric Sites
  • 1,494
  • 10
  • 16