我的MIPS程序认为每个用户输入都是数字4

时间:2015-03-09 10:47:36

标签: assembly input integer output mips

创建此汇编代码是为了将两个数字相乘 我已经编程.s程序并在QTSpim中执行它们3天了,尽管我已经学习了超过2个月如何这样做。我了解到您可以将整数作为用户输入并将其存储到临时寄存器中以供使用;但是,当我尝试这个简单的任务时,我发现我的用户输入总是变成相同的数字:4。代码如下所示。

在寻找问题时的注意事项:

  • 我不担心在搜索此代码中的问题时会阻止负面输入的程序部分,但是我已将其保留在以防出现问题的地方。
  • 仅仅从我尝试修复此程序中学到的东西,我认为我如何定义和使用arg1和arg2可能存在问题。如果您正在寻找它们,它们会在代码顶部附近创建,并且在检索,存储和加载用户输入到临时寄存器($ t2和$ t3)中是不可或缺的。几乎在代码的每一步。
  • 答案总是存储在$ t1中,而$ t1总是从0开始。在对代码进行逐行评估之后,我可以得出结论,寄存器$ t1没有任何问题所以我不会&# 39;不要再看那个了。
  • 跟随井号(#)的任何内容都将成为我在代码每一步的思考过程。这对于弄清楚我哪里出错非常有用。
  • 缩进在Stack Overflow上看起来有点不同,但如果您认为问题可能来自缩进,请随意指出,我不知道不正确的缩进是否会导致错误

代码:

.data   

request_input:
.asciiz "Please enter the numbers you would like to multiply: \n"
give_output:
.asciiz "The numbers multiply to give: "
neg_input:
.asciiz "Sorry, this program doesn't work with negative inputs. \n"
arg1: .word 4
arg2: .word 4

.text
.globl main

main:
#Output the request for user input
la  $a0, request_input
li  $v0, 4
syscall

#Takes the user's input and stores it to temp registers
la  $a0, arg1
la  $a1, arg1
li  $v0, 5 #op code for getting an integer input
syscall
lw  $t2, arg1 #store the input to temp register 2

la  $a0, arg2
la  $a1, arg2
li  $v0, 5 #op code for getting an integer input
syscall
lw  $t3, arg2 #store the input to temp register 3

#sets t1 to zero so we can reliably store the answer into it
addi    $t1, $zero, 0

#checks if the arguments are less than zero, if so goes to output zero
blez    $t2, negzero
blez    $t3, negzero
bgtz    $t3, fori

negzero:
#what happens if an argument is negative
la  $a0, neg_input
li  $v0, 4
syscall

la  $a0, 0($t1)
li  $v0, 1
syscall

li  $v0, 10
syscall

fori:
#Does the math
add $t1, $t1, $t2
addi    $t3, $t3, -1
bnez    $t3, fori

fin:
#Outputs the answer
la  $a0, give_output
li  $v0, 4
syscall

la  $a0, 0($t1)
li  $v0, 1
syscall

li  $v0, 10
syscall}

0 个答案:

没有答案
相关问题