Write a program in MIPS assembly for use in the SPIM simulator. The program will
ID: 3880487 • Letter: W
Question
Write a program in MIPS assembly for use in the SPIM simulator. The program will implement a single player guessing game. The program will ask the player try to guess the
secret number. The program will output a message to indicate if the number guessed is too high or too low, and ask the player to enter a new number. If the number guessed by the
player matches the secret number, a congratulatory message will be displayed, followed by the number of tries it took the player to guess the secret number. The program should end at this
point.
Requirements:
1. Your code must be well-commented to receive full credit. This means any non-trivial line of code should have an accompanying comment
2. Add appropriate welcome/goodbye messages to the program (in addition to the prompt messages, etc. as part of the game).
3. The program should allow for a maximum of 10 guesses by the player.
a. If the user makes 10 incorrect guesses, print an appropriate “you lose” message and end the program.
4. You should hardcode the “secret” number as a single word in the .data section of the assembly code. Change the number for each test case below.
5. Test your program using the following inputs.
Secret number: 4
Guesses: 42 23 16 15 8 4
Secret number: 17
Guesses: 17
Secret number: 11
Guesses: 1 2 3 4 5 6 7 8 9 10
Explanation / Answer
.data
message: .asciiz "Guess a number "
higher: .asciiz "Higher! "
lower: .asciiz "Lower! "
correct: .asciiz "Correct! "
.text
##############################################################################
# seed the random number generator
##############################################################################
# get the time
li $v0, 30 # get time in milliseconds (as a 64-bit value)
syscall # Low order 32 bits of system time are now in $a0
move $t0, $a0 # save the lower 32-bits of time into $t0
# seed the random generator (just once)
li $a0, 1 # random generator id (will be used later)
move $a1, $t0 # seed from time
li $v0, 40 # seed random number generator syscall
syscall # Random Num takes 2 args, $a0 = id num $a1 = seed,
# which is now milliseconds of current time
##############################################################################
# seeding done
##############################################################################
##############################################################################
# get number from user
##############################################################################
# note $v0 now contains integer that was read
##############################################################################
# Generate a random number while it is unequal to guess
##############################################################################
# generate 10 random integers in the range 100 from the
# seeded generator (whose id is 1)
#li $t2, 6 # max number of iterations
#li $t3, 0 # current iteration number
LOOP:
la $t0 message # load the message's address
move $a0, $t0 # make the address an argument
li $v0, 4 # print string -> message
syscall
li $v0, 5 # get integer -> load it to $v0
syscall
move $t5, $v0 # move guessed int from $v0 to $t5
li $a0, 1 # as said, this id is the same as random generator id
li $a1, 10 # upper bound of the range
li $v0, 42 # System number for random int with range
syscall
addi $a0, $a0, 1 # $a0 now holds the random number, add one to it
move $t2, $a0 # move the generated number to $t2
# loop terminating condition
beq $t5, $t2, EXIT # branch to EXIT if iterations is 100
# This is saying, if t5 == t2 then exit
# Next iteration of loop
j LOOP
#Exit program
EXIT:
la $t0 correct # load the message's address
move $a0, $t0 # make the address an argument
li $v0, 4 # print string -> message
syscall
li $v0, 10 # exit syscall
syscall
Hope this helps you:)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.