Xv6 with picoc & Linkage editor  v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
 All Data Structures
30_hanoi.c
00001 /* example from http://barnyard.syr.edu/quickies/hanoi.c */
00002 
00003 /* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */
00004 /* By Terry R. McConnell (12/2/97) */
00005 /* Compile: cc -o hanoi hanoi.c */
00006 
00007 /* This program does no error checking. But then, if it's right, 
00008 it's right ... right ? */
00009 
00010 
00011 /* The original towers of hanoi problem seems to have been originally posed
00012 by one M. Claus in 1883. There is a popular legend that goes along with
00013 it that has been often repeated and paraphrased. It goes something like this:
00014 In the great temple at Benares there are 3 golden spikes. On one of them,
00015 God placed 64 disks increasing in size from bottom to top, at the beginning
00016 of time. Since then, and to this day, the priest on duty constantly transfers
00017 disks, one at a time, in such a way that no larger disk is ever put on top
00018 of a smaller one. When the disks have been transferred entirely to another
00019 spike the Universe will come to an end in a large thunderclap.
00020 
00021 This paraphrases the original legend due to DeParville, La Nature, Paris 1884,
00022 Part I, 285-286. For this and further information see: Mathematical 
00023 Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967,
00024 303-305.
00025 *
00026 *
00027 */
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #define TRUE 1
00033 #define FALSE 0
00034 
00035 #define N 4    /* This is the number of "disks" on tower A initially. */
00036                /* Taken to be 64 in the legend. The number of moves 
00037                   required, in general, is 2^N - 1. For N = 64, this is
00038                   18,446,744,073,709,551,615 */
00039 
00040 int A[N], B[N], C[N];  /* These are the three towers. For example if the
00041 state of A is 0,1,3,4, that means that there are three discs on A of sizes
00042 1, 3, and 4. (Think of right as being the "down" direction.) */
00043 
00044 void Hanoi(int,int*,int*,int*);
00045 
00046 /* Print the current configuration of A, B, and C to the screen */
00047 
00048 void
00049 PrintAll()
00050 {
00051         int i;
00052 
00053         printf("A: ");
00054         for(i=0;i<N;i++)printf(" %d ",A[i]);
00055         printf("\n");
00056 
00057         printf("B: ");
00058         for(i=0;i<N;i++)printf(" %d ",B[i]);
00059         printf("\n");
00060 
00061         printf("C: ");
00062         for(i=0;i<N;i++)printf(" %d ",C[i]);
00063         printf("\n");
00064         printf("------------------------------------------\n");
00065         return;
00066 }
00067         
00068 /* Move the leftmost nonzero element of source to dest, leave behind 0. */
00069 /* Returns the value moved (not used.) */
00070 
00071 int Move(int *source, int *dest)
00072 {
00073         int i,j;
00074 
00075         while (i<N && (source[i])==0) i++;
00076         while (j<N && (dest[j])==0) j++;
00077 
00078         dest[j-1] = source[i];
00079         source[i] = 0;
00080         PrintAll();       /* Print configuration after each move. */
00081         return dest[j-1];
00082 }
00083 
00084 
00085 /* Moves first n nonzero numbers from source to dest using the rules of Hanoi.
00086    Calls itself recursively.
00087 */
00088 
00089 void
00090 Hanoi(int n,int *source, int *dest, int *spare)
00091 {
00092         int i;
00093         if(n==1){
00094                 Move(source,dest);
00095                 return;
00096         }
00097 
00098         Hanoi(n-1,source,spare,dest);
00099         Move(source,dest);
00100         Hanoi(n-1,spare,dest,source);   
00101         return;
00102 }
00103 
00104 
00105 int
00106 main()
00107 {
00108         int i;
00109 
00110         /* initialize the towers */
00111         for(i=0;i<N;i++)A[i]=i+1;
00112         for(i=0;i<N;i++)B[i]=0;
00113         for(i=0;i<N;i++)C[i]=0;
00114                 
00115         printf("Solution of Tower of Hanoi Problem with %d Disks\n\n",N);
00116 
00117         /* Print the starting state */
00118         printf("Starting state:\n");
00119         PrintAll();
00120         printf("\n\nSubsequent states:\n\n");
00121 
00122         /* Do it! Use A = Source, B = Destination, C = Spare */
00123         Hanoi(N,A,B,C);
00124         return 0;
00125 }
 All Data Structures