# Non-recursive towers of hanoi solution. # It does use a stack, of course, but with # significantly fewer pushes and pops than # a normal recursive subroutine would use. MAIN: save 4 # number of disks. bsr HANOI end HANOI: restore I0 le I0, 0, end_of_sub save 0 # Sentinel set S0, "a" set S1, "b" set S2, "c" eq I0, 1, doprint pushsome: dec I0 exchange S1, S2 push S2 push S1 push S0 push I0 gt I0, 1, pushsome doprint: print "Move from tower " print S0 print " to tower " print S2 print ".\n" dec I0 if I0, justswitch restore I0 eq I0, 0, end_of_sub # sentinel? restore S0 restore S1 restore S2 branch doprint justswitch: exchange S0, S1 gt I0, 1, pushsome branch doprint end_of_sub: ret