COSC 201: Computer Organization

Lab 3: Recursive Functions in Assembly Language

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

The file towers.c 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.c uses a recursive function to solve the problem, returning the total moves needed. The file towers.s has an outline of the ARMv8 assembly code for the recursive function towers.

You should complete the assembly code for towers.s, using the conventions for ARMv8 function calls and register usage as discussed in the textbook and class.

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 to 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 should be disallowed by changing the preconditions appropriately). Call your revised version towersopt.s.)

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

Hand in your files towersXX.s (XX are your last name, first initial) and towersoptXX.s by emailing them to the instructor.