I'm making a MIPS program to calculate 5.4xy - 12.3y + 18.23x - 8.23y where x and y are inputs from console. However, the result is too weird. Here is my code:
.data
promptX: .asciiz "Enter x: \n"
promptY: .asciiz "Enter y: \n"
result: .asciiz "Result: "
first: .float 5.40
second: .float -12.30
third: .float 18.23
fourth: .float -8.23
.text
# Print prompt to input x
li $v0, 4
la $a0, promptX
syscall
# Get x, store in f2
li $v0, 7
syscall
mov.d $f2, $f0
# Prompt input y
li $v0, 4
la $a0, promptY
syscall
# Get y, store in f4
li $v0, 7
syscall
mov.d $f4, $f0
# f6 = xy
mul.d $f6, $f2, $f4
# load 5.4 to f8
lwc1 $f8, first
# f6 = 5.4xy
mul.d $f6, $f6, $f8
# load -12.3 to f8
lwc1 $f8, second
# f8 = -12.3y
mul.d $f8, $f8, $f4
# f6 = 5.4xy - 12.3y
add.d $f6, $f6, $f8
# load 18.23 to f8
lwc1 $f8, third
# f8 = 18.23x
mul.d $f8, $f8, $f2
# f6 = 5.4xy - 12.3y + 18.23x
add.d $f6, $f6, $f8
# load -8.23 to f8
lwc1 $f8, fourth
# f6 = 5.4xy - 12.3y + 18.23x + (- 8.23)
add.d $f12, $f6, $f8
# Print answer
li $v0, 4
la $a0, result
syscall
li $v0, 2
syscall
# End program
li $v0, 10
syscall
When I input x = 2.13 and y = 2.13 it returns 1.26719E-10, where it should be 28.90016. I recheck the code many times but still don't know why. Any help is appreciated.
This will work:
.data
promptX: .asciiz "Enter x: \n"
promptY: .asciiz "Enter y: \n"
result: .asciiz "Result: "
first: .double 5.40
second: .double -12.30
third: .double 18.23
fourth: .double -8.23
.text
# Print prompt to input x
li $v0, 4
la $a0, promptX
syscall
# Get x, store in f2
li $v0, 7
syscall
mov.d $f2, $f0
# Prompt input y
li $v0, 4
la $a0, promptY
syscall
# Get y, store in f4
li $v0, 7
syscall
mov.d $f4, $f0
# f6 = xy
mul.d $f6, $f2, $f4
#mul.d $f2, $f4, $f6
# load 5.4 to f8
l.d $f8, first
# f6 = 5.4xy
#mul.d $f6, $f6, $f8
mul.d $f6, $f6, $f8
# load -12.3 to f8
l.d $f8, second
# f8 = -12.3y
mul.d $f8, $f8, $f4
# f6 = 5.4xy - 12.3y
add.d $f6, $f6, $f8
# load 18.23 to f8
l.d $f8, third
# f8 = 18.23x
mul.d $f8, $f8, $f2
# f6 = 5.4xy - 12.3y + 18.23x
add.d $f6, $f6, $f8
# load -8.23 to f8
l.d $f8, fourth
# f6 = 5.4xy - 12.3y + 18.23x + (- 8.23)
add.d $f12, $f6, $f8
# Print answer
li $v0, 4
la $a0, result
syscall
li $v0, 3
syscall
# End program
li $v0, 10
syscall
You were mixing precisions. I made everything to be double. In the end you were trying to print a float with 2 in v0, you need 3 in v0 to print a double.
Another problem that I noticed was the way that you were loading data from memory to your registers. I switched those to l.d, and the values were loaded correctly.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments