Write MIPS program to \"guess a number\". Specification 1. Create a variable cal
ID: 3760335 • Letter: W
Question
Write MIPS program to "guess a number".
Specification
1. Create a variable called numToGuess and set it equal to your number to guess.
2. Pass a number entered from the keyboard to a procedure that prints one of the following messages:
o If the number from the keyboard equals the number to guess, print a "good guess" message.
o If the number from the keyboard is smaller than the number to guess, print a "too low" message.
o If the number from the keyboard is greater than the number to guess, print a "too hi" message.
and returns 1 for a "good guess" or 0 for a "too low" or "too hi" guess.
3. Allow no more than 5 guesses.
o If the number is guessed correctly before the 5th guess, print a congratulatory message and exit the program.
o If the number is not guessed correctly in five guesses, print a "you had your five guesses" message and exit the program.
Explanation / Answer
.data
welcome: .asc "You Guess =secret number from 1-20 "
guess: .asc "Guess: "
higher: .asc "too hii! "
lower: .asc "too low! "
win: .asc "good guess!"
.text
Generate a random number
# $v0 still has the random number, lets put it in t1
add $t1,$zero,$v0
got number -- Secret Number is in $t1
start program:
# >From Secret Numbers? [newline]
la $a0, welcome
li $v0, 4 #print string sevice, 4
syscall
USERGUESS: #loop start!
# >Guess: [user input value from keyboard]
la $a0, guess
li $v0, 4 #print string sevice, 4
syscall
# >User inputed value [will be placed in $v0]
li $v0, 5 #read int service, 5
syscall
#move user val to $t2
move $t2, $v0
# [compare] $t1:Secret $t2:userGuess
#if equal, jump to winner, else compare and loop back to guess again
beq $t1, $t2, WINNER
#else:
# figure if higher or lower
blt $t1, $t2, LOWER
#default, HIGHER:
# Print "too hi!"
la $a0, higher
li $v0, 4 #print string sevice, 4
syscall
# loop back to get another guess
j USERGUESS
LOWER:
# Print "too low!"
la $a0, lower
li $v0, 4 #print string sevice, 4
syscall
# loop back to get another guess
j USERGUESS
WINNER:
# Print "good guess!"
la $a0, win
li $v0, 4 #print string sevice, 4
syscall
# Terminate the progam: EXIT:
li $v0, 10
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.