Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A program to calculate sin(x) using taylor series
- .data
- pr1: .asciiz "Enter Float x: "
- sym_fnc: .asciiz "sin(x): "
- nl: .asciiz "\n"
- .text
- .globl main
- main:
- li $s7,0 # clear debug flag #+
- # prompt user for x value
- li $v0,4 # print string
- la $a0,pr1 # prompt user for x
- syscall
- # read user's x value
- li $v0,6 # read float
- syscall
- jal qsin
- la $a0,sym_fnc # string
- jal prtflt
- li $v0,10
- syscall
- # qsin -- calculate sine
- #
- # RETURNS:
- # f0 -- sin(x)
- #
- # arguments:
- # f0 -- x value
- #
- # registers:
- # f2 -- x value
- # f4 -- sum
- # f6 -- xpow (x^n)
- # f8 -- n2m1
- # f10 -- factorial (nfac)
- # f12 -- RESERVED (used in pflt)
- # f14 -- current term
- # f16 -- x^2
- #
- # f18 -- a one value
- #
- # t0 -- zero value
- # t1 -- one value
- #
- # t6 -- negation flag
- # t7 -- iteration count
- qsin:
- move $s0,$ra # save return address
- mov.s $f2,$f0 # save x value
- mul.s $f16,$f2,$f2 # get x^2
- li $t0,0 # get a zero
- li $t1,1 # get a one
- li $t6,1 # start with positive term
- # xpow = x
- mtc1 $t0,$f6 # xpow = x
- cvt.s.w $f6,$f6 # convert to float
- # n2m1 = 0
- mtc1 $t1,$f8 # n2m1 = 0
- cvt.s.w $f8,$f8 # convert to float
- # nfac = 1
- mtc1 $t1,$f10 # nfac = 1
- cvt.s.w $f10,$f10 # convert to float
- # get a one value
- mtc1 $t1,$f18 # onetmp = 1
- cvt.s.w $f18,$f18 # convert to float
- # zero the sum
- mtc1 $t0,$f4 # sum = 0
- cvt.s.w $f4,$f4 # convert to float
- li $t7,10 # set number of iterations
- sinloop:
- div.s $f14,$f6,$f10 # cur = xpow / nfac
- # apply the term to the sum
- bgtz $t6,sinpos # do positive? yes, fly
- sub.s $f4,$f4,$f14 # subtract the term
- b sinneg
- sinpos:
- add.s $f4,$f4,$f14 # add the term
- sinneg:
- subi $t7,$t7,1 # bump down iteration count
- blez $t7,sindone # are we done? if yes, fly
- # now calculate intermediate values for _next_ term
- # get _next_ power term
- mul.s $f6,$f6,$f16 # xpow *= x2
- # go from factorial(2n+1) to factorial(2n+1+1)
- add.s $f8,$f8,$f18 # n2m1 += 1
- mul.s $f10,$f10,$f8 # nfac *= n2m1
- # go from factorial(2n+1+1) to factorial(2n+1+1+1)
- add.s $f8,$f8,$f18 # n2m1 += 1
- mul.s $f10,$f10,$f8 # nfac *= n2m1
- neg $t6,$t6 # flip sign for next time
- j sinloop
- sindone:
- mov.s $f0,$f4 # set return value
- move $ra,$s0 # restore return address
- jr $ra
- # dbgflt -- debug print float number
- dbgflt:
- bnez $s7,prtflt
- jr $ra
- # dbgnum -- debug print int number
- dbgnum:
- beqz $s7,dbgnumdone
- li $v0,1
- syscall
- dbgnumdone:
- jr $ra
- # dbgprt -- debug print float number
- dbgprt:
- beqz $s7,dbgprtdone
- li $v0,4
- syscall
- dbgprtdone:
- jr $ra
- # prtflt -- print float number
- #
- # arguments:
- # a0 -- prefix string (symbol name)
- # f12 -- number to print
- prtflt:
- li $v0,4 # syscall: print string
- syscall
- li $v0,2 # print float
- syscall
- li $v0,4 # syscall: print string
- la $a0,nl # print newline
- syscall
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement