Translate this into MIPS Below is the C code for the function you are required t
ID: 3720628 • Letter: T
Question
Translate this into MIPSBelow 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"
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"
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 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" ####################### # 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"
Explanation / Answer
.include "util.s"
.data
#
msg_welcome: .asciiz "I have a secret number from 1-10, can you guess it? "
msg_guess: .asciiz "Guess: "
#
hint_higher: .asciiz "Higher! "
hint_lower: .asciiz "Lower! "
#
msg_win: .asciiz "Correct! You win!"
.text
#############################################################################
# Generate a random number
#############################################################################
jal random
# $v0 still has the random number, lets put it in t1
add $t1,$zero,$v0
##############################################################################
# got number -- Secret Number is in $t1
##############################################################################
##############################################################################
# start program:
##############################################################################
# >I have a secret number ... can you guess it? [newline]
la $a0, msg_welcome
li $v0, 4 #print string sevice, 4
syscall
USERGUESS: #start of loop!
# >Guess: [wait for user input]
la $a0, msg_guess
li $v0, 4 #print string sevice, 4
syscall
# >User input [will be placed in $v0]
li $v0, 5 #read int service, 5
syscall
# [move users input to a better position -- $t2!]
move $t2, $v0
###############################################################################################
################### USERS GUESS IS STORED IN: ###############
########################################## $ t2 #######################
############# (and the computers secret number is in $t1 #######
###############################################################################################
# [compare] $t1:Secret $s7:userGuess
#if equal, jump to winner, else compare and loop back to guess again
beq $t1, $t2, WINNER
#else:
# figure if higher or lower
blt $t1, $t2, LOWER
#############################
##### default CASE #####
#default, HIGHER:
# Print "HIGHER!"
la $a0, hint_higher
li $v0, 4 #print string sevice, 4
syscall
# loop back to get another guess
j USERGUESS
#### END default #####
#############################
#############################
##### LOWER CASE #####
LOWER:
# Print "LOWER!"
la $a0, hint_lower
li $v0, 4 #print string sevice, 4
syscall
# loop back to get another guess
j USERGUESS
#### END LOWER #####
#############################
#############################
##### WINNER CASE #####
WINNER:
# Print "YOU WIN!"
la $a0, msg_win
li $v0, 4 #print string sevice, 4
syscall
# Terminate the progam: EXIT:
li $v0, 10
syscall
#### END WINNER #####
#############################
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.