data # Guess the Number game in MIPS assembly # Constants STARTING_RANGE: .word 1 # Starting range for random number ENDING_RANGE: .word 100 # Ending range for random number MESSAGE_PROMPT: .asciiz "Guess a number between 1 and 100:" MESSAGE_TOO_HIGH: .asciiz "Too high! Guess again: " MESSAGE_TOO_LOW: .asciiz "Too low! Guess again: " MESSAGE_CORRECT: .asciiz "Congratulations! You guessed the number!" NEWLINE: .asciiz "\n" random_number_prompt: .asciiz "The random number was: " guess_prompt: .asciiz "Your guess was: " .text # Initialize random number generator li $v0, 40 syscall # Generate random number lw $t0, ENDING_RANGE lw $t1, STARTING_RANGE sub $t2, $t0, $t1 addi $t2, $t2, 1 li $v0, 42 move $a0, $t1 move $a1, $t2 syscall move $t0, $v0 # Loop until number is guessed guess_loop: # Prompt user for guess la $a0, MESSAGE_PROMPT li $v0, 4 syscall # Get user input li $v0, 5 syscall move $t1, $v0 # Check if guess is correct beq $t1, $t0, correct_guess # Check if guess is too high bgt $t1, $t0, guess_too_high # Guess must be too low j guess_too_low # Guess is too high guess_too_high: la $a0, MESSAGE_TOO_HIGH li $v0, 4 syscall j guess_loop # Guess is too low guess_too_low: la $a0, MESSAGE_TOO_LOW li $v0, 4 syscall j guess_loop # Correct guess correct_guess: la $a0, MESSAGE_CORRECT li $v0, 4 syscall j exit_program # Exit program exit_program: li $v0, 10 syscall this code is doing this in mips assembly creat a program which is a simple game of "Guess the Number". In this game, the computer generates a random number between 1 and 100, and the player has to guess what that number is. After each guess, the computer tells the player whether their guess was too high, too low, or correct. The game ends when the player correctly guesses the number. But my code is not generating random numbers. it is keep giving me fixed output which is 42. every time i play the game getting same number. please fix it. here is my output Too low! Guess again: Guess a number between 1 and 100:34 Too low! Guess again: Guess a number between 1 and 100:54 Too high! Guess again: Guess a number between 1 and 100:43 Too high! Guess again: Guess a number between 1 and 100:42 Congratulations! You guessed the number! -- program is finished running --
data
# Guess the Number game in MIPS assembly
# Constants
STARTING_RANGE: .word 1 # Starting range for random number
ENDING_RANGE: .word 100 # Ending range for random number
MESSAGE_PROMPT: .asciiz "Guess a number between 1 and 100:"
MESSAGE_TOO_HIGH: .asciiz "Too high! Guess again: "
MESSAGE_TOO_LOW: .asciiz "Too low! Guess again: "
MESSAGE_CORRECT: .asciiz "Congratulations! You guessed the number!"
NEWLINE: .asciiz "\n"
random_number_prompt: .asciiz "The random number was: "
guess_prompt: .asciiz "Your guess was: "
.text
# Initialize random number generator
li $v0, 40
syscall
# Generate random number
lw $t0, ENDING_RANGE
lw $t1, STARTING_RANGE
sub $t2, $t0, $t1
addi $t2, $t2, 1
li $v0, 42
move $a0, $t1
move $a1, $t2
syscall
move $t0, $v0
# Loop until number is guessed
guess_loop:
# Prompt user for guess
la $a0, MESSAGE_PROMPT
li $v0, 4
syscall
# Get user input
li $v0, 5
syscall
move $t1, $v0
# Check if guess is correct
beq $t1, $t0, correct_guess
# Check if guess is too high
bgt $t1, $t0, guess_too_high
# Guess must be too low
j guess_too_low
# Guess is too high
guess_too_high:
la $a0, MESSAGE_TOO_HIGH
li $v0, 4
syscall
j guess_loop
# Guess is too low
guess_too_low:
la $a0, MESSAGE_TOO_LOW
li $v0, 4
syscall
j guess_loop
# Correct guess
correct_guess:
la $a0, MESSAGE_CORRECT
li $v0, 4
syscall
j exit_program
# Exit program
exit_program:
li $v0, 10
syscall
this code is doing this
in mips assembly creat a program which is a simple game of "Guess the Number". In this game, the computer generates a random number between 1 and 100, and the player has to guess what that number is. After each guess, the computer tells the player whether their guess was too high, too low, or correct. The game ends when the player correctly guesses the number.
But my code is not generating random numbers. it is keep giving me fixed output which is 42. every time i play the game getting same number. please fix it.
here is my output
Too low! Guess again: Guess a number between 1 and 100:34
Too low! Guess again: Guess a number between 1 and 100:54
Too high! Guess again: Guess a number between 1 and 100:43
Too high! Guess again: Guess a number between 1 and 100:42
Congratulations! You guessed the number!
-- program is finished running --
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images