2015-03-30 121 views
1

我正在尝试编写一个MIPS程序,提示用户猜测随机数生成器生成的随机数。他们有10次尝试正确猜测或失败。一旦游戏结束,它会告诉用户他们是赢了还是输了,并打印出他们猜测的数字。我在存储用户的输入并在完成游戏后显示回来时遇到问题。MIPS用户输入数组存储和打印问题

回答

1

你可以通过增加每猜测指针的偏移使用动态内存来存储猜测:

li $v0, 9  #allocate memory for new record 
li $a0, 60  #enough memory for 15 guesses 
syscall 
move $s0, $v0 #hang onto the initial address of our array (memory) 

li $t6, 0  #Current Offset of our array = 0 

然后,在你的循环:

li $v0, 5   #enter integer 
syscall 

add $s1, $s0, $t6 #add our offset to our initial memory location 

sw $v0, 0($s1)  #Store our guess in the offset location 

add $t6, $t6, 4  #add 4 to our offset 

若要显示, ,你可以这样做:

showlist:   #Show us what we guessed 

li $t1, 0   #Set our initial offset to 0 

listloop: 
add $s1, $s0, $t1 #Add our offset to our initial memory location 

li $v0, 1 
lw $a0, 0($s1) #Print the number at our memory address + offset 
syscall 

add $t1, $t1, 4 #Add 4 to our offset 
bge $t1, $t6, done #If our offset is >= our final guess offset, we're done 

li $v0, 4   #Printa coma and space 
la $a0, space 
syscall 

j listloop 

请参见下面的完整的解决方案:

#t0 = Number to guess 
.data 
nl: .asciiz "\n" 
space: .asciiz ", " 
prompt1: "\nPlease enter a number: " 
toohigh: "\nYou guessed too high" 
toolow: "\nYou guessed too low" 
winner: "\nThat's correct! You Win!" 
guesses: "\nHere are your guesses: " 
youlose: "\nYou Lose!" 
youlose2: "\nThe Correct Number Was: " 
.text 

################Generate our random number################### 
li $v0, 42  #random number generator 
li $a1, 21  #upper bound, gen number from 0-20 
syscall   # runs whatever is in $v0 (command 42-RNG) 
move $t0, $a0 #move stuff from $a0 to $t0 
############################################################ 

li $v0, 9  #allocate memory for new record 
li $a0, 60  #enough memory for 15 guesses 
syscall 
move $s0, $v0 #hang onto the initial address of our array (memory) 

li $t6, 0  #Current Offset of our array = 0 

################## 
# Our Guess Loop # 
################## 
loop: 
li $v0, 4  #prompt for Number: 
la $a0, prompt1 
syscall 

li $v0, 5   #enter integer 
syscall 

add $s1, $s0, $t6 #add our offset to our initial memory location 

sw $v0, 0($s1)  #Store our guess in the offset location 

add $t6, $t6, 4  #add 4 to our offset 
beq $v0, $t0, win #Did we win? 
bge $t6, 60, lose #Did we lose? (60 = 4 * 15) We can use that to tell how many guesses we tried 
blt $v0, $t0, less #Is our number less than the right number? 

li $v0, 4   #Must have guessed too high - let us know 
la $a0, toohigh 
syscall 
j loop 

less: 
li $v0, 4   #Must have guessed too low - let us know 
la $a0, toolow 
syscall 
j loop 
################### 

win: 
li $v0, 4   #We won! 
la $a0, winner 
syscall 

showlist:   #Show us what we guessed 
li $v0, 4 
la $a0, guesses 
syscall 

li $t1, 0   #Set our initial offset to 0 

listloop: 
add $s1, $s0, $t1 #Add our offset to our initial memory location 

li $v0, 1 
lw $a0, 0($s1) #Print the number at our memory address + offset 
syscall 

add $t1, $t1, 4 #Add 4 to our offset 
bge $t1, $t6, done #If our offset is >= our final guess offset, we're done 

li $v0, 4   #Printa coma and space 
la $a0, space 
syscall 

j listloop 

lose:    #We lost 
li $v0, 4 
la $a0, youlose #Let us know 
syscall 

li $v0, 4 
la $a0, youlose2 #Display "The correct number was: " 
syscall 

move $a0, $t0 #Move the right number into $a0 to display it 
li $v0, 1 
syscall 

j showlist  #Then show us our guesses 

done: 
li $v0, 10   #Terminate Program 
syscall 

WIN:

Win

失去:

Lose