COMP 411: Lab 10: More Procedure Calls and Recursion

Due Wednesday, April 4, 2018, 11:59 PM

Exercise 1

This assignment introduces you to writing MIPS assembly code using multiple files, one procedure per file. By specifying such a decomposition, the idea is to give you more practice with writing modular code. All of the MIPS calling conventions discussed in class must be adhered to. The exercise involves assembly coding of 5 distinct procedures. However, assembly code of two of those procedures is given to you. Your task is to write the other three assembly procedures in a robust manner, following calling conventions and with appropriate stack management, so your code can work properly with the code provided.

A full C program is provided to you for this exercise (ex1.c). Your task is to write the equivalent MIPS assembly program. Exactly like the C program, your MIPS program should consist of the following procedures, and they should be in separate files as indicated:

Note: Please use the names of the procedures and files exactly as specified above (including upper/lower case). The files ex1_main.asm and ex1_printit.asm are provided. Please use them without making any modifications.

Here is some explanation of the code. Procedure main repeatedly reads a pair of integers (a, b) from the terminal, and then calls do_task(a, b). If a value of 0 was read for a by main, then it terminates the program without reading b. The procedure do_task(a, b) computes two mathematical functions and prints their results. First, it calls NchooseK(n, k) but with n = a+b and k = a, i.e., NchooseK(a+b, a), and prints its result. Next, it calls Fibonacci(b), and prints its result. To print both these results, procedure print_it() is used.

NchooseK: You need to use a recursive implementation of this function, exactly as in Exercise 3 of Lab 8. If your code in Lab 8 was recursive and worked correctly, you can simply use it here. If not, you will have to fix it first. No points will be given if your implementation is not recursive. A template is provided (ex1_nchoosek.asm).

Fibonacci: Write a recursive implementation of a function that computes the N-th Fibonacci number, i.e., Fibonacci(N), using the following definition:

A non-recursive implementation of the function will receive zero credit. A template is provided (ex1_fib.asm)

do_task: Write the assembly code for the do_task procedure, closely following the C code. A template is provided (ex1_dotask.asm)

main: Use the code provided, without making any changes. In addition to performing the tasks described, our implementation of main "stress tests" your code by reading in initial values of registers $s0-$s7 before calling your functions. At the end, it prints the final values of registers $s0-$s7. These final values should be the same as the initial values because none of your procedures should tamper with the values of these saved registers, i.e., if your procedures modify any of these registers, they will save and restore them by appropriately using the stack.

print_it: Use the code provided, and as indicated in the C source, without making any changes. This procedure also "stress tests" your code by destroying the values of temporary registers: $a0-$a3, $v0-$v1, and $t0-$t9. Since these are scratch registers, our procedure is free to modify them without saving and restoring them, so your code cannot rely on these registers being preserved by our code.

Splitting up assembly code into separate files

Your assembly code must be split into separate files as follows:

Once you are confident your program is working properly in MARS, copy your three source files (ex1_dotask.asm, ex1_nchoosek.asm and ex1_fib.asm) to classroom.cs.unc.edu under the appropriate folder in your home directory (e.g., comp411lab/lab10). Use the following commands to set up your environment, and copy ex1_main.asm and ex1_printit.asm as well as the sample input/output files into your folder:

 % /home/porter/comp411/bin/comp411start
 % cp /home/porter/comp411/samples/lab10/ex1* .

Then run your program:

 % java -jar /home/porter/comp411/bin/Mars4_5.jar nc mc CompactDataAtZero ex1_main.asm ex1_dotask.asm ex1_nchoosek.asm ex1_fib.asm ex1_printit.asm < ex1in1 > ex1result1

That is, all the assembly files are listed in the command, with the one with the main procedure first.

Test Inputs, Due Date and Submission Procedure

Sample inputs and corresponding outputs are provided under /home/porter/comp411/samples/lab10. Run your program as instructed above for each of the sample inputs, and make sure the program's output is identical to that in the corresponding sample output.

Your assignment must be submitted electronically by 11:59pm on Wednesday, April 4.

After copying your work to classroom.cs.unc.edu under the appropriate folder in your home directory (e.g., comp411lab/lab10), you can run the selfchecking and submit scripts:

 % /home/porter/comp411/bin/comp411start
 % cd ~/comp411lab/lab10
 % selfchecklab10
 % submitlab10

In case of any problems, please contact the instructor or the TAs.


28 March 2018, Montek Singh, montek@cs.unc.edu

Last updated: 2018-04-03 19:03:08 -0400 [validate xhtml]