user2502844
user2502844

Reputation: 59

How can I test for integer multiplication overflow in MIPS?

I am learning MIPS assembly. I have read the following: "Both MIPS multiply instructions ignore overflow, so it is up to the software to check to see if the product is too big to fit in 32 bits. There is no overflow if Hi is 0 for multu or the replicated sign of Lo for mult" on http://opencourseware.kfupm.edu.sa/colleges/ccse/ics/ics233/files/5_Lab7.pdf.

How can I test for integer multiplication overflow in MIPS?

Upvotes: 2

Views: 2005

Answers (1)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26644

For multu (multiply unsigned), a,b ≥ 0 and (a+b) > b. If a and b are numbers in registers $8 and $9 then you can (but I didn't try and run this in SPIM yet) catch an overflow in $10 with the code:

 add $8, $8, $9
 slt $11, $9, $0    //set $11 if b is less than 0 
 slt $10, $8, $9    //set $10 if (a+b) is less than b
 xor $10, $10, $11  

I hope it works? Or we can try and run MIPS code.

Upvotes: 2

Related Questions