Mips单双浮点精度

时间:2015-04-08 00:34:32

标签: floating-point mips double-precision single-precision

我有mips的这个程序,我不是要把它改为双精度。看起来单精度和双精度浮动指令具有相同的指令但不是.s它是.d如果任何人有共识或帮助它会帮助我很多。

#‎Babylonian method: The babylonian method is one of the many method to approximate Roots.
#The method uses the following C code:
#   float x = n;
#   float y = 1;
#   float e = 0.000001; /* e decides the accuracy level*/
#   while(x - y > e)
#   {
#     x = (x + y)/2;
#     y = n/x;
#   }

.data
StartingStatment:   .asciiz    "Please enter a number that you would like to find the square root of: \n"
Answer:     .asciiz    "Your answer is: \n"
NumberofIterations: .asciiz "Please enter the number of Iteration. Example: 100\n"
ErrorQ: .asciiz "Pleae enter a number which is greater than zero!"
Ended: .asciiz "\n--Ended--"
Y: .float 1.0
SmallValue: .float 0.000001
Two:        .float 2.0


.text
.globl  main
main:

#printing message for StartingStatment
li  $v0, 4
la  $a0, StartingStatment       
syscall 

li  $v0, 6      #load a six into vo for syscall user input  
syscall         
mov.d $f2, $f0      #$f2 have value
mov.d $f8, $f2      ##### convert float to int
cvt.w.d $f8, $f8
mfc1 $t1, $f8       #converted int stored in t1

blt $t1,0,ErrorG    #if int is less than one then throw an error

l.d $f4, Y      #loading float into $f4
l.d $f12, SmallValue    #load value into f12
l.d $f8, Two        #load value into f8

j FindSqaureRoot    #now find the square root 

FindSqaureRoot:

#$f2        #x
#f4     #y 
#f12        #SmallValue
mov.d $f14,$f2 #n

#while(x - y > e)
#  {
#    x = (x + y)/2;
#    y = n/x;
#  }
#  return x;

SubtractionChecking:
sub.d $f2,$f2,$f4               #x-y

c.lt.d  $f2,$f12                # is (x-y) < e?
bc1t    Print                   #yes:  print(Move towards answer)

c.lt.d  $f12,$f2                # is (x-y) > e?
bc1t    Loop                    # yes: Continue with the #loop!


Loop:
add.d $f6,$f2,$f4               #x+y
div.d $f2,$f6,$f8               #divding it by 2
div.d $f4,$f14,$f2              #n/x
j SubtractionChecking

#Printing the answer!
Print:
mov.d $f16,$f2
li $v0,2
syscall

j EndProgram

#Error that is generated when a number is less than 0!
ErrorG:
li  $v0, 4
la  $a0, ErrorQ     
syscall

j EndProgram

EndProgram:
    li  $v0, 4          # printing message for xvalue
    la  $a0, Ended      
    syscall     

    li $v0,10
    syscall

0 个答案:

没有答案