Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pseudo Instruction for Division in MIPS

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

like image 556
Alfred Avatar asked Nov 20 '12 14:11

Alfred


1 Answers

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
like image 174
gusbro Avatar answered Sep 21 '22 06:09

gusbro