Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Integer to Double in MIPS

I want to divide two values that are in $tn registers.

I have to divide these two values to get a double result but the function div only returns the integer part of that division can anyone help out?

Do i need to convert $t1 and $t2 to $f0 and $f2?

How do i do that?

li $t1,2 
li $t2,5 

div $f0,$t2,$t1

This gives me an error because it expects a $tn value not a $fn value...

like image 787
André Alvarez Avatar asked May 18 '13 19:05

André Alvarez


1 Answers

You have to move and convert the integer stored in a general purpose register to floating point or double register.

Assuming your number is stored in $a1, To convert to a double pair ($f12, $f13) you have to issue:

  mtc1.d $a1, $f12
  cvt.d.w $f12, $f12

And to convert it to a single precision float ($f12) you'd do:

  mtc1 $a1, $f12
  cvt.s.w $f12, $f12

Then you can use div.d or div.s to do floating-point division and get a floating point result.

like image 178
gusbro Avatar answered Nov 19 '22 08:11

gusbro