title: mips program to calculate sin(x)
body: <p>I have this equation:
  <a href="http://i.stack.imgur.com/YtC9a.png" rel="nofollow">Summation of sin(x)</a></p>
  
  <p>I am trying to calculate this by creating a program on MIPS. However, it outputs incorrect BIG numbers! I have no idea where is the stupid mistake I have made. 
  I had created three functions, one that calculates the factorial, another for the power, and the third big one is the sin function. </p>
  
  <pre><code>#read integer
  li $v0, 5
  syscall
  
  addi $a0, $v0, 0 #argument of sin function, a0 = x
  li $t4, 1 #n starting from 1
  addi $s7, $v0, 0 #sum = x
  jal sin
  j end
  
  sin:
      Loop:
      slti $t5, $t4, 5 #t0 &lt; 5 (n &lt; 5)
      beq $t5, 0, exitLoop
      addi $sp, $sp, -8 # adjust stack for 2 items
      sw $ra, 4($sp) # save return address
      sw $a0, 0($sp) # save argument
      li $a0, -1 #argument of pow function, number = -1
      addi $a1, $t4, 0 #argument of pow function, power = t4
      jal power
      addi $s0, $v0, 0 #s0 = v0 (return value of pow)
      lw $a0, 0($sp) # restore original x
  
      sll $a1, $t4, 1 #a1 = 2n 
      addi $a1, $a1, 1 #a1 = 2n+1
      jal power
      addi $s1, $v0, 0 #s0 = v0 (return value of pow)
  
      sll $a0, $t4, 1 #a0 = 2n
      addi $a0, $a0, 1 #a0 = 2n + 1
      jal factorial
      addi $s2, $v0, 0 #s0 = v0 (return value of pow)
      lw $a0, 0($sp) # restore original n
      lw $ra, 4($sp) # and return address
  
      mult $s0, $s1 # LO = (-1)^n * x^(2n+1)
      mflo $s3 # S3 = LO
      div $s3, $s2 # s3 / (2n+1!)
      mflo $s3
      add $s7, $s7, $s3 #sum = sum + s3
      addi $t4, $t4, 1 #n++
  
      j Loop  
      exitLoop:
  addi $v1, $s7, 0
  addi $sp, $sp, 8
  jr $ra
  
  power:
      addi $t0 $a0, 0 #t0 = a0
      li $t1, 0 #i = 0
      loop:
      slt $t3, $t1, $a1 #if i &lt; n
      beq $t3, 0, exit 
      mult $t0, $a0 #t0 * a0
      mflo $t0 #LO = t0
      addi $t1, $t1, 1
      j loop
      exit:
      addi $v0, $t0, 0
      jr $ra
  
  factorial:
      li $t0, 1
      bgt $a0, $t0, L1
      li $v0, 1
      jr $ra
      L1:
      addi $sp, $sp, -8 # adjust stack for 2 items
      sw $ra, 4($sp) # save return address
      sw $a0, 0($sp) # save argument
      addi $a0, $a0, -1 # decrement n
      jal factorial # recursive call
      lw $a0, 0($sp) # restore original n
      mul $v0, $a0, $v0 # multiply to get result
      lw $ra, 4($sp) # and return address
      addi $sp, $sp, 8 # pop 2 items from stack
      jr $ra # and return
  end:
  li $v0, 1
  addi $a0, $v1, 0
  syscall
  </code></pre>