Reputation: 13
I am trying to write a program in MIPS that prompts the user to guess a random number that is generated from a random number generator. They have 10 tries to guess correctly or they lose. Once the game is over it will tell the user whether they have won or lost and will print out an array of the numbers they guessed. I am having trouble with storing the user's input and displaying back once they finish the game.
Upvotes: 1
Views: 1584
Reputation: 8144
You could use dynamic memory to store the guesses by incrementing the offset of your pointer with each guess:
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
Then, in your loop:
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
To display them, you can do it like this:
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
See the complete solution below:
#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:
Lose:
Upvotes: 1