Question: Translate this into MIPS Below is the C code for the function you are required to implement (this is also included in the starter code):

Translate this into MIPS
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
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 #
#######################
subi $sp, $sp, 40
sw $ra, 36($sp)# return
sw $s0, 32($sp)# theguess
li $s1, 0# status
li $s2, 0# byte_read
jr $ra
.include "./util.s"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!