We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,363 Members — Technology Publication meets Social Media

# Evaluating A Polynomial With Stacks/Subroutines

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:
nop                      #
move    \$s0,\$v0          # save it in \$s0

nop                      #
move    \$s1,\$v0          # save it in \$s1

jal     poly
nop

addu    \$s0,\$s0,\$s1      # compute the sum

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

li  \$t0,0                 #Evaluate -2y2
mul \$t0, \$s1, \$s1
mul \$t0, \$t0, -2

li \$t0, 0                #Evaluate -5xy
mul \$t0, \$s0, \$s1
mul \$t0, \$t0, -5

li \$t0, 0                #Evaluate +20x
mul \$t0, \$s0, 20

li \$t0, 0                #Evaluate -16y
mul \$t0, \$s0, -16

li \$t0, 0                  #Evaluate +100
li \$t0, 100

# epilog
lw      \$s1,(\$sp)        # pop \$s1
lw      \$s0,(\$sp)        # pop \$s0
lw      \$ra,(\$sp)        # pop return address
nop

#
# on entry:
#
# on exit:
#    \$v0 -- the integer

.text
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 "
``````
1
Contributor
0
Replies
1
View
Question
userct
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10