In this assignment you will write a MIPS program that plays a simple game. The g
ID: 3814553 • Letter: I
Question
In this assignment you will write a MIPS program that plays a simple game. The game chooses a [random] secret number between two non-negative variables min and max and requests guesses from the user, telling them whether their guess is too high or too low until the number is found. It allows the user to play multiple games until she indicates she is finished. The name of the program is guess, and its assembler file is guess. s. There is a caveat of guess: It does its guessing in hexadecimal! To standardize the program and make it more manageable, guess has already been designed and partially written for you. You must complete it. Here is an outline of the program function: At program start, the question string is created, using a function you will translate from C (see below). This message must tell the user to enter a guess in hexadecimal what the min and max values are (the secret number will be in [min, max] (i.e., inclusive)). The min and max values are constant for a program invocation, but they are simply initialized .data integers-hence they must be converted to text for inclusion in the question string. it then initializes the random number generator using a function you will write (see below) For each game it then gets a secret number from a function you will write (see below) repeatedly calls a function which is provided for you to get a legal guess (i.e., one which is a valid hex number and is in [min, max]) compares the resulting guess to the secret number, giving the user feedback on whether the guess is too high or too low until the secret number is 'guessed' at the end of the game it asks if the user wants to play again. at any time the user can quit by typing q When the user doesn't want to play again, the program completes by returning from main(). The program requires the following functions: The main program which implements the algorithm above. A function to take two strings, and allocate and return a new string which is a copy of the first string concatenated with the second string. This function, called straup2, is written for you in C. You simply translate it. (It is very simple-it simply calls functions in util.s. This is much simpler than the equivalent function strcat2 that you will examine in the online notes.) You will call this function multiple times to create your question string from text and the min and max values, which you must convert to text using functions in util.s: char * strdup2 (char * firstString, char * secondString) A function to get a legal guess named GetGuess. This function is provided for you: int GetGuess (char*question, int min, int max) GetGuess asks the question and does not return until the user inputs a hexadecimal number inExplanation / Answer
.text
li $a1, 100 #Here you set $a1 to the max bound.
li $v0, 42 #generates the random number.
syscall
#add $a0, $a0, 100 #Here you add the lowest bound
li $v0, 1 #1 print integer
syscall
or
Rand:
move $2, $4
li $8, 33614
multu $2, $8
mflo $9
srl $9, $9, 1
mfhi $10
addu $2, $9, $10
bltz $2, overflow
j $31
overflow:
sll $2, $2, 1
srl $2, $2, 1
addiu $2, 1
j $31
here how it works i will explain
Load the random number into a register, lets say $t1.
Now load your max range, let's use 13 into $t2 == MAX
Now divide $t1/$t2
The remainder will always be between 0 and MAX - 1, so if you add one to the remainder you will always have a number between 1 and MAX. Does that make sense?
The basical prinicpal is modular arithmetic:
N = Rand
MAX = 13
Number = (N mod Max) + 1, always produces a random number between 1 and MAX
................................................................................................
if you still find it difficult try this
.RandRange
call Rand ; get random number (this call the function that I wrote)
mov ebx, [esp+4] ; read range limit
xor edx,edx ; clear edx register
div ebx ; do integer division; eax / ebx
mov eax,edx ; read remainder of division
inc eax ; add one
ret 4 ; restore stack and return
Call RandRange using code similar to the following:
push N%
call RandRange