Domanda

when I run my program and input 20 degrees, the output is 8.8E-44 in MARS and 0.000000 in QtSpim. it should be 62.8

am I using the wrong instructions here?

.data
pi:     .float   3.14
c:      .word   180
initMSG: .asciiz "\nEnter an angle in degrees: "

.text
.globl main

main:
    mul $t5, $t2, $t6
add $t5, $t5, $sp
addi    $t5, $t5, 4
lw  $t9, 0($t5)

mul $t4, $t1, $t6
add $t4, $t4, $a0
sw  $t9, 0($t4)

addi    $t2, $t2, 1

addi    $t1, $t1, 1
j   forLoop3


Exit:
li $v0, 10          #load the system exit command 
syscall             #stop the program
È stato utile?

Soluzione

The problem is that pi is a float, but the value you grab from the user is a plain integer. You'd need to convert the user's value into a float before you multiply it with pi.

Fortunately you don't have to convert it manually because there's a syscall for reading floats instead of integers. Change the reading syscall from 5 to 6 (6 means read float), and delete the subsequent two lines (syscall 6 will read the float into $f0 directly). After doing that and running it again using 20 degrees, I get 62.800003 as the output (there will be a small amount of error because 3.14 can't be represented exactly using binary floats).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top