Lab 10: Extra Credit (Optional)

This assignment is for extra credit only. You will not be penalized in any manner for not doing this assignment, or for doing only part of it.

This assignment has two parts. The first part is to do a MIPS implementation of the second exercise of Lab 9 (the bedtime story exercise). The second part is an exercise in passing functions as parameters (using function pointers).


Exercise 1: A child couldn't sleep... (MIPS assembly)

Take your C implementation of bedtimestory() from Lab 9, and implement it in MIPS assembly. All of the specifications remain the same. Write a main function as well, and use the same sample input and output files.

Your implementation of bedtimestory() must be recursive. A non-recursive implementation of the function will receive zero credit. Name the file with your MIPS code ex1.asm. Test it using your own inputs, and also the sample inputs provided for the C part of this exercise in Lab 9.


Exercise 2: Functions as Parameters (function pointers)

You will essentially re-implement bubble sort from Labs 4 and 5, but this time a more general version that can accept as an argument a function to compare two objects. Thus, whether the sorting is to be in ascending order or descending order can be determined simply by passing an appropriate compare function (less-than vs. greater-than).

Reading

  1. First read the entire Chapter 5 from the K&R book ("Pointers and Arrays") before even beginning this lab assignment. All of the material in that chapter is relevant. Section 5.11 in particular covers pointers to functions.
  2. Next, read about MIPS floating-point registers and instructions from the following sources: Textbook Chapter 3.5, pages 196-213; and Appendix A-73 to A-80 (MIPS floating-point instruction reference). Specifically, you need to know how to compare two floating-point numbers, and how to move data between floating-point registers and memory. In particular, focus on the following instructions: floating-point load/stores (l.s and s.s), copying from one FP register to FP another (mov.s), comparing two FP registers (c.lt.s), and conditional branching (bc1t and bc1f).

C Code

Understand, compile and run bubble.c and bubble_generic.c. The first program, bubble.c is basically identical to the bubble sort implementation from Lab 4, except that the code has been partitioned into procedures, and it involves floating-point numbers. The second program, bubble_generic.c replaces the simple comparison operation "A > B" with a generic function compare_fn whose actual definition is an argument/parameter passed into the sort routine. Thus, the same bubble sort function can be used to sort floats in increasing order or in decreasing order simply by handing it a different compare function. Carefully study this generic implementation, compile it, and run it. (This implementation comes closer to how you can specify a "compare object/method" to a generic sort routine in C++ or Java.)

MIPS assembly code

Take the C code for bubble_generic, and code the entire program in MIPS assembly, including the main function. Use single-precision floating-point operands and instructions in MIPS. Name the file with your MIPS code ex2.asm.

Hint: You should use the jalr instruction to implement function pointers, i.e., calling a function whose address is available in an argument register, instead of being a constant label.

Sample inputs: A sample input is provided within the C source code. Your MIPS program must generate the same output as the C program, except for differences in formatting of the floating-point numbers. In particular, use syscall 2 in MARS to print floats; it is okay if the output precision and format do not match the C output exactly.


Submission

Due Date: Mon Nov 16, 11:59pm for full extra credit; Fri Nov 20, 11:59pm for half extra credit; no credit thereafter.

If you have created multiple files for each exercise (e.g., following my instructions for earlier labs to use a separate file per assembly procedure), that is okay, but for submission you will need to join them because I did not specify the file names, so different students may be using different names. Concatenate all the files for Exercise 1 into the file ex1.asm, with the code for main at the top, and everything else below; make sure it compiles and runs fine in MARS. Similarly, concatenate all the files for Exercise 2 into the file ex2.asm, with its main at the top.

Submit your MIPS code (files ex1.asm and ex2.asm only), using the commands:

        % cd ~/comp411lab/lab10
        % /home/montek/comp411/bin/submitlab10

No self-checking is performed for this extra-credit assignment. You are responsible for checking your programs before submission. You can use the sample input/output files from Lab 9 for bedtimestory(); Exercise 2 has its input supplied internally.


6 November 2015, Montek Singh, montek@cs.unc.edu