A third degree polynomial equation a cubic equation is of th
A third degree polynomial equation (a cubic equation) is of the form p(x) = c3x 3 + c2x 2 + c1x + c0, where x and the four coefficients are integers for this exercise. Suppose the values of the coefficients c0, c1, c2, and c3have been loaded into registers $t0, $t1, $t2, and $t3, respectively. Suppose the value of x is in $t7.
Write the MIPS32 instructions that would evaluate this polynomial, placing the result in $t9.
Solution
# A third degree polynomial equation is p(x) = c3x 3 + c2x 2 + c1x + c0,
.text
.globl main
main:
lw $t0,c0 # get c0 -- Load word of c0 to the register of t0
lw $t1,c1 # get c1 -- Load word of c1 to the register of t1
lw $t2,c2 # get c2 -- Load word of c2 to the register of t2
lw $t3,c3 # get c3 -- Load word of c3 to the register of t3
lw $t7,x # get x -- Load word of x to the register of t7
mult $t7,$t7 # x2 -- Multiply x => x*x=x2
mflo $t4 # $t4 = x2 -- move x2 to $t4
nop # No operation performed
mult $t4,$t7 # x3 -- Multiply x => x2*x=x3
mflo $t5 # $t5 = x3 -- move x3 to $t5
nop
mult $t1,$t7 # c1x -- Multiply c1 by x => c1*x=c1x
mflo $t6 # $t6 = c1x -- move c1x to $t6
nop
mult $t2,$t4 # c2x2 -- Multiply c2 by x2 => c2*x2=c2x2
mflo $t8 # $t8 = c2x2 -- move c2x2 to $t8
nop
mult $t3,$t5 # c3x3 -- Multiply c3 by x3 => c3*x3=c3x3
mflo $t9 # $t9 = c3x3
nop
addu $t9,$t9,$t8 # $t9 = c3x3 + c2x2 -- add c3x3 and c2x2. Hence c3x3+c2x2 and load it to $t9 register
addu $t9,$t9,$t6 # $t9 = c3x3 + c2x2 + c1x
addu $t9,$t9,$t0 # $t9 = c3x3 + c2x2 + c1x + c0
sw $t9,value # p(x) = c3x3 + c2x2 + c1x + c0 -- polynomial
