6

What is the pseudo instruction for division in MIPS? Like there is a pseudo instruction of multiplication is MIPS "mul" which makes the life a little bit easier. I am using QT Spim.

Regards

5
  • This kind of questions should be googled... anyway, for integer division, use div
    – gusbro
    Nov 20, 2012 at 15:44
  • that is a true instruction. I googled it already but couldn't find it so asked here. Please check my other question also stackoverflow.com/questions/13476421/…
    – Alfred
    Nov 20, 2012 at 15:45
  • div is a true instruction and also a pseudo instruction ;). The pseudoinstruction div $t1, $t2, $t3 will set $t1 to the integer division of $t2/$t3
    – gusbro
    Nov 20, 2012 at 15:47
  • sir Please check my other question also. I am having difficulty in finding even and odd using integer registers in mips
    – Alfred
    Nov 20, 2012 at 15:48
  • How can I find remainder in floating point division in Mips
    – Alfred
    Nov 20, 2012 at 15:58

1 Answer 1

9

To perform integer division you can use div $t1, $t2, $t3 which will set $t1 to the integer division of $t2/$t3, or div $t1, $t2, imm where imm is an immediate.

Likewise, to compute the remainder of integer division, you can use rem $t1, $t2, $t3 or rem $t1, $t2, imm

These pseudoinstructions will basically do a div $t1, $t2 which stores the results in LO and HI special registers (for quotient and remainder) and then move those values to the target register by means of mfhi and mflo.

To perform floating point division you need to use floating point registers $f0 to $f31. Once you have your floating point numbers stored in some of these registers (properly encoded as floating point numbers, e.g. in $f1 and $f2) you issue a div.s $f0, $f1, $f2 instruction to get into $f0 the result of $f1/$f2. Now, to get the remainder you can subtract the result of the division with the truncation of the result. For example:

     li $a1, 20
     mtc1  $a1, $f1      
     cvt.s.w $f1, $f1    # $f1 = 20
     li $a1, 7
     mtc1  $a1, $f2     
     cvt.s.w $f2, $f2    # $f2 = 7

     div.s $f0, $f1, $f2 # $f0 = 20/7
     trunc.w.s $f3, $f0  
     cvt.s.w $f3, $f3    # $f3 = trunc(20/7)
     sub.s $f4, $f0, $f3 # $f4 = remainder of 20/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.