COSC 201: Computer Organization

Lab 3: Recursive Functions in Assembly Language

Purpose
Learn how functions are implemented in assembly language.
Method
Write assembly code to implement a simple program given in C++, using the outline supplied.
Preparation
Read Chapter 2 in the text.
Files to Use
towers.cpp, main.s, towers.s
What to Hand In
Completed towersXX.s (XX are your initials or name)

The file towers.cpp contains a simple C++ program that finds the number of moves needed to solve the classic Towers of Hanoi puzzle. In this problem a stack of discs (20 gold discs in the classic myth) are stacked, each one in the stack a bit smaller than the one beneath. Using two other positions, the problem is to move the stack from the first position to the third, using the second as an intermediate resting place. Discs must be moved one at a time (gold is heavy), and a larger disc cannot be placed on top of a smaller one. According to the myth, when the priests finish moving the tower of discs according to these rules, the world will end.

The C++ program towers.cpp uses a recursive function to solve the problem, printing each of the moves and the total moves needed. The file main.s has the MIPS assembly code corresponding to the main C++ program and the print function. The file towers.s has an outline of the MIPS assembly code for the recursive function towers.

You should complete the assembly code for towers.s, using the conventions for MIPS function calls and register usage as discussed in the textbook and class. Compare the output of your code to the sample output for one, two, three, four, five, six, seven, and eight discs.


One aspect of this code is that a recursive call to the towers function is made with the first parameter, the number of discs to move, equal to 1. This is, of course, the base case of the recursion. It requires the full function treatment of saving and restoring information on the stack, when all it does is a simple call to print and return 1. Modify your assembly code so that no recursive call is made when the number of discs to be moved is one, but instead "inline" the appropriate code at the appropriate place, so the behavior of the function is the same (except when the original call is with the first parameter equal 1, which shold be disallowed by changing the preconditions appropriately). Call your revised version towersopt.s. Compare the number of instructions executed for this version compared to the original version for numdiscs = 8; what is the speedup? (Put the speedup into the toweropt.s file as a comment.)

Hint: be careful of the case where the number of discs parameter is 2!


Hand in your files towersXX.s (XX are your initials or name) and towersoptXX.s by emailing them to the instructor.