Purpose:
__|__ | | |_____| | | ____|____ | | |_________| | | _______|________ | | |_______________| | | | | | ________|_________________|_________________|______________ PEG1 PEG2 PEG3The puzzle is begun by placing all of the discs on one of the pegs, from largest on the bottom to smallest on the top (see diagram above). The goal is to move all of the discs to another peg, following these simple rules:
____|____ |_________| __|__ WRONG! |_____| | ________|________
Here is a C version of the solution to the puzzle:
/* This program provides the solution to the */ /* Towers of Hanoi problem written in C. */ #include <stdio.h> static char peg1[] = "one"; static char peg2[] = "two"; static char peg3[] = "three"; void Hanoi(char * source, char * spare, char * destination, int n) /* This function takes a tower of n discs and prints */ /* instructions for moving from peg 'source' to peg */ /* 'destination'. Peg 'spare' may be used temporarily. */ { if (n>0) { Hanoi(source, destination, spare, n-1); /* recursive call */ printf("Move disk from %s to %s\n", source, destination); /* move one disk */ Hanoi(spare, source, destination, n-1); /* recursive call */ } } main() { int n; printf("Please enter the number of disks: "); scanf("%d",&n); printf("\n"); Hanoi(peg1,peg2,peg3,n); }
Prelab:
Task 1: Create a "lab" directory and copy the lab file: "/course/csu380jc/.www/lab5/lab5.s".
Task 2: Study the "main:" procedure, figure out how it operates. It is currently set so that 4 discs begins on the first peg. For checkoff you will have to start with 5 discs.
Task 3: Go to the "hanoi:" procedure and fill in the prologue and epilogue, plus anything else you need, using the MIPS version that you developed in the prelab. The procedure "hanoi" receives the source peg in $a0, "spare" peg in $a1, destination peg in $a2, and number of discs (n) in $a3.
Your routine will call "print_message" for printing moves.
Your program should work for any arbitrary non-negative number of starting disks, i.e. your program must work for n=0 but not n=-1. Sample Output:
Move disk from one to two
Move disk from one to three
Move disk from two to three
Move disk from one to two
Move disk from three to one
Move disk from three to two
Move disk from one to two
Move disk from one to three
Move disk from two to three
Move disk from two to one
Move disk from three to one
Move disk from two to three
Move disk from one to two
Move disk from one to three
Move disk from two to three
For Checkoff:
Show me your MIPS code and run your program for n=5 discs.