1. Consider function getint() documented below in pseudocode. getint() has one p
ID: 3678534 • Letter: 1
Question
1. Consider function getint() documented below in pseudocode. getint() has one parameter, prompt, which is the address of a string to be displayed on the output window. It displays prompt using the SysPrintStr() system call. Then it calls SysReadInt() to read an integer from the keyboard that the user enters storing the integer in local variable n. It then returns n. function getint(string prompt) returns int int n SysPrintStr(prompt) n = SysReadInt() return n end function Here is the MIPS assembly language function header comments for getint(), which you will be implementing.
#-------------------------------------------------------------------------------------------------------------
# FUNCTION: int getint(string prompt)
# DESCRIPTION # Displays the prompt string (the variable 'prompt' contains the address of the string which is defined in the
# .data section) using SysPrintStr(). Then calls SysReadInt() to read the integer the user enters. Returns the
# integer in $v0. # PARAMETERS # $a0 prompt (addr of string to be displayed)
# PSEUDOCODE
# See above
# RETURNS
# An int in $v0 which is the integer the user typed
# STACK FRAME
# param prompt 4($sp) # local var n 0($sp)
#-------------------------------------------------------------------------------------------------------------
The input parameter, prompt, contains the address in memory of the string to be displayed; this string is defined in the .data section. It returns the integer that the user types in $v0. Note that getint() is a leaf procedure since it does not call other functions. This means we do not need to save $ra in getint()'s stack frame. Furthermore, since this function is small and simple enough, we could get by without allocating a stack frame at all. However, for educational purposes (i.e., so you will learn how to allocate a stack frame containing parameter and local variables and understand how to access those stack frame variables) we will allocate a stack frame for getint(). The number of words to be allocated in the stack frame will be two: parameter prompt will be allocated so it is at offset 4 relative to $sp and local variable n will be allocated so it is at offset 0 relative to $sp. Since we are allocating two words (which is eight bytes), the first instruction in getint() must be addi $sp, $sp, -8 to allocate the stack frame. The eight bytes that are allocated must be deallocated by adding 8 to $sp right before the jr $ra instruction; I refer to this as "destroying or deallocating the stack frame." For this exercise, write the MIPS32 assembly language code that implements getint(). My unoptimized implementation required 11 instructions: four addi, one jr, two lw, two syscall, and two sw. Note, you are not being asked to write a complete program; just implement getint(). However, for testing purposes, you should write a driver main() function that calls getint() to verify it works correctly.
Explanation / Answer
function getint(string prompt) returns int
int n
SysPrintStr(prompt)
n = SysReadInt()
return n
end function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.