Skip to content

Instantly share code, notes, and snippets.

@hc5
Created April 3, 2012 06:34
Show Gist options
  • Select an option

  • Save hc5/2289794 to your computer and use it in GitHub Desktop.

Select an option

Save hc5/2289794 to your computer and use it in GitHub Desktop.
.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
INTERVAL: .float 0,2
POLYORDER: .word 0
COEFFICIENTS:
.space 40 #; enought for up to degree 10.. should be enough
powTests: .asciiz "Tests for power\n---------------------------\n"
pow1: .float 1.3
pow1ex: .float 3.71293
pow2: .float -2.5
pow2ex: .float -3814.6972
pow3: .float 1000000
pow3ex: .float 1000000
evaluatePTests: .asciiz "\nTests for evaluateP\n---------------------------\n"
ep1: .asciiz "Testing x^3 - 2x^2 + 3x + 4 at x = 1.1\n"
ep1order: .word 3
ep1coef: .word 1,-2,3,4
ep1x: .float 1.1
ep1ex: .float 6.211
ep2: .asciiz "Testing 5x^4 + 100x^2 - 5x - 1 at x = 5.5\n"
ep2order: .word 4
ep2coef: .word 5,0,100,-5,-1
ep2x: .float 5.5
ep2ex: .float 7571.81
ep3: .asciiz "Testing -100x^2 - 100x + 100 at x = -10\n"
ep3order: .word 2
ep3coef: .word -100,-100,100
ep3x: .float -10
ep3ex: .float -8900
findRootTests: .asciiz "\nTests for findRoot\n---------------------------\n"
fr1: .asciiz "Testing x^3 - 2x^2 + 3x + 4 for x between -1 and 0\n"
.align 4
fr1Int: .float -1,0
fr1ex: .float -0.776045
fr2: .asciiz "Testing 5x^4 + 100x^2 - 5x - 1 for x between -100 and 0\n"
.align 4
fr2Int: .float -100,0
fr2ex: .float -0.0780686
fr3: .asciiz "Testing 5x^4 + 100x^2 - 5x - 1 for x between 0 and 20\n"
.align 4
fr3Int: .float 0,20
fr3ex: .float 0.128012
fr4: .asciiz "Testing -100x^2 - 100x + 100 for x between -25.2 and -1\n"
.align 4
fr4Int: .float -25.2,-1
fr4ex: .float -1.61803
fr5: .asciiz "Testing -100x^2 - 100x + 100 for x between 0.5 and 1\n"
.align 4
fr5Int: .float 0.5,1
fr5ex: .float 0.618034
#; DON'T TOUCH
__regsav: .space 20
__exc0_msg: .asciiz "[Interrupt] "
__exc1_msg: .asciiz "[TLB]"
__exc2_msg: .asciiz "[TLB]"
__exc3_msg: .asciiz "[TLB]"
__exc4_msg: .asciiz "[Address error in inst/data fetch] "
__exc5_msg: .asciiz "[Address error in store] "
__exc6_msg: .asciiz "[Bad instruction address] "
__exc7_msg: .asciiz "[Bad data address] "
__exc8_msg: .asciiz "[Error in syscall] "
__exc9_msg: .asciiz "[Breakpoint] "
__exc10_msg: .asciiz "[Reserved instruction] "
__exc11_msg: .asciiz ""
__exc12_msg: .asciiz "[Arithmetic overflow] "
__exc13_msg: .asciiz "[Trap] "
__exc14_msg: .asciiz ""
__exc15_msg: .asciiz "[Floating point] "
__exc_msg_table:
.word __exc0_msg, __exc1_msg, __exc2_msg, __exc3_msg, __exc4_msg
.word __exc5_msg, __exc6_msg, __exc7_msg, __exc8_msg, __exc9_msg
.word __exc10_msg, __exc11_msg, __exc12_msg, __exc13_msg, __exc14_msg
.word __exc15_msg
__ex: .asciiz "Expected: "
__actual: .asciiz " \tActual: "
__pass: .asciiz "\t\t\t\tTest passed!\n"
__fail: .asciiz "\t\t\t\tTest failed!***\n"
__exc: .asciiz "Exception - "
__endmsg: .asciiz " tests ran in "
__ms: .asciiz " ms\n------------------------\n"
__failmsg: .asciiz " failures"
__startT: .word 0
__fails: .word 0
__total: .word 0
__ferror: .float 0.01
.ktext 0x80000180 #; might be different, check your settings for exception handler address
la $k1, __regsav
sw $v0, 0($k1)
sw $a0, 4($k1)
li $v0, 4
la $a0, __exc
syscall
mfc0 $k0, $13
andi $k0, $k0, 0x7c
lw $a0, __exc_msg_table($k0)
li $v0, 4
syscall
lw $v0, 0($k1)
lw $a0, 4($k1)
la $k0, __exception
mtc0 $k0, $14
eret
.text
main:
jal __setup
la $t8, power
li $v0, 4
la $a0, powTests
syscall
li $a0, 5
l.s $f12, pow1
l.s $f18, pow1ex
jal testFloat
li $a0, 9
l.s $f12, pow2
l.s $f18, pow2ex
jal testFloat
li $a0, 1
l.s $f12, pow3
l.s $f18, pow3ex
jal testFloat
li $v0, 4
la $a0, evaluatePTests
syscall
la $t8, evaluateP
li $v0, 4
la $a0, ep1
syscall
lw $t0, ep1order
sw $t0, POLYORDER
la $a0, ep1coef
li $a1, 16
la $v0, COEFFICIENTS
jal memmove
l.s $f12, ep1x
l.s $f18, ep1ex
jal testFloat
li $v0, 4
la $a0, ep2
syscall
lw $t0, ep2order
sw $t0, POLYORDER
la $a0, ep2coef
li $a1, 20
la $v0, COEFFICIENTS
jal memmove
l.s $f12, ep2x
l.s $f18, ep2ex
jal testFloat
li $v0, 4
la $a0, ep3
syscall
lw $t0, ep3order
sw $t0, POLYORDER
la $a0, ep3coef
li $a1, 12
la $v0, COEFFICIENTS
jal memmove
l.s $f12, ep3x
l.s $f18, ep3ex
jal testFloat
li $v0, 4
la $a0, findRootTests
syscall
la $t8, findRoot
li $v0, 4
la $a0, fr1
syscall
lw $t0, ep1order
sw $t0, POLYORDER
la $a0, ep1coef
li $a1, 16
la $v0, COEFFICIENTS
jal memmove
l.d $f12, fr1Int
l.s $f18, fr1ex
jal testFloat
li $v0, 4
la $a0, fr2
syscall
lw $t0, ep2order
sw $t0, POLYORDER
la $a0, ep2coef
li $a1, 20
la $v0, COEFFICIENTS
jal memmove
l.d $f12, fr2Int
l.s $f18, fr2ex
jal testFloat
li $v0, 4
la $a0, fr3
syscall
l.d $f12, fr3Int
l.s $f18, fr3ex
jal testFloat
li $v0, 4
la $a0, fr4
syscall
lw $t0, ep3order
sw $t0, POLYORDER
la $a0, ep3coef
li $a1, 20
la $v0, COEFFICIENTS
jal memmove
l.d $f12, fr4Int
l.s $f18, fr4ex
jal testFloat
li $v0, 4
la $a0, fr5
syscall
l.d $f12, fr5Int
l.s $f18, fr5ex
jal testFloat
j __teardown
__setup:
li $v0, 30
syscall
sw $a0, __startT
jr $ra
__teardown:
li $a0, 10
li $v0, 11
syscall
syscall
li $v0, 1
lw $a0, __total
syscall
li $v0, 4
la $a0, __endmsg
syscall
li $v0, 30
syscall
addi $t0, $a0, 0
lw $a0, __startT
sub $a0, $t0, $a0
li $v0, 1
syscall
li $v0, 4
la $a0, __ms
syscall
lw $a0, __fails
li $v0, 1
syscall
la $a0, __failmsg
li $v0, 4
syscall
li $v0, 10
syscall
#; a0 - start of memory region
#; a1 - length
#; v0 - start of copied memory region
memcpy:
addi $t0, $a0, 0
addi $t1, $a1, 0
li $v0, 9
addi $a0, $a1, 0
syscall
addi $t2, $v0, 0
add $t4, $t0, $t1
__copyword:
sub $t5, $t0, $t4
beq $t5, $zero, __endcopy
bgtz $t5, __copybytes
lw $t3, ($t0)
sw $t3, ($t2)
addi $t0, $t0, 4
addi $t2, $t2, 4
b __copyword
__copybytes:
xor $t5, $t5, $t5
addi $t0, $t0, -4
addi $t2, $t2, -4
__byteloop:
beq $t0, $t4, __endcopy
lb $t6, ($t0)
sb $t6, ($t2)
addi $t0, $t0, 1
addi $t2, $t2, 1
b __byteloop
__endcopy:
jr $ra
#; a0 - start of memory region
#; a1 - length
#; v0 - start of destination memory region
memmove:
addi $t0, $a0, 0
addi $t1, $a1, 0
addi $t2, $v0, 0
add $t4, $t0, $t1
__mmcopyword:
sub $t5, $t0, $t4
beq $t5, $zero, __mmendcopy
bgtz $t5, __mmcopybytes
lw $t3, ($t0)
sw $t3, ($t2)
addi $t0, $t0, 4
addi $t2, $t2, 4
b __mmcopyword
__mmcopybytes:
xor $t5, $t5, $t5
addi $t0, $t0, -4
addi $t2, $t2, -4
__mmbyteloop:
beq $t0, $t4, __mmendcopy
lb $t6, ($t0)
sb $t6, ($t2)
addi $t0, $t0, 1
addi $t2, $t2, 1
b __mmbyteloop
__mmendcopy:
jr $ra
#; a0-a3, f12-f16 - args to the function to test
#; t8 - function to test
#; f18 - expected value
testFloat:
lw $t0, __total
addi $t0, $t0, 1
sw $t0, __total
sw $ra, ($sp)
addi $sp, $sp, -4
s.s $f18, ($sp)
addi $sp, $sp, -4
sw $t9, ($sp)
addi $sp, $sp, -4
sw $t8, ($sp)
addi $sp, $sp, -4
jalr $t8
addi $sp, $sp, 4
lw $t8, ($sp)
addi $sp, $sp, 4
lw $t9, ($sp)
addi $sp, $sp, 4
l.d $f18, ($sp)
addi $t0, $v0, 0
la $a0, __ex
li $v0, 4
syscall
mov.s $f12, $f18
li $v0, 2
syscall
la $a0, __actual
li $v0, 4
syscall
mov.s $f12, $f0
li $v0, 2
syscall
sub.s $f0, $f18, $f0
abs.s $f0, $f0
l.s $f2, __ferror
c.le.s $f0, $f2
bc1t __success
b __failure
#; a0-a3 - args to the function to test
#; t8 - function to test
#; t9 - expected value
test:
lw $t0, __total
addi $t0, $t0, 1
sw $t0, __total
sw $ra, ($sp)
addi $sp, $sp, -4
s.s $f18, ($sp)
addi $sp, $sp, -4
sw $t9, ($sp)
addi $sp, $sp, -4
sw $t8, ($sp)
addi $sp, $sp, -4
jalr $t8
addi $sp, $sp, 4
lw $t8, ($sp)
addi $sp, $sp, 4
lw $t9, ($sp)
addi $sp, $sp, 4
l.s $f18, ($sp)
addi $t0, $v0, 0
la $a0, __ex
li $v0, 4
syscall
addi $a0, $t9, 0
li $v0, 1
syscall
la $a0, __actual
li $v0, 4
syscall
addi $a0, $t0, 0
li $v0, 1
syscall
beq $t0, $t9, __success
b __failure
__exception:
addi $sp, $sp, 4
lw $t8, ($sp)
addi $sp, $sp, 4
lw $t9, ($sp)
addi $sp, $sp, 4
l.s $f18, ($sp)
j __failure
__success:
la $a0, __pass
j __end
__failure:
la $a0, __fail
lw $t0, __fails
addi $t0, $t0, 1
sw $t0, __fails
__end:
li $v0, 4
syscall
addi $sp, $sp, 4
lw $ra, ($sp)
jr $ra
#; TODO: Insert your functions here
#; $f12 is a
#; $f13 is b
#; $f0 is the value of a root
findRoot:
jr $ra
#; $f12 is x
#; $a0 is n
#; $f0 is x^n
power:
jr $ra
#; $f12 is x
#; $f0 is the polynomial at x
evaluateP:
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment