MIPS将键盘输入(int)存储到数组

时间:2012-10-05 04:25:50

标签: mips

我正在尝试从键盘上取4个数字并将其存储到数组中。我已经提出了以下代码,但是当我尝试打印数组中的第一个数字时,它会打印0.为什么不打印数组中的第一个值?

main: 
li $v0, 5 # read integer 
syscall
add $s0, $v0, $zero #store input1 to s0

li $v0, 5 # read integer 
syscall
add $s1, $v0, $zero #store input2 to s0

li $v0, 5 # read integer 
syscall
add $s2, $v0, $zero #store input3 to s0

li $v0, 5 # read integer 
syscall
add $s3, $v0, $zero #store input4 to s0

.data 
list:   $s0, $s1, $s2, $s3 #set array from keyboard values
.text

#get the array into a register
la $t3, list # put address of list into $t3
lw $t4, 0($t3) # get the value from the array cell

li $v0, 1 # print integer 
move $a0, $t4 # what to print is stored at $s1 
syscall # make system call


exit:    
    li $v0, 10 # exit system call 
syscall

理念取自: http://www.cs.pitt.edu/~xujie/cs447/AccessingArray.htm 但他们使用硬编码的int而不是从键盘保存到寄存器的值。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

此:

.data 
list:   $s0, $s1, $s2, $s3 #set array from keyboard values
.text

这种方式不起作用。您需要sw将寄存器的值存储到内存中。

因此,在程序开始时,保留一些空间来存储值:

.data 
list:  .space 16
.text

然后,在您阅读了这些值后,请使用sw

存储它们
sw $s0, list
sw $s1, list + 4
sw $s2, list + 8
sw $s3, list + 12
相关问题