mipsmips32mars-simulatormips64

There is something wrong with the output of this program MIPS(double)


I am writing a program in MIPS to convert inches into centimeters, but the result is always evaluating to zero. I don't know where I did wrong. I have written the program below. It's compiling, but not evaluating the correct result, always giving 0.

#declaring some things

.data
    inchesText: .asciiz "Enter the number in inches: "
    resultText: .asciiz " Centimeters are ==> "
    inches: .double 0
    inchesToCenti: .double 2.54
    centi: .double 0
    zero: .word 0
    result: .double 0
.text

main:
    jal getInches
    jal inches_To_Centi
    jal finalResult

    jal Exit
getInches:
    # printing string
    la $a0,inchesText
    li $v0, 4
    syscall
    # get inches
    li $v0, 7
    syscall
    s.d $f2, inches
    jr $ra

inches_To_Centi:

    # loading the formula contstant as it is
    l.d $f0, inchesToCenti

    #actual inches gained through argument
    l.d $f2, inches

    # mul both of these to get the centimeters
    mul.d $f6, $f0, $f2
    s.d $f6, centi

    jr $ra

finalResult:
    # printing text
    la $a0, resultText
    li $v0, 4
    syscall

    # now printing the actual value
    l.d $f12, centi
    li $v0, 3
    syscall

Exit:
    li $v0, 10
    syscall

Solution

  • Is it a lot of time I do not do assemble but I think the solution of your problem is all about the system call you use to read the double value.

    The syscall 7 does not store the input value in the $f2 register but into $f0 one.

    Change the line #26 to

    s.d $f0 inches 
    

    To give a little more context, as the line number are not present, the getInches subroutine needs the fix:

    getInches:
        # printing string
        la $a0,inchesText
        li $v0, 4
        syscall
        # get inches
        li $v0, 7
        syscall
        s.d $f0, inches
        jr $ra