2015-03-30 122 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