Mult overflow detection in MIPS assembly?

slickben

Weaksauce
Joined
Jun 9, 2003
Messages
114
Hello,
My partner and I are trying to get extra credit on one of our projects by adding overflow detection to the program. Is this possible? I know that it doesn't generate exceptions, but we have yet to figure out a way to tell if it overflows.
thanks
ben
 
I'm really struggling to find a reference to back this up, but I believe there's a kind of "branch on overflow" instruction. All I could find is bczt, which says "branch if coproc z is true" (reference). I think the coprocessor Z is set to true if an arithmetic operation overflows, among other things.
 
Alternately, you could check if the result of the multiplication is smaller than either of the things you're multiplying by - it's probably not a perfect answer, but it may be partial.
 
UMCPWintermute - thats what we were going to do originally, but we realized that its possible for the overflow to wrap all the way around and end up greater than the original number (does that sentence make any sense?)

HorsePunchKid - this sounds promising, but i cant find any info on what coprocessor z actually is. we weren't taught about coprocessors in class, but i like the idea of learning more about it. a google search doesn't really tell me anything about what coprocessor z is, all i find is that bczt command, which says branch if coprocessor z is true.

thanks
ben
 
A quick google on "mips overflow bit" gives you something. The first 2 pages explain it.

hi and lo
A general problem with integer multiplication is that the result is twice as wide as the operands. The basic solutions to this problem are either to assume the result will fit (and use the oVerflow bit if it doesn't), or to provide a double-wide place to put the result. MIPS takes the latter approach; the result of a multiply is placed in two special registers called hi and lo; special instructions exist to get the two halves of the result and move them to ordinary registers. We won't be concerned about these registers in the course of this class.
( http://www.cs.nmsu.edu/~pfeiffer/classes/473/notes/mips.html - talking about special-purpose registers )

and then the other page talks about the instructions to access hi.
 
but even if the hi isn't == 0, that doesn't mean that the multiplication is overflowing does it? or if we mfhi to a temp register and check that to see if that is == 0, does that indicate overflow?

i thought that the reason that mult uses hi and lo is because that max number of digits resulting from multiplication is twice the multiplicand.

am i misunderstanding this?

peace
 
i'm pretty sure you cannt overflow a multiplication operation because of the hi lo that allows for 64 bit product.
 
i know what you mean binary digit, but then how could we be getting negative numbers when we multiply very large quantities? can anyone clarify this?
thanks
ben
 
Are you multipling floats or int?


This makes a big difference.

Also you could get what appears as a negative number in the lo registers because it is unsigned and it if it interpreted as signed it could show up as negative when it really isn't. If your intial large quanties are greater then the register size then you will end up with negatives too.
 
sorry, shoulve mentioned were using ints. so if i understand what youre saying, when i see the negative number, im seeing the lo register being read as signed.
is there a way to display the value that spans hi and lo?
i think im beginning to understand...
thanks
ben
 
There is no direct assembly code to display anything.


If you want to display the whole result you will need custom code to take care of that. MIPS does provide a whole set of unsigned operations should you want to do anything to the value stored in lo.

If I know more of what you are trying to accomplish I might be able to help more.
 
well at this point now, im thinking about writing a subroutine to print out a number stored in 2 32 bit registers, namely hi and lo. is there a good way to do this?
thanks for everyones help
ben
 
Back
Top