[40] Implement a guessing game in MIPS, where you pick a number between 1 and 10
ID: 3749828 • 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 x> 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 end of game message and # tries counter. A program that integrates all components and functions as described above will receive full credit.Explanation / Answer
.data
result_1: .asciiz " Yay! I got it in "
result_2: .asciiz "tries"
guess: .asciiz " The guessed number : "
prompt: .asciiz " Press R for right ; H for higher ; L for lower "
number_select: .asciiz " Choose a number between 1- 100 and computer will guess it "
.text
main:
li $s0,0
li $s1,0
li $s2,100
li $v0,4
la $a0,number_select
syscall
LOOP:
move $a1, $s2
li $v0, 42 #random
syscall
move $s3,$a0
add $t1,$s3,$s1
bgt $t1,$s2,SKIP
move $s3,$t1
SKIP:
addi $s0,$s0,1
li $v0,4
la $a0,guess
syscall
li $v0,1
move $a0,$s3
syscall
li $v0,4
la $a0,prompt
syscall
li $v0,12
syscall
move $t0,$v0
li $t1,'H'
beq $t0,$t1,HIGHER
li $t1,'L'
beq $t0,$t1,LOWER
li $t1,'R'
beq $t0,$t1,RIGHT
HIGHER:
move $s1,$s3
j LOOP
LOWER:
move $s2,$s3
j LOOP
RIGHT:
li $v0,4
la $a0,result_1
syscall
li $v0,1
move $a0,$s0
syscall
li $v0,4
la $a0,result_2
syscall
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.