Write a MIPS program to \"guess a number\". Specification Create a variable call
ID: 3907429 • Letter: W
Question
Write a MIPS program to "guess a number". Specification Create a variable called numToGuess and set it equal to your number to guess. Pass a number entered from the keyboard to a procedure that prints one of the following messages: If the number from the keyboard equals the number to guess, print a "good guess" message. If the number from the keyboard is smaller than the number to guess, print a "too low" message. 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. Allow no more than 5 guesses. If the number is guessed correctly before the 5th guess, print a congratulatory message and exit the program. If the number is not guessed correctly in five guesses, print a "you had your five guesses" message and exit the program.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
.data
numToGuess: .word 8
lowMsg: .asciiz "too low"
highMsg: .asciiz "too high"
goodMsg: .asciiz "good guess"
winMsg: .asciiz " congratulations! you got it within 5 guesses"
lostMsg: .asciiz " you had your five guesses"
prompt: .asciiz " enter a number: "
.text
li $s0, 1
loop:
bgt $s0, 5, lost
li $v0, 4
la $a0, prompt
syscall
#read number from keyboard
li $v0, 5
syscall
move $t1, $v0
#setup parameter and call procedure
move $a0, $t1
jal checkGuess
beq $v0, 1, won #chck return value if 1, then guess is correct
add $s0, $s0, 1 #increment counter
j loop
won:
li $v0, 4
la $a0, winMsg
syscall
b exit
lost:
li $v0, 4
la $a0, lostMsg
syscall
exit:
#exit
li $v0, 10
syscall
#=================================
#procedure to check user guess and print message, return 1 if good guess and 0 otherwise
#receives the user guess as argument
checkGuess:
lw $t0, numToGuess
beq $t0, $a0, equal
bgt $a0, $t0, higher
li $v0, 4
la $a0, lowMsg
syscall
li $v0, 0
jr $ra
higher:
li $v0, 4
la $a0, highMsg
syscall
li $v0, 0
jr $ra
equal:
li $v0, 4
la $a0, goodMsg
syscall
li $v0, 1
jr $ra
output
----
enter a number: 4
too low
enter a number: 9
too high
enter a number: 7
too low
enter a number: 8
good guess
congratulations! you got it within 5 guesses
-- program is finished running --
enter a number: 1
too low
enter a number: 10
too high
enter a number: 3
too low
enter a number: 4
too low
enter a number: 5
too low
you had your five guesses
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.