开发者

Arithmetic Overflow vs. Arithmetic Carry

开发者 https://www.devze.com 2023-03-12 01:08 出处:网络
One of my lecture slides gives an example of arithmetic overflow and carry in a topic for conditional branching fla开发者_运维知识库gs on an ARM chip, quoted below:

One of my lecture slides gives an example of arithmetic overflow and carry in a topic for conditional branching fla开发者_运维知识库gs 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


  • 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).


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消