Search code examples
mipsadditionsignedinteger-overflow

MIPS overflow exception


I have read through a couple of posts here on overflow exception for MIPS signed and unsigned addition, but it is still not clear to me.

1) What is meant by an overflow exception and when does it occur? 2) When is the overflow ignored for an unsigned addition?

Suppose I have two numbers, lets start with 4 bit signed and then go to 4 bit unsigned

Signed negative addition

   1010    -6
 + 1101    -3
  ------
 1 0111    +7 with a carry(overflow) of 1, but result should have been -9. 

Signed positive addition

   0110    +6
 + 0011    +3
--------
   1001    -7 with no carry(probably an overflow of 1), but actual result should have been +9. 

Next is an unsigned addition

   1111    +15
 + 1010    +10
---------
 1 1001    +9 with an overflow/carry of 1, but actual result is +25.

Please let me know in which case, do we ignore overflow exception and in which case do we raise overflow exception and why. I read that for unsigned, we always ignore the overflow, but then if the result is incorrect(9 instead of 25), why do we ignore the overflow?


Solution

  • The difference on MIPS signed and unsigned addition instructions is that integer overflow exception is generated for signed additions in case of 2's complements overflow, where no exception is generated for unsigned exceptions.

    Instructions are described here: MIPS - Integer - Arithmetic.

    So in your examples, the two "signed additions" (e.g. ADD instruction) will generate exception, since and 2's complement overflow occur, and the "unsigned addition" (e.g. ADDU instruction) won't, even through the result is also wrong here. The programmer hereby have control over when a 2's complement overflow should generate and exception. However, then instruction set does not allow 32-bit unsigned addition with overflow from bit 32, so if you really need that last bit, do 64-bit arithmetic.

    See these posts also: