我试图划分MARS MIPS,但它给了我错误的答案

时间:2016-02-14 06:20:47

标签: assembly mars-simulator

.data
prompt1: .asciiz "Enter your first number:"
prompt2: .asciiz "Enter your second number:"
.text
main:

#displays "Enter your first number:"
li $v0 4
la $a0 prompt1
syscall

#Reads the next int and stores in $s0
li $v0 5
syscall
move $s0 $v0

#displays "Enter your second number:"
li $v0 4
la $a0 prompt2
syscall

#Reads next int and stores in $s1
li $v0 5
syscall 
move $s1 $v0


#Divides user input $s0/$s1 and stores in $t0
div $t0 $s0 $s1
syscall


#Print value of $t0
li $v0 1
move $t0 $v0
syscall

li $v0 10
syscall

我的代码的目标是向用户询问2个输入并获取这两个输入并将它们分开。但是,当我在输入“1”中运行程序时,我的输出是一个非常高的数字。

1 个答案:

答案 0 :(得分:0)

我看到了几件事:

  1. 除法指令后还有一个额外的系统调用。
  2. 如果要打印$ t0的值,请记住print_int系统调用将打印$ a0中的任何内容,而不是$ t0。现在,$ v0(即1)的内容被移入$ t0,但是print_int系统调用是打印$ a0,这是垃圾。
  3. 尝试这样做(除了删除上面的额外系统调用):

    #Print value of $t0
    li $v0 1
    move $a0 $t0
    syscall
    

    希望有所帮助。

相关问题