0

I do not get the difference between addition and subtraction, I have tried to look it up but still no answer. Please explain in the simplest way.

I know when addition, exceeds the maximum size of the integer type used to store it. When an integer overflow occurs, the interpreted value will appear to have “wrapped around” the maximum value and started again at the minimum value but how, what are some conditions to prevent that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
rezy3312
  • 1
  • 2

1 Answers1

0

related: http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt It's about x86's carry (unsigned) and overflow (signed) flags.

For MIPS, read it with that in mind: add will trap any time x86 would set OF.

In MIPS, you can get the carry-out from an add using (a+b) < a unsigned compare. Or for a sub, (a-b) > a unsigned.

I guess you could also detect signed overflow after subu with (a-b) > a signed compare. So if you want a-b to not overflow, choose your inputs such that the non-wrapped result is between INT_MIN and INT_MAX. That is all.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847