Question
In this assignment, you will fill in the MIPS code for a function that asks the user to enter a hexadecimal number. Once the user
In this assignment, you will fill in the MIPS code for a function that asks the user to enter a hexadecimal number. Once the user enters a valid hexadecimal number that is within a certain range, the function should return that number.
In Part 2 of this project (Assignment 7), you will build on this code to implement a simple game that allows the user to try to guess a number.
The GetGuess function
Your function will accept three arguments: the initial prompt, the minimum value for an acceptable guess, and the maximum value for an acceptable guess. The starter code already calls your function 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 step is given to you as the first argument.)
If the user enters nothing (if they just press enter), just immediately return -1.
If the user enters something that is not a valid hexadecimal number, tell them and loop again. (The message you print in this case is given to you as a global.)
If the user enters a valid hexadecimal number that is outside the acceptable range, tell them and loop again. (The message you print in this case is also given to you as a global.)
If the user enters a valid hexadecimal number that is within the acceptable range, return the number they entered.
C code
Below is the C code for the function you are required to implement (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 can just keep this one in a register
char buffer[16]; // This is 16 contiguous bytes on the stack
// Loop
while (true)
{
// Print prompt, get string (NOTE: You must pass the
// address of the beginning of the character array
// buffer as the second argument!)
bytes_read = InputConsoleString(question, buffer, 16);
if (bytes_read == 0) return -1;
// Ok, we successfully got a string. Now, give it
// to axtoi, which, if successful, will put the
// int equivalent in theguess.
//
// Here, you must pass the address of theguess as
// the first argument, and the address of the
// beginning of buffer as the second argument.
status = axtoi(&theguess, buffer);
if (status != 1)
{
PrintString(invalid); // invalid is a global
continue;
}
// Now we know we got a valid hexadecimal number, and the
// int equivalent is in theguess. Check it against min and
// max to make sure it'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 and labels 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 as guess.s. (Otherwise, the include at the end of the starter code will 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 C code 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 the message you want to display in the dialog window. The second argument should be the address of some amount of buffer space where the user's input will be placed. The third argument should be the number of bytes in the buffer specified in the second argument.
int axtoi(int *num, char *string)
This function converts the string representation of a hexadecimal number into the int value for that number. The second argument is the address of the string to be converted. The first argument is the address of the int that should hold the result. The function then puts the result at the address specified in the first argument.
void PrintString(char *message)
You use this function to display a message in the console window. The argument should be the address of the message you wish to display.
Some things to keep in mind
Since you are implementing a function, you must take care to save the registers that you (the callee) are responsible for saving. You must also make necessary stack space for everything you will need to store on the stack, as well as for arguments to functions you call. You must clean up that stack space when the function is finished. You should clean up exactly as much stack space as you created! (The starter code contains a comment that should help you figure out how to allocate stack space.)
Each time you call a function, you must save any registers that might be overwritten (any registers that the caller is responsible for saving).
Sample Run
Below is the Mars console window after a completed sample run of a finished program. The user entered "hello" first, which was not a valid hexadecimal number. The user then tried "a000", which was out of range. Finally, the user tried "5", which was successful.
Make a guess. hello Not a valid hexadecimal number. Make a guess. a000 Guess not in range. Make a guess. 5 5 -- program is finished running --
Below is the starter file:
#######################
# guess.s
# -------
# This program asks the user to enter a guess. It
# reprompts if the user's entry is either an invalid
# hexadecimal number or a valid hexadecimal number
# that is outside the range specified in the program
# by min and max.
#
.data
min: .word 1
max: .word 10
msgguess: .asciiz "Make a guess. "
msgnewline: .asciiz " "
.text
.globl main
main:
# Make space for arguments and saved return address
subi $sp, $sp, 20
sw $ra,16($sp)
# Get the guess
la $a0, msgguess
lw $a1, min
lw $a2, max
jal GetGuess
# Print the guess
move $a0, $v0
jal PrintInteger
# Print a newline character
la $a0, msgnewline
jal PrintString
# Return
lw $ra, 16($sp)
addi $sp, $sp, 20
jr $ra
################################
# GetGuess
################################
.data
invalid: .asciiz "Not a valid hexadecimal number. "
badrange: .asciiz "Guess not in range. "
.text
.globl GetGuess
#
# C code:
#
# int GetGuess(char * question, int min, int max)
# {
# // Local variables
# int theguess; // Store this on the stack
# int bytes_read; // You can just keep this one in a register
# int status; // This can also be kept in a register
# char buffer[16]; // This is 16 contiguous bytes on the stack
#
# // Loop
# while (true)
# {
# // Print prompt, get string (NOTE: You must pass the
# // address of the beginning of the character array
# // buffer as the second argument!)
# bytes_read = InputConsoleString(question, buffer, 16);
# if (bytes_read == 0) return -1;
#
# // Ok, we successfully got a string. Now, give it
# // to axtoi, which, if successful, will put the
# // int equivalent in theguess.
# //
# // Here, you must pass the address of theguess as
# // the first argument, and the address of the
# // beginning of buffer as the second argument.
# status = axtoi(&theguess, buffer);
# if (status != 1)
# {
# PrintString(invalid); // invalid is a global
# continue;
# }
#
# // Now we know we got a valid hexadecimal number, and the
# // int equivalent is in theguess. Check it against min and
# // max to make sure it's in the right range.
# if (theguess < min || theguess > max)
# {
# PrintString(badrange); // badrange is a global
# continue;
# }
#
# return theguess;
# }
# }
#
#
GetGuess:
# stack frame must contain $ra (4 bytes)
# plus room for theguess (int) (4 bytes)
# plus room for a 16-byte string
# plus room for arguments (16)
# total: 40 bytes
# 16 byte buffer is at 16($sp)
# theguess is at 32($sp)
#
#######################
# YOUR CODE HERE #
#######################
jr $ra
.include "./util.s"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started