Created
April 15, 2012 04:38
-
-
Save hc5/2390061 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #; Yang Zhou 260366321 | |
| .data | |
| ZERO: .float 0.0 | |
| ONE: .float 1.0 | |
| TWO: .float 2.0 | |
| a: .float 0.7 | |
| b: .float 1.375 | |
| EPSILON: .float 0.0001 #; Value e in algorithm above. | |
| INTERVAL: .float -5,5 #; Root is between these values. | |
| POLYORDER: .word 11 #; Order of polynomial. | |
| COEFFICIENTS: #; Integer coefficient of highest | |
| .word 10,-2,2,-5,5,-5,5,0,1,-3,5,-5 #; order comes first in list. | |
| .text | |
| main: | |
| l.d $f12, INTERVAL | |
| jal findRoot | |
| li $v0, 2 | |
| mov.s $f12, $f0 | |
| syscall | |
| li $v0, 10 | |
| syscall | |
| #; $f12 is a | |
| #; $f13 is b | |
| #; $f0 is the value of a root | |
| findRoot: | |
| sw $ra, ($sp) | |
| s.s $f20, -4($sp) | |
| s.s $f22, -8($sp) | |
| s.s $f24, -12($sp) | |
| addi $sp, $sp, -16 | |
| add.s $f4, $f12, $f13 #; save registers onto stack | |
| l.s $f6, TWO | |
| div.s $f20, $f4, $f6 | |
| mov.s $f22, $f12 | |
| mov.s $f24, $f13 #; c = $f20, a = $f22, b = $f23 | |
| mov.s $f12, $f20 | |
| sw $ra, ($sp) | |
| addi $sp, $sp, -4 | |
| jal evaluateP | |
| addi $sp, $sp, 4 | |
| lw $ra, ($sp) | |
| l.s $f4, ZERO | |
| c.eq.s $f4, $f0 | |
| mov.s $f2, $f0 | |
| mov.s $f0, $f20 | |
| bc1t __return #; p(c) == 0 | |
| sub.s $f4, $f22, $f24 | |
| abs.s $f4, $f4 | |
| l.s $f6, EPSILON | |
| c.lt.s $f4, $f6 | |
| bc1t __return #; if abs(a-b) < epsilon return c | |
| mov.s $f12, $f22 | |
| sw $ra, ($sp) | |
| addi $sp, $sp, -4 | |
| jal evaluateP | |
| addi $sp, $sp, 4 | |
| lw $ra, ($sp) | |
| mul.s $f6, $f2, $f0 #; p(a)*p(b) | |
| l.s $f4, ZERO | |
| c.lt.s $f4, $f6 #; test greater than zero | |
| movt.s $f12, $f20 | |
| movt.s $f13, $f24 | |
| movf.s $f12, $f22 | |
| movf.s $f13, $f20 #; if true, a = c, b = b, if false, a = c, b = c | |
| addi $sp, $sp, 12 | |
| l.s $f24, -8($sp) | |
| l.s $f22, -4($sp) | |
| l.s $f20, ($sp) #; pop all the values off the stack since we no longer need them | |
| jal findRoot #; recursive call | |
| addi $sp, $sp, 4 #; return, pop the function off the stack | |
| lw $ra, ($sp) | |
| jr $ra | |
| __return: #; return c | |
| addi $sp, $sp, 16 | |
| l.s $f24, -12($sp) | |
| l.s $f22, -8($sp) | |
| l.s $f20, -4($sp) | |
| lw $ra, ($sp) | |
| jr $ra | |
| #; $f12 is x | |
| #; $a0 is n | |
| #; $f0 is x^n | |
| power: | |
| sw $ra, ($sp) | |
| addi $sp, $sp, -4 | |
| beq $a0, $zero, __return1 #; if zero, return 1 | |
| b __else | |
| __return1: | |
| l.s $f0, ONE | |
| addi $sp, $sp, 4 | |
| jr $ra | |
| __else: | |
| mov.s $f4, $f12 | |
| addi $a0, $a0, -1 | |
| jal power | |
| mul.s $f0, $f0, $f4 #; else return x * pow(x, n-1) | |
| addi $sp, $sp, 4 | |
| lw $ra, ($sp) | |
| jr $ra | |
| #; $f12 is x | |
| #; $f0 is the polynomial at x | |
| evaluateP: | |
| lw $a0, POLYORDER | |
| li $t0, 0 | |
| l.s $f0, ZERO #; $f0 starts at 0 | |
| __start: | |
| bltz $a0, __end | |
| sw $a0, ($sp) | |
| addi $sp, $sp, -4 | |
| s.s $f0, ($sp) | |
| addi $sp, $sp, -4 | |
| sw $ra, ($sp) | |
| addi $sp, $sp, -4 | |
| jal power #; calculate x to the current power | |
| addi $sp, $sp, 4 | |
| lw $ra, ($sp) | |
| lwc1 $f4, COEFFICIENTS($t0) | |
| cvt.s.w $f4, $f4 | |
| mul.s $f4, $f4, $f0 #; load and multiply by the coefficient | |
| addi $sp, $sp, 4 | |
| l.s $f0, ($sp) | |
| add.s $f0, $f4, $f0 #; add it to f0 | |
| addi $t0, $t0, 4 | |
| addi $sp, $sp, 4 | |
| lw $a0, ($sp) | |
| addi $a0, $a0, -1 | |
| b __start | |
| __end: | |
| jr $ra | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment