This may be submitted for 40 points extra credit.
Recursion in Assembly Language
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). See this link for an applet demonstrating the solution to the problem. 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, say, 10 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:
/cs/cs60/isc/hanoi/hanoi.test hanoi.iscwhere hanoi.isc is your ISC program. If your program is correct, no output should be produced (but the program will terminate), since the script produces the differences between your output and the correct output.