Arithmetic Overflow in mips

29,176

According to the MIPS instruction reference, the only addition operations which can produce overflow exceptions are the signed addition instructions:

ADD
ADDI

MIPS integers are 32-bit, and since you'll be using signed integers, the maximum value is 231-1 (aka 2147483647 or hex 7FFFFFFF). Thus any addition which results in a number larger than this should throw an exception, e.g if you try to add 1 to 2147483647:

# Load 2147483647 into $s1
LUI $s0, 32767
ORI $s1, $s0, 65535

# Add 1 to $s1 and store in $s2. This should produce an overflow exception
ADDI $s2, $s1, 1
Share:
29,176
Dennis
Author by

Dennis

Updated on July 06, 2022

Comments

  • Dennis
    Dennis almost 2 years

    I am just started learning exception handler of MIPS instruction.

    I need to make my program to have Arithmetic overflow exception so that i can test my exception handler.

    I have two array A and B. Array A has hex number and Array B has integers.

    How to make overflow by adding hex number and integer ?

    The addition of which hex number and integer can cause overflow?

    • Simon MᶜKenzie
      Simon MᶜKenzie about 10 years
      I'm not familiar with MIPS, but hex is just a representation of a number, not a datatype, so surely you're just adding two integers. If your integers are 32 bits, then any two numbers which add to more than 2^32-1 (i.e. 4294967295) will produce an overflow.
    • Dennis
      Dennis about 10 years
      Thank you Simon, i tried it already. I tried like this 0x1+4294967295. but I still don't get an overflow error. i get the answer 0x0.
    • Simon MᶜKenzie
      Simon MᶜKenzie about 10 years
      Are you using add or addi? As far as I can see, if you use signed instructions, you should get overflow exceptions.
    • Dennis
      Dennis about 10 years
      i used "add". i should get overflow right?
    • Simon MᶜKenzie
      Simon MᶜKenzie about 10 years
      The problem is probably that if you're using signed operations, the largest (signed) integer is actually 2^31-1. Try adding 1 to that.
    • Dennis
      Dennis about 10 years
      Wow !!! It works. I got the overflow exception error. It look like i am the only one who is happy to get the error. LOL Thanks a lot Simon. You are a great help. :D
  • Roronoa Zoro
    Roronoa Zoro almost 7 years
    How come is the maximum value (2^31) -1? Your range for an unsigned number is from 0 to (2^32) -1, using 32-bit integers!
  • Simon MᶜKenzie
    Simon MᶜKenzie almost 7 years
    Very good point @RoronoaZoro - I'm not sure why I wrote "unsigned" - ADD and ADDI are signed operations (which I even mentioned at the top of the answer). I've removed the "un"!