读取和打印用户输入的MIPS阵列

时间:2016-09-30 15:42:41

标签: arrays mips

所以我正在使用MIPS尝试读取用户输入的几个字符串然后打印它们,但我没有得到我期望的行为。

我想接受4个字节的用户输入(基本上是4个字符)。在我的循环中,我使用字母'D'作为退出的信号。问题是,无论我输入什么,当我尝试稍后打印第一个输入(甚至第二个或第三个输入)时,我打印出来的都是用于退出的字母'D'(应该是是否是数组的最后一个值?)。

.data
 mem: .space 256 #256 bytes of space for input
 inst .space 5
.text 

la $s1, mem #s1 used to take input
la $s2, 0($s1) #Pointer to base address of memory

jal readLoop #Read input loop 

lw $a0, 0($s2) #Attempt to read very first saved input
li $v0, 4
syscall

li $v0, 10 #End program 
syscall

readLoop:

li $v0, 8 #read string
la $a0, inst #location of input memory
addi $a1, $zero, 5 #length of buffer
syscall

lb $t2,($a0) #used to exit loop

sw $a0, 0($s1) #store input into memory
addi $s1, $s1, 4 #increment memory by 4 bytes

li $t1, 'D'
bne $t2, $t1, readLoop #exit loop on input of a 'D'

jr $ra

我已经检查了输入,甚至在保存后在数组中。看来我的打印是问题,但我很容易出错。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

修复很容易。我没有将用户输入存储到单独的内存位置,然后将该位置放在我的数组中,而是将用户输入直接存储到我的数组中。请参阅下面的代码更改

.data
 mem: .space 256 #256 bytes of space for input
.text 

la $s1, mem #s1 used to take input

jal readLoop #Read input loop 

la $a0, mem #Attempt to read very first saved input
li $v0, 4
syscall

li $v0, 10 #End program 
syscall

readLoop:

li $v0, 8 #read string
la $a0, mem #set user input as memory location
addi $a1, $zero, 5 #length of buffer
syscall

lb $t2,($a0) #used to exit loop

addi $s1, $s1, 4 #increment memory by 4 bytes

li $t1, 'D'
bne $t2, $t1, readLoop #exit loop on input of a 'D'

jr $ra
相关问题