CS221 Project #2
CS221 Project #2
This project focuses on the use of a stack to pass parameters to a recursive procedure.
For this project the stack should be increased, you decide by how much, based on your
first few runs of test data. You can reuse code of previous labs and projects.
For this project, you must be able to assemble, link, and execute assembler programs,
use the stack to pass parameters and use recursive procedure calls as covered in
Lab #4. Do Lab #4 before this project .
For Project #2, you must write an assembly language program that meets the following
specifications:
The program must prompt the user for a:
- number of disks,
- a starting pole position and
- an ending pole position for a Towers of Hanoi problem.
Note: The starting pole # and ending pole # must not be the same and your program
must do error checking and error recovery.
The range of pole #s is 1, 2 or 3.
The number of disks may range from 3 up to 7.
These three variables must be inputted from the keyboard. Echo to the user the input
parameters in a nicely formatted way.
The program must generate the series of disk moves to solve the puzzle. The
moves should be printed to the screen as a sequence of distinct moves i.e. number
each move from the first move to the last move.
Redirect the output to a file on the command line. Make liberal use of whitespace to
have a nicely formatted output.
Change the text printed by the Greet procedure so that it prints an appropriate title
or name for the program, your name as programmer, and the date on which you
turn in your project.
Your program must work correctly with any possible combination of the proper
inputs.
Pseudo code for an algorithm follows the project description.
Your program must use recursive procedure (subroutine) calls to solve the
problem. Use the BP register to access the parameters on the stack without
popping them off. The stack must be cleaned when the procedure returns to itself
and the main calling program.
Your code must be well structured and it must have comments that make it read
like a high level language with assembly language code inserted after each of the
lines of high level language.
The Towers of Hanoi
The towers of Hanoi puzle was first posed by a French professor, E. Lucas, in 1883.
Although commonly sold today as a child’s toy, it is often discussed in computer
science books because it provides a good example of recursion. In addition, its analysis
is straightfoward and it has many possible variations.
The object of the Towers of Hanoi problem is to specify the steps required to move the
disks (or rings) from pole r ( r = 1, 2 or 3) to pole s ( s = 1, 2 or 3; s ≠ r ), observing the
following rules:
Solution:
The algorithm to solve this probelm exemplifies the recursive paradigm. We image that
we know a solution for n-1 disks (“reduce to a previous case”) , and then we use this
solution to solve the problem for n disks. Thus to move n disks from pole 1 to pole 3,
we would:
1. move n-1 disks (the imagined known solution) from pole 1 to pole 2. However
we do this , the nth disk on pole 1 will never be in our way because any valid
sequence of moves with only n-1 disks will still be valid if there is an nth
(larger) disk always sitting at the bottom of the pole 1.
3. use the same method as in step 1 to move n-1 disks now on pole 2 to pole 3.
Algorithm in pseudo-code:
procedure H(n, r, s)
else {
H(n-1, 6-r-s, s)
endif