MIPS Assembly (MARS 4.5): floating point arithmetric answer is weird

AerysS

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.

bhristov

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.

edited at
0

Comments

0 comments
Login to comment

Related

x86 Assembly (NASM): Floating Point Exception, not dividing by 0?

Floating point assembly on intel processor

Harsh differences in generated assembly of floating-point comparisons < and >=

perl integer arithmetic giving floating point answer

Understanding GCC's floating point constants in assembly listing output

Calling an assembly language function causes "floating point stack check" exception

Floating point not outputting right answer

Floating point division vs integer division gives different answer

ARMv8 floating point output inline assembly

MIPS: Division algorithm (Dividing significands of IEEE-754 format) gives incorrect answer for the last 4-5 bits (LSB)

Calling printf() in assembly causes a 'floating point exception'

manipulating mips assembly code to decrease cache miss rate (mars simulator)

How to print floating point numbers from assembly?

Is "jr $ra" required to end a MIPS assembly language program? (MARS & QtSpim behave differently!)

floating point accuracy in MIPS assembly

Integer Implementation of Floating-Point Addition In MIPS

Convert Binary to Decimal in MIPS, Assembly MARS

C weird approximation on floating point

Load address and Strings, Mars Mips

assembly floating point code error

Convert decimal to base 4 assembly (MIPS)

Regex behaving weird when finding floating point strings

Floating point is very weird in C language

Error in MIPS division with single precision floating point numbers

The 4 most significant bit in a jump instruction in MIPS Assembly

assembly-time evaluation of floating-point expressions

Assembly MIPS: Cannot debug floating-point power function

Compare a Floating Point value to 1 in MIPS assembler

Assembly floating point math