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.


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. You can find these in the MIPS language reference in the textbook, or any number of websites. Specifically, you need to know how to compare two floating-point numbers, and how to move data between floating-point registers.

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 your C code for bubble_generic, and code the entire program in MIPS assembly, including the main function. You should use single-precision floating-point operands and instructions in MIPS. Name the file with your MIPS code ex2.asm.


Submission

Submit your MIPS code by November 9th, 1pm (files ex1.asm and ex2.asm only), using the commands:

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


2 November 2012, Montek Singh, montek@cs.unc.edu