CS 60 Fall 1997, Optional Assignment 9

This may be submitted for 40 points extra credit.

Recursion in Assembly Language

Due Monday December 15

This assignment exposes you to a simple assembly language and helps you learn how recursion is implemented at a low level within a computer.

Develop an ISCAL (ISC assembly language) program which repeatedly accepts an input integer N (>= 0) and, using recursion, prints out the spindle numbers of the N disk Towers-of-Hanoi problem. This problem is described in the notes (States and Transitions). For example, for N = 3, the solution printed will be (one number per line):

1 2    1  3    2  3    1  2    3  1    3  2    1  2

meaning:

move top disk from spindle 1 to spindle 2,
move top disk from spindle 1 to spindle 3,
move top disk from spindle 2 to spindle 3,
move top disk from spindle 1 to spindle 2,
move top disk from spindle 3 to spindle 1,
move top disk from spindle 3 to spindle 2,
move top disk from spindle 1 to spindle 2.

The following java method will print the solution when called with move(N, 1, 2, 3):

  // move N disks from spindle From to spindle To,
  // with Other as the remaining spindle
 
  static void move(int N, int From, int To, int Other)
    {
    if( N <= 0 )
      return;                    // no disks to move
 
    move(N-1, From, Other, To);  // move top N-1 disks from From to Other
    System.out.println(From);    // move Nth disk from From to To
    System.out.println(To);
    move(N-1, Other, To, From);  // move top N-1 disks from Other to To
    }

For N disks, the number of moves required is 2N-1. However, the stack space required for the recursion is only O(N), so your program should be able to handle any N up to the word-size of the machine (32 bits) without running out of stack space.

Note that non-recursive solutions are possible, but a recursive solution is specified for this problem. That is, the primary purpose of this assignment is learning to implement recursion, not solving the Towers-of-Hanoi problem.

Here are some hints and suggestions:

  1. Directory turing: /cs/cs60/isc/ contains several examples of ISC code, in addition to what is in the notes and available in the ISC web page. Note that the assembler and simulator (called isc) has a trace facility that can show you each instruction as it executes.
  2. Start with program io.isc which just echoes the input to the output. You can build on this.
  3. Program rfac.isc is a recursive factorial program. It is similar in that it implements recursion with a stack. It is different in that it requires only one argument rather than four. It also requires a return value, which the current program does not.
  4. The java program for this problem is in hanoi/hanoi.isc along with several output examples: hanoi01.out, hanoi02.out, etc.
  5. One possible approach would be to first convert the java program to a non-recursive one using an array to implement a stack, then transcribe that program to ISCAL. This way you can get an understanding of recursion implementation apart from the issues of dealing with ISCAL. If you choose this route, you will have to simulate saving a return address using some kind of switch or conditional statement.