我正在尝试编写一个MIPS汇编程序,以用户那里获取两个整数,将其保存到数组中的内存中并打印出来。这就是我到目前为止所拥有的。我的程序打印了一些我没有输入的大数字。我对这个游戏很陌生。请有人帮忙!
这是我的代码:
.text
.globl main
main:
li $v0, 4
la $a0, prompt
syscall
li $t0, 0 #count for the loop to get two integers
getnum:
li $v0, 5 #read integer
syscall
sw $v0, num($s0) #save the integer from user input into num and $s0 has address for num, I'm not sure if i did this right
addi $s0, $s0, 4 # increment $s0 by 4 to save another integer
addi $t0, $t0, 1 #increment the counter
ble $t0, 1, getnum #if counter $t0, is less then or equal to 1, it will go through the loop again
printnum:
la $a0, num($s0) #load address of num to print
li $v0, 1 #print int
syscall
addi $s0, $s0, 4
addi $t1, $t1, 1
ble $t1, 1, printnum #does it twice
li $v0, 10
syscall
.data
num:
.word 0, 0 # i want to store my two numbers here
prompt:
.asciiz "Enter 2 positive integers: "
你的问题是双重的。
首先,加载整数的地址而不是实际整数。要修复此更改,la
lw
。
其次,由于您在getnum
循环中将$s0
递增两次,并立即在printnum
循环中使用它,因此您需要添加move $s0, $zero
来解决此问题。
此外,您的代码似乎依赖于这样一个事实,即$s0
以值 0 启动程序,这可能不是一个很好的假设。最好将其显式设置为零。