Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. # A program to calculate sin(x) using taylor series
  2. .data
  3. pr1: .asciiz "Enter Float x: "
  4. sym_fnc: .asciiz "sin(x): "
  5. nl: .asciiz "\n"
  6.  
  7. .text
  8. .globl main
  9.  
  10. main:
  11. li $s7,0 # clear debug flag #+
  12.  
  13. # prompt user for x value
  14. li $v0,4 # print string
  15. la $a0,pr1 # prompt user for x
  16. syscall
  17.  
  18. # read user's x value
  19. li $v0,6 # read float
  20. syscall
  21.  
  22. jal qsin
  23.  
  24. la $a0,sym_fnc # string
  25. jal prtflt
  26.  
  27. li $v0,10
  28. syscall
  29.  
  30. # qsin -- calculate sine
  31. #
  32. # RETURNS:
  33. # f0 -- sin(x)
  34. #
  35. # arguments:
  36. # f0 -- x value
  37. #
  38. # registers:
  39. # f2 -- x value
  40. # f4 -- sum
  41. # f6 -- xpow (x^n)
  42. # f8 -- n2m1
  43. # f10 -- factorial (nfac)
  44. # f12 -- RESERVED (used in pflt)
  45. # f14 -- current term
  46. # f16 -- x^2
  47. #
  48. # f18 -- a one value
  49. #
  50. # t0 -- zero value
  51. # t1 -- one value
  52. #
  53. # t6 -- negation flag
  54. # t7 -- iteration count
  55. qsin:
  56. move $s0,$ra # save return address
  57. mov.s $f2,$f0 # save x value
  58.  
  59. mul.s $f16,$f2,$f2 # get x^2
  60.  
  61. li $t0,0 # get a zero
  62. li $t1,1 # get a one
  63.  
  64. li $t6,1 # start with positive term
  65.  
  66. # xpow = x
  67. mtc1 $t0,$f6 # xpow = x
  68. cvt.s.w $f6,$f6 # convert to float
  69.  
  70. # n2m1 = 0
  71. mtc1 $t1,$f8 # n2m1 = 0
  72. cvt.s.w $f8,$f8 # convert to float
  73.  
  74. # nfac = 1
  75. mtc1 $t1,$f10 # nfac = 1
  76. cvt.s.w $f10,$f10 # convert to float
  77.  
  78. # get a one value
  79. mtc1 $t1,$f18 # onetmp = 1
  80. cvt.s.w $f18,$f18 # convert to float
  81.  
  82. # zero the sum
  83. mtc1 $t0,$f4 # sum = 0
  84. cvt.s.w $f4,$f4 # convert to float
  85.  
  86. li $t7,10 # set number of iterations
  87.  
  88. sinloop:
  89.  
  90. div.s $f14,$f6,$f10 # cur = xpow / nfac
  91.  
  92. # apply the term to the sum
  93. bgtz $t6,sinpos # do positive? yes, fly
  94. sub.s $f4,$f4,$f14 # subtract the term
  95. b sinneg
  96.  
  97. sinpos:
  98. add.s $f4,$f4,$f14 # add the term
  99.  
  100. sinneg:
  101.  
  102. subi $t7,$t7,1 # bump down iteration count
  103. blez $t7,sindone # are we done? if yes, fly
  104.  
  105. # now calculate intermediate values for _next_ term
  106.  
  107. # get _next_ power term
  108. mul.s $f6,$f6,$f16 # xpow *= x2
  109.  
  110. # go from factorial(2n+1) to factorial(2n+1+1)
  111. add.s $f8,$f8,$f18 # n2m1 += 1
  112. mul.s $f10,$f10,$f8 # nfac *= n2m1
  113.  
  114. # go from factorial(2n+1+1) to factorial(2n+1+1+1)
  115. add.s $f8,$f8,$f18 # n2m1 += 1
  116. mul.s $f10,$f10,$f8 # nfac *= n2m1
  117.  
  118. neg $t6,$t6 # flip sign for next time
  119. j sinloop
  120.  
  121. sindone:
  122. mov.s $f0,$f4 # set return value
  123.  
  124. move $ra,$s0 # restore return address
  125. jr $ra
  126.  
  127. # dbgflt -- debug print float number
  128. dbgflt:
  129. bnez $s7,prtflt
  130. jr $ra
  131.  
  132. # dbgnum -- debug print int number
  133. dbgnum:
  134. beqz $s7,dbgnumdone
  135. li $v0,1
  136. syscall
  137.  
  138. dbgnumdone:
  139. jr $ra
  140.  
  141. # dbgprt -- debug print float number
  142. dbgprt:
  143. beqz $s7,dbgprtdone
  144. li $v0,4
  145. syscall
  146.  
  147. dbgprtdone:
  148. jr $ra
  149.  
  150. # prtflt -- print float number
  151. #
  152. # arguments:
  153. # a0 -- prefix string (symbol name)
  154. # f12 -- number to print
  155. prtflt:
  156. li $v0,4 # syscall: print string
  157. syscall
  158.  
  159. li $v0,2 # print float
  160. syscall
  161.  
  162. li $v0,4 # syscall: print string
  163. la $a0,nl # print newline
  164. syscall
  165.  
  166. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement