Arithmetic Overflow vs. Arithmetic Carry

26,049

Solution 1

  • Overflow flags get set when the register cannot properly represent the result as a signed value (you overflowed into the sign bit).
  • Carry flags are set when the register cannot properly represent the result as an unsigned value (no sign bit required).

Solution 2

You get an overflow, if the sign bit is corrupted. So you know that you have to adjust your result.

You get a carry if the result does not fit in this value. (need more bits than you have).

In the old days you had to write your own addition and multiplication subroutines for multi byte values.

Solution 3

The beauty of twos complement is that the ADD and SUB operations are exactly the same so you don't need different instructions for signed and unsigned values.

However there is a problem the ALU is just dealing with an 8 bit number so it does not know if the bits represent signed or unsigned numbers.

Consider two operations:

  1. 255 (AKA -1) ADD 1 The answer is 0 either way. However if the 255 was representing -1 this operation is fine -1 ADD 1 = 0 no problem. OTOH if 255 was unsigned 255 ADD 1 = 0 is wrong so the ALU sets the C flag to say "IF that was an unsigned operation it went out of bounds"

  2. 127 ADD 1 answer 128 (AKA -128). If 127 was representing an unsigned number this is fine 127 ADD 1 = 128, however if its a signed number 127 ADD 1 = -128 is wrong. So in this case the ALU sets the V flag saying "IF that was a signed operation the answer went out of bounds"

In principle you could design an ALU with just one flag if you had a bit to tell it if the operation is signed or unsigned. then you would have different op codes for signed and unsigned operations.

Share:
26,049
eggonlegs
Author by

eggonlegs

internet tough guy

Updated on April 09, 2021

Comments

  • eggonlegs
    eggonlegs about 3 years

    One of my lecture slides gives an example of arithmetic overflow and carry in a topic for conditional branching flags on an ARM chip, quoted below:

    • V (overflow) -  7FFFFFFF+1
    • C (carry) -  FFFFFFFF+1

    Presumably for the sake of the example, the address can only hold 8 bytes. So to me, it seems likes adding 1 to 7FFFFFFF gives 80000000. I thought 80000000 would still fit into an 8-byte address.

    Why is this an arithmetic overflow? Is it the wrong way around on the slide? Or is my understanding flawed?

    Thanks for any responses