[solved]-Assignment Fill Mips Code Function Asks User Enter Hexadecimal Number User Enters Valid He Q39030236
In this assignment, you will fill in the MIPScode for a function that asks the user to enter ahexadecimal number. Once the user enters a valid hexadecimal numberthat is within a certain range, the function should return thatnumber.
In Part 2 of this project (Assignment 6), you will build on thiscode to implement a simple game that allows the user to try toguess a number.
The GetGuess function
Your function will accept three arguments: the initial prompt,the minimum value for an acceptable guess, and the maximum valuefor an acceptable guess. The starter code already calls yourfunction with an appropriate value for each of these.
Here are the rough steps your function should follow:
-
Ask the user for a number. (The message you print in this stepis given to you as the first argument.)
- If the user enters nothing (if they just pressenter), just immediately return -1.
-
If the user enters something that is not a valid hexadecimalnumber, tell them and loop again. (The message you print in thiscase is given to you as a global.)
-
If the user enters a valid hexadecimal number that is outsidethe acceptable range, tell them and loop again. (The message youprint in this case is also given to you as a global.)
-
If the user enters a valid hexadecimal number that is within theacceptable range, return the number they entered.
C code
Below is the C code for the function you are required toimplement (this is also included in the starter code):
int GetGuess(char * question, int min, int max)
{
// Local variables
int theguess; //Store this on the stack
int bytes_read; // You canjust keep this one in a register
char buffer[16]; // This is 16contiguous bytes on the stack
// Loop
while (true)
{
// Print prompt, getstring (NOTE: You must pass the
// address of thebeginning of the character array
// buffer as thesecond argument!)
bytes_read =InputConsoleString(question, buffer, 16);
if (bytes_read == 0)return -1;
// Ok, we successfullygot a string. Now, give it
// to axtoi, which, ifsuccessful, will put the
// int equivalent intheguess.
//
// Here, you must passthe address of theguess as
// the first argument,and the address of the
// beginning of bufferas the second argument.
status =axtoi(&theguess, buffer);
if (status != 1)
{
PrintString(invalid);// invalid is a global
continue;
}
// Now we know we gota valid hexadecimal number, and the
// int equivalent isin theguess. Check it against min and
// max to make sureit’s in the right range.
if (theguess < min|| theguess > max)
{
PrintString(badrange);// badrange is a global
continue;
}
return theguess;
}
}
You should translate this C code into C code with gotos andlabels first. Then, translate the result into assembly.
Functions you have to call
Make sure to put a copy of utils.s in the same directory asguess.s. (Otherwise, the include at the end of the starter codewill not work.)
There are a number of functions (already implemented in utils.s)that you will have to call from your function. You can see in the Ccode how each one is used. These include:
int InputConsoleString (char *message, char *buf, int max)
You will use this function to retrieve a string from the user.The first argument should be the address of a string containing themessage you want to display in the dialog window. The secondargument should be the address of some amount of buffer space wherethe user’s input will be placed. The third argument should be thenumber of bytes in the buffer specified in the second argument.
int axtoi(int *num, char *string)
This function converts the string representation of ahexadecimal number into the int value for that number. The secondargument is the address of the string to be converted. The firstargument is the address of the int that should hold the result. Thefunction then puts the result at the address specified in the firstargument.
void PrintString(char *message)
You use this function to display a message in the consolewindow. The argument should be the address of the message you wishto display.
Important notes
Since you are implementing a function, you must take care tosave the registers that you (the callee) are responsible forsaving. You must also make necessary stack space for everything youwill need to store on the stack, as well as for arguments tofunctions you call. You must clean up that stack space when thefunction is finished. You should clean up exactly as much stackspace as you created! (The starter code contains a comment thatshould help you figure out how to allocate stack space.)
Each time you call a function, you must save any registers thatmight be overwritten (any registers that the caller is responsiblefor saving).
Finally, in order to pass the tests, make sure your mainfunction is contiguous and uninterrupted! This isimportant because some of the tests may replace your main functionwith an alternate function, and these tests will not work if youinterrupt your main function (with another function, forexample).
Sample Run
Below is the Mars console window after a completed sample run ofa finished program. The user entered “hello” first, which was not avalid hexadecimal number. The user then tried “a000”, which was outof range. Finally, the user tried “5”, which was successful.
Make a guess.helloNot a valid hexadecimal number.Make a guess.a000Guess not in range.Make a guess.55– program is finished running —
Expert Answer
Answer to In this assignment, you will fill in the MIPS code for a function that asks the user to enter a hexadecimal number. Once… . . .
OR