Starting from:

$30

CMSC216 Exercise 4 -Solved

You will implement three functions in MIPS assembly. Their prototypes, in C syntax, are given below. 
• int max(int x, int y): Parameters x and y are passed in registers. Returns the max of x and y in a 
register. Eg, max(8, 9) returns 9. 
• int mash(int x, int y): Parameters x and y are passed in the stack. Returns the value of 10*x + y in 
a register. Eg, mash(8, 9) returns 89. Assume inputs do not cause overflflow. 
• int strlen(char* str): Parameter str is passed in a register. Assume that str points to a nul-terminated 
string. Returns the length of the string in a register. 
1We supply template fifiles max.s, mash.s and strlen.s, one for each function. We also supply drivers for these 
functions, in fifiles max_driver.s, mash_driver.s and strlen_driver.s. Consult the drivers to determine the 
precise locations of arguments and return values, ie, which registers and/or where in stack. 
The key reference documentation for the SPIM instruction set is HP_AppA.pdf, pages 48–72 (also available in 
the class page resources). 

Important 
1. To run spim and qtspim in a shell in Grace, you must fifirst run the command “module load spim” in 
that shell. To avoid typing this for each new shell, add a line with this command to your ~/.cshrc.mine 
fifile (and then run “source ~/.cshrc.mine” or logout and login). 
2. Your code must be written in the template fifiles max.s, mash.s and strlen.s. 
3. Labels representing the functions are already included in the template fifiles. You may need additional 
labels for branch targets. Do not have any label starting with “main”. (Each driver has a main function.) 
4. Do not remove the existing comments in the template fifiles. Add comments to your code as you see fifit. 
5. Do not modify the driver fifiles. 
6. To test your function, concatenate the driver fifile and your fifile and execute that resulting fifile. For exam
ple, do 
cat max_driver.s max.s > max_prog.s 
and execute fifile max_prog.s in qtspim or in command-line spim. The latter is illustrated below (see 
max_test1); the fifirst three lines are typed in, the last line is output. 
% spim -file max_prog.s 
8
9
9
%
8. Obey conventions for accessing quantities on the stack, otherwise you will lose points. In particular, 
access arguments and local variables on stack via offsets to the frame pointer. 
9. Obey function calling conventions, otherwise you will lose points. If your function uses a callee
saved register, it should be saved before using it and restored before returning, regardless of whether 
the supplied driver depends upon that register. For testing, we may use other drivers with the same 
passing conventions for arguments and return value. 
Although this assignment is not graded for style, use good style. Labels are flflush-left, instructions are in
dented, and comments at the end of code lines are further indented. Instructions should line up, and com
ments after instructions should line up. The assembler, unlike make, does not need a literal tab character for 
indenting; spaces are fifine. Emacs “assembler-mode” will try to help with this formatting; other editors may 
do the same. 

More products