Skip to content

Instantly share code, notes, and snippets.

@jpgg
Created March 25, 2012 23:36
Show Gist options
  • Save jpgg/2201635 to your computer and use it in GitHub Desktop.
Save jpgg/2201635 to your computer and use it in GitHub Desktop.
Programs to test 273 A3 assignment
# To use, first add your evaluateP and power functions to the bottom of the
# file where specified.
# Next, edit the values of POLYORDER, COEFFICIENTS, and ARGX to whatever you
# want. Test what the polynomial will return with something like wolfram alpha
# and then assemble it in Mars and run it. The program just returns the final
# value.
.data
ZERO: .float 0.0
ONE: .float 1.0
result: .asciiz "Result: "
# ARGUMENTS -- Edit these!
POLYORDER: .word 3 # order of polynomial
COEFFICIENTS: .word 5,2,-2,-3 # integer coefficient of highest order
ARGX: .float 3.2 # value of x in polynomial
# --
.text
#.align 2
.globl main
main:
# $f23 stores zero
la $t0, ZERO
lwc1 $f23, 0($t0)
# put x into $f12
la $t0, ARGX
lwc1 $f12, 0($t0)
addi $sp, $sp, -4
sw $ra, 0($sp)
# call function evaluateP
jal evaluateP
# print message
li $v0, 4
la $a0, result
syscall
# move result from $f0 to $f12
add.s $f12, $f23, $f23
add.s $f12, $f12, $f0
# print result
li $v0, 2
syscall
# end program
end:
li $v0, 10
syscall
evaluateP:
# PASTE YOUR evaluateP FUNCTION HERE
power:
# PASTE YOUR power FUNCTION HERE
# To use, first add your power function to the bottom of the file.
# Next, edit the values of ARGX and ARGN. These represent x and n (x^n)
# Assemble in Mars and run and it will print the result of x^n
.data
ZERO: .float 0.0
ONE: .float 1.0
up: .asciiz "^"
equals: .asciiz " = "
# ARGUMENTS (X^N) -- Edit these!
ARGX: .float 4.0
ARGN: .word 1
# --
.text
#.align 2
.globl main
main:
# $f23 stores zero
la $t0, ZERO
lwc1 $f23, 0($t0)
# set args
la $t0, ARGX
lwc1 $f12, 0($t0)
la $t0, ARGN
lw $a0, 0($t0)
addi $sp, $sp, -4
sw $ra, 0($sp)
# call function power
jal power
# print x
la $t0, ARGX
lwc1 $f12, 0($t0)
li $v0, 2
syscall
# print ^
li $v0, 4
la $a0, up
syscall
# print n
la $t0, ARGN
lw $a0, 0($t0)
li $v0, 1
syscall
# print =
li $v0, 4
la $a0, equals
syscall
# move result from $f0 to $f12
add.s $f12, $f23, $f23
add.s $f12, $f12, $f0
# print result
li $v0, 2
syscall
# end program
end:
li $v0, 10
syscall
power:
# PASTE YOUR power FUNCTION HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment