0

I think I've come to learn the use cases on when the overflow flag is set: when the signed bit changes on signed arithmetic. For example, the following three cases:

    # For example, with overflow:
    mov $0b00100000, %al # 32
    add $0b01100000, %al # 96
    # ---------------------
    #   $0b10000000  # Notice how the signed bit is set so the answer is -128, not +128

    # And with negative numbers (subtracting two negative numbers) where the sign =-bit is different
    # mov $-0b00100000, %al # 
    mov  $0b10000000, %al # -128
    add  $0b10000001, %al # -127
    # ---------------------
    #    $0b00000001  # +1 -- again the sign bit changes, so we have the overflow on

    # Doing the same thing but subtracting a positive number to get the overflow instead
    mov  $0b10000000, %al # -128
    sub  $0b00000100, %al # 4
    # ---------------------
    #    $0b01111100 # + 124 -- result is positive, so again we have overflow

However, what are some applications or use cases when the overflow flag is used? In the minimal asm knowledge I have it seems the Zero and Sign flag are used all the time for comparisons, but in what use cases is the Overflow flag used?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • 2
    Everytime you are interested in whether a signed addition or subtraction overflowed (which is also implicitly checked by signed comparisons). Note that the sign flag by itself is used less frequently than together with the overflow flag. E.g. `JG` checks `ZF == 0 && SF == OF` – Jester Sep 26 '20 at 23:16
  • @Jester thanks. Could you please elaborate on this, perhaps in an answer? For example, why does it check that `SF == OF` ? – carl.hiass Sep 26 '20 at 23:33
  • 3
    Because as you yourself have described, SF gets reversed if an overflow happens. So to check greater-than you either have a positive result with no overflow (`SF = OF = 0`) or a negative result with an overflow (`SF = OF =1`). – Jester Sep 26 '20 at 23:36
  • @Jester that makes sense, thanks for the explanation. – carl.hiass Sep 26 '20 at 23:38
  • Tables like http://ref.x86asm.net/geek.html show which instructions can modify this flag. It's basically just add, subtract, and signed multiply, and they use it to mean "overflow" in the natural sense of signed arithmetic. Note `cmp` is a version of subtraction. Bitwise operations tend to unconditionally clear it. – Nate Eldredge Sep 27 '20 at 02:09
  • The [RCL / RCR / ROL / ROL](https://www.felixcloutier.com/x86/rcl:rcr:rol:ror) rotate instructions set it in a complicated way which surely has some purpose that is not immediately clear to me. Oh, see https://stackoverflow.com/questions/31125111/ror-turns-on-the-overflow-flag and the comment on the answer. – Nate Eldredge Sep 27 '20 at 02:25
  • Also related: [Understanding sign and overflow flag in assembly](https://stackoverflow.com/q/14155145) - pretty similar point to [Difference between JS and JL x86 instructions](https://stackoverflow.com/q/25031860) but SO only allows 5 duplicates. It seems you missed the fact that OF is an essential part of signed comparison between two non-zero numbers, and there are several duplicates for that, as well as one about actually detecting signed integer overflow after each math operation. – Peter Cordes Sep 27 '20 at 02:27

0 Answers0