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