| New Reply |
Re: MIPS - Making a 64 bit number when you only have 32 bit register support |
Share Thread | Thread Tools |
| Aug12-12, 07:19 AM | #1 |
|
|
Re: MIPS - Making a 64 bit number when you only have 32 bit register support
Hey guys I've been working on this program for just over a week now and I just can't seem to find any way around my problem. The problem I'm having is:
I'm trying to add 32 bit numbers together and if there is an overflow then I'd like to find a way to output the result as a 64 bit number. I had a couple of ideas (as of yet, none of them have worked ) so here they are:
Thanks guys!!! *** Edit *** I originally posted this to the Electrical Engineering forum by mistake so I just copied it here 12/08/2012
|
| Aug12-12, 11:43 AM | #2 |
|
Recognitions:
|
MIPS doesn't have borrow or carry bits, so you have to code around this. If an 32 bit unsigned add would produce a carry, then the 32 bit lower sum will be less than either of the 32 lower order addends (input). You can use SLTU to take advantage of this fact. For subtract, you can use SLTU to check for a borrow condition in the low order registers. You'll want to use the unsigned add and subract instructions. For signed numbers you'll need to do an overflow check on the high order operation.
|
| Aug12-12, 12:56 PM | #3 |
|
|
Awesome thanks for the reply rcgldr
! If I do take the overflow into account and add it to a new registry, is there even a way to correctly output the 64 bit number?As in: If I have two registers, a high and a low, and whenever there is an overflow then I increment the high register, is there a way to get the actual decimal result between these two registers? Thanks !
|
| Aug12-12, 06:57 PM | #4 |
|
Recognitions:
|
Re: MIPS - Making a 64 bit number when you only have 32 bit register support |
| Aug13-12, 01:56 AM | #5 |
|
|
Awesome! I'm going to try dividing the number into 4X16 bit registers and see if I can make it work
!! Thanks so much for the help rcgldr !!!!!!!
|
| Aug13-12, 04:47 AM | #6 |
|
Recognitions:
|
Otherwise, to divide large numbers by large numbers, you'd need something like long hand division, where you make a "guess" at the quotient, and do a multiply and subtract step, iterating 2 or 3 times for each "word" of quotient. For really big numbers, there are complex algorithms based on Fourier Transforms, finite field (modulo) math, ..., to speed up the process, but I doubt you'd need any of this stuff for what you're doing with the MIPS projects. |
| Aug16-12, 11:45 AM | #7 |
|
|
Hi rcgldr thanks so much for the help!! Unfortunately I couldn't get the output to work so I switched to a different approach, I also made it a multiply instead of just an add function:
I made my result hi and result low registers the same as the mult hi and low registers. In case anyone ever needs to know how here's a short explanation: If we are to multiply 2 numbers in binary form there are many ways to do it without the mult function. Firstly if you want to do something like 8x5 you could add 8 to a result 5 times ie 8x5 = 8 + 8 + 8 + 8 + 8. Another way to do it is to loop through the binary representation of the number and every time you go through the loop - shift the multiplicand by one bit, now look at the multiplier, if the multiplier bit is 1 then add it to the result otherwise skip the add and continue the loop. Example: ![]() So this is all great and it works like a charm!... for small numbers, but what happens when you shift a binary number 30 times or more?? What you need is a high register for your multiplicand, when you shift it too high you need to shift it into this high register. You can actually add this high register to the result high register right from the start of the loop because at the start this "multiplicand high register" will be zero so you will effectively be adding nothing. That takes care of the multiplicand being shifted too much, another problem can occur when the result register actually has a carry bit. In this case you just add 1 to the result high register. Why? Because you need to add a bit to the high register and one bit is [itex]2^0 = 1[/itex]. A couple of the functions used are: addu $lowReg, $lowReg, $multiplicandReg Add the shifted multiplicand to the low register sll $multiplicandReg, 1 This shifts the multiplicand left by 1 bit and $t0, $multiplier, 1 Check if the least significant bit is a 1 srl $multiplier, 1 Once you've checked if it's a 1 or 0 you can shift that bit off the register There are a couple of others but those are the main ones that should allow for 2 numbers that are pretty small to easily multiply. |
| Aug16-12, 06:29 PM | #8 |
|
Recognitions:
|
If you store large integers using only 16 bits of each 32 bit register, you can use the multiply instruction to do multiplies. The method is similar to doing long hand multiplication. After a multiply step to produce a set of intermediate product terms, the upper 16 bits of a low order word get added to the lower 16 bits of the next higher order word (then those upper 16 bits in the low order word are zeroed out), looping through all the words used to hold the product. |
| New Reply |
| Thread Tools | |
Similar Threads for: Re: MIPS - Making a 64 bit number when you only have 32 bit register support
|
||||
| Thread | Forum | Replies | ||
| MIPS - Making a 64 bit number when you only have 32 bit register support | Electrical Engineering | 1 | ||
| Making a Shift Register Using NOR Gates | Engineering, Comp Sci, & Technology Homework | 2 | ||
| MIPS: Saving a String to register(s)? | Engineering, Comp Sci, & Technology Homework | 1 | ||
| Making a number into fraction.. | Precalculus Mathematics Homework | 3 | ||
| MIPS: How to parse an number | Programming & Comp Sci | 0 | ||