Homework 4: Some MIPS with SPIM!

Homework is important practice for you, but it’s not graded so you don’t have to submit it. This particular homework is designed to help you practice programming the MIPS architecture in assembly using the excellent SPIM simulator.

Overview

Note that we’ll use “SPIM Version 8.0 of January 8, 2010” which is the default in Lubuntu 16.04 LTS! For some problems we give you a bunch of starter code on Piazza already, so for those all you have to do is fill in one or two subroutines in the way described below (and in the code itself, be sure to read it for all the gory details).

Note that for all problems the MIPS conventions regarding register usage (arguments, results, caller-saved vs. callee-saved, etc.) are in effect! The full MIPS calling conventions (regarding the format of a stack frame for example) are not in effect however.

Problem 1: Insertion Sort

Sorting is always a popular programming problem, and now it’s time to hack a sorting algorithm in MIPS assembly. Sadly this is going to be a little less exciting than it was for the 6502 because our MIPS simulator doesn’t have any cool graphics features.

Note that you don’t have a choice of sorting algorithm, you must implement insertion sort! Also note that the operation of swapping two array elements must also be a subroutine, and that subroutine should be called from within the sorting subroutine! Finally, please make sure that you access the array only through the subroutine’s parameters and not directly as a global!

Your job? Add the necessary subroutines to the file insertion.s of course!

Problem 2: Iterative and Recursive Fibonaccis

The starter code includes an iterative (iter_fact.s) as well as a recursive (recu_fact.s) implementation of the Factorial function complete with a simple driver program (actually there are two recursive ones, but you just have to pick one of them to follow). For this problem you’ll write analogous functions/programs to compute the n-th Fibonacci number instead. The Fibonacci numbers are defined by the following recurrence:

F(n) = F(n-1) + F(n-2)
F(1) = 1
F(0) = 0

Your driver program should read an integer from standard input and then print the corresponding Fibonacci number to standard output. You may reuse the driver program from the starter code if you want, it doesn’t require any changes. Please call your iterative Fibonacci program iter_fib.s, your recursive Fibonacci program should be called recu_fib.s.

Problem 3: Integer Square Root

For this problem you’ll write a function (and driver program) that computes the integer square root of a 32-bit integer. The integer square root of a positive integer n is the greatest integer less than or equal to the (actual) square root of n.

Your driver program isqrt.s should read an integer from standard input and then print the corresponding integer square root to standard output. You may (once again) reuse the driver program from the previous problem.

Here’s the twist: Your function should be fast! There’s no easy measure for “fast” here other than the number of MIPS instructions you execute for a given input. So you’ll want to make sure to run as few instructions as possible for computing the integer square root! Also you cannot use floating point MIPS instructions, everything has to be done with integer instructions!

Problem 4: The MIPS-y Truth

One nice thing about MIPS assembly and SPIM in particular is that the tools hide a lot of the complications of the actual CPU from assembly language programmers. However, knowing the full truth (and nothing but the truth) about your CPU is actually rather helpful for hacking good assembly code. So for this problem, we’ll leave behind all those cozy comforts you may already have become used to and deal with the “true MIPS” instead.

Your job? First read the delay.s code we provided for you carefully, then convert it to equivalent code (see the file itself for details!) that will run on the bare MIPS with identical results as the original code produced for the nice MIPS. Here is the output you want to get, without any errors and without fundamentally changing the program; for example, you’re not allowed to remove the memory references from the code even though there are more than enough registers available.

$ spim -bare -file delay.s
SPIM Version 8.0 of January 8, 2010
Copyright 1990-2010, James R. Larus.
All Rights Reserved.
See the file README for a full copyright notice.
Loaded: /usr/lib/spim/exceptions.s
333

Strictly speaking this is an “optional” problem. However, seeing as homeworks are already “optional” this semester, that’s sort of meaningless. (You should still be familiar enough with the bare MIPS to answer basic questions about it, on a quiz for example.)