我正在尝试用MIPS编写一个程序,提示用户猜测从随机数生成器生成的随机数。他们有10次尝试正确猜测或他们输了。一旦游戏结束,它将告诉用户他们是赢还是输,并将打印出他们猜到的数字数组。我无法存储用户的输入并在完成游戏后显示回来。
答案 0 :(得分: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:强>
<强>输强>