Stack-based Evaluation and Simple Subroutine Linkage
Hello, I have to Write a program that evaluates 3x2 – 2y2 – 5xy + 20x – 16y + 100 for integers x and y that the user enters.
Prompt the user for x and y. Use pread from chapter 26 for this. Use it as it is, as if it were a library function which you can’t modify.
Write a subroutine, with entry point poly, that evaluates the polynomial. The subroutine takes two arguments, x and y, in registers $a0 and $a1 and returns the value of the polynomial in register $v0.
I've been doing pretty well up to this point. When my program executes and run it accepts my integers but the math performed is completely wrong. I'm having trouble grasping how to do the math exactly and feel I did it very wrong. I haven't worked with stacks at all so it's new to me.
Does anyone have any suggestions on how I could fix this?
Thank you for all your help.
## Evaluate a Polynomial 3x2 – 2y2 – 5xy + 20x – 16y + 100
#
# This program uses simple linkage.
#
# Settings: Load delays ON; Branch delays ON
# Trap file ON; Pseudoinstructions ON
#
## Registers:
## $s0: x
## $s1: y
## $v0: Sum
## $t0 as temporary?
.text
.globl main
main:
jal pread # read first integer (x)
nop #
move $s0,$v0 # save it in $s0
jal pread # read second integer (y)
nop #
move $s1,$v0 # save it in $s1
jal poly
nop
addu $s0,$s0,$s1 # compute the sum
addu $a0,$s0,$s2
la $a0,end # print string
li $v0,4 # service 4
syscall
li $v0,1 # print the sum
syscall
li $v0,10 # exit
syscall
.data
end: .asciiz "The end result of 3x2 - 2y2 - 5xy + 20x - 16y + 100 is: "
#Begin polynomial (poly subroutine)
.text
.globl poly
poly:
# prolog
sub $sp,$sp,4 # push the return address
sw $ra,($sp)
sub $sp,$sp,4 # push $s0
sw $s0,($sp)
sub $sp,$sp,4 # push $s1
sw $s1,($sp)
li $t0, 0 #Evaluate 3x2
mul $t0, $s0, $s0
mul $t0, $t0, 3
addu $v0, $v0, $t0
li $t0,0 #Evaluate -2y2
mul $t0, $s1, $s1
mul $t0, $t0, -2
addu $v0, $v0, $t0
li $t0, 0 #Evaluate -5xy
mul $t0, $s0, $s1
mul $t0, $t0, -5
addu $v0, $v0, $t0
li $t0, 0 #Evaluate +20x
mul $t0, $s0, 20
addu $v0, $v0, $t0
li $t0, 0 #Evaluate -16y
mul $t0, $s0, -16
addu $v0, $v0, $t0
li $t0, 0 #Evaluate +100
li $t0, 100
addu $v0, $v0, $t0
# epilog
lw $s1,($sp) # pop $s1
add $sp,$sp,4
lw $s0,($sp) # pop $s0
add $sp,$sp,4
lw $ra,($sp) # pop return address
add $sp,$sp,4
jr $ra # return to caller
nop
# pread -- prompt for and read an integer
#
# on entry:
# $ra -- return address
#
# on exit:
# $v0 -- the integer
.text
.globl pread
pread:
la $a0,prompt # print string
li $v0,4 # service 4
syscall
li $v0,5 # read int into $v0
syscall # service 5
jr $ra # return
nop #
.data
prompt:
.asciiz "Enter an integer "