[40] Implement a guessing game in MIPS, where you pick a number between 1 and 10
ID: 3748790 • Letter: #
Question
[40] Implement a guessing game in MIPS, where you pick a number between 1 and 100, and the computer tries to guess your number. After each guess, you should specify whether your secret number is higher or lower than the computer's guess. The computer's guess should be a random number between the known maximum and minimum values. For example, if your secret number is 55, the computer would begin by guessing a random value from 1 to 100. Depending on the result, its guess may be higher or lower than 55, to which you would input "h" or "". Let's say the computer guesses 42. You would input "h". The new lower bound is 42, and upper bound is still 100. The computer's next guess would be between 43 and 100. This continues until the computer guesses your number-for this, you will enter "X" to indicate the game is over. The program should then print a message that includes the number of guesses it took (e.g. "Yay! I got it in tries!"). You will need to use syscalls for printing a string to the console, reading a character from the console, and generating a random number. These can be found in the syscall reference posted on Canvas. There is also a MIPS instruction reference in the book that will be useful for checking available instructions and instruction formats 2. Partial credit will be given as follows: [5] printing to the console, [5] reading from the console into memory, [5] generating random numbers, [15] core game logic (adjusting random number bounds based on user input), [5] end of game message and # tries counter. A program that integrates all components and functions as described above will receive full credit.Explanation / Answer
ScreenShot
------------------------------------------------------------
Program
#Data declaration
.data
high: .asciiz "h "
low: .asciiz "l "
result: .asciiz "yay! I got it in "
remain: .asciiz " tries! "
prompt: .asciiz "Enter a number between 1-100(inclusive):"
errorMessage: .asciiz "Input should be 1-100 "
exitPrompt: .asciiz "Do you want to continue(x for exit): "
bye: .asciiz " Good Bye!!"
#Main program
.text
#Loop until user enter 'x'
input:
#User input
la $a0,prompt
li $v0,4
syscall
#read data
li $v0,5
syscall
#If user not enter between 1-100 error
blt $v0,1,error
bgt $v0,100,error
#Otherwise store user input in s0
move $s0,$v0
#Counter
li $s1,1
#for lowest bount
li $t0,1
random:
#random generation
li $v0, 42
#Max bound
li $a1, 100
syscall
#lowest bound
add $a0, $a0,$t0
#check guess is correct or not
beq $a0,$s0,printResult
#Increment count
addi $s1,$s1,1
#Check the guess lesser or greater
blt $a0,$s0,printHigh
bgt $a0,$s0,printLow
#Is result low print low
printLow:
move $a1,$a0
la $a0,low
li $v0,4
syscall
j random
#Is result high print high
printHigh:
move $t0,$a0
la $a0,high
li $v0,4
syscall
j random
#print result (count of tries)
printResult:
la $a0,result
li $v0,4
syscall
move $a0,$s1
li $v0,1
syscall
la $a0,remain
li $v0,4
syscall
j exitLoop
#Prompt user to continue or exit
exitLoop:
la $a0,exitPrompt
li $v0,4
syscall
li $v0,12
syscall
beq $v0,120,end
j input
#'x' input then display bye and exit
end:
la $a0,bye
li $v0,4
syscall
#end of the program
li $v0,10
syscall
#Error message
error:
la $a0,errorMessage
li $v0,4
syscall
j input
---------------------------------------------
Output
Enter a number between 1-100(inclusive):24
yay! I got it in 1 tries!
Do you want to continue(x for exit): x
Good Bye!!
-- 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.