[MIPS Assembly] Need help handling negative numbers while calculating a polynomial. via /r/learnprogramming


[MIPS Assembly] Need help handling negative numbers while calculating a polynomial.

I was tasked to calculate 6x3 – 3x2 + 7x+2, then store it to $15. While my program correctly calculates with positive values, I get wrong answers using negatives. Comments are what I think it should be happening.

Using x = 2, I get in $15 = 0x34 (52 in base 10), which is correct.

Using x =-2, I get in $15 = 0xFFFF FFB8. What am I missing?

# This program evaluates the polynomial # 6x^3 -3x^2 + 7x+2 and places the result in $15. # $8 = value of x # $9 = Constant # $10 = result of 6x^3 # $11 = result of -3x^2 # $12 = result of 7x+2 # $15 = End result of the polynomial .text .globl main main: # 6(x^3) ori $9,$0,0x6 # assign 0x6 to $9 addiu $8,$0,2 # assign example variable (x) 0x2 to $8 mult $8,$8 # 0x2x0x2 = 0x4 mflo $10 # Load 0x4 to $8 mult $10,$8 # 0x4*0x2 = 0x8 mflo $10 # 0x8 mult $9,$10 # multiplies 6 times the value of $10 (0x8) mflo $10 # 0x30 = 48 in decimal notation add $15,$0,$10 # Sum the result to $15 # -3(x^2) ori $9,$0,0x3 # change constant to 0x3 mult $8,$8 # 0x2*0x2 = 0x4 mflo $11 # $11 = 0x4 mult $9,$11 # 0x4 * 0x3 = 0xC mflo $11 # Load result to $11 subu $15,$15,$11 # 0x30 (48) - 0xC (12) # 7x+2 ori $9,$0,0x7 # change constant to 0x7 mult $8,$9 # 0x2*0x7 = 0xE mflo $12 # Load result to $12 addi $12,$12,0x2 # sum 0xE + 0x2 add $15,$15,$12 # final sum and result of the polynomial 

Submitted July 09, 2017 at 11:54PM by MrPootisBrights
via reddit http://ift.tt/2tEyRae

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s