# The Towers Of Hanoi # Parrot VM # Copyright (C) 2003 Amit Singh. All Rights Reserved. # http://hanoi.kernelthread.com # # Last tested under Parrot version 0.0.0-10devel # http://www.parrotcode.org # # Usage: # # % assemble.pl hanoi.pasm -o hanoi.pbc # % parrot hanoi.pbc 3 main: set I0, P0[1] # I0 = argv[1] (n) lt I0, 1, error # if (argv[1] < 1) ... error gt I0, 10, error # if (argv[1] > 10) .. error set I1, 1 # I1 = 1 (f) set I2, 2 # I2 = 2 (u) set I3, 3 # I3 = 3 (t) bsr hanoi # hanoi(n, f, u, t) branch done # OK error: print "usage: hanoi n, where 0 < n <= 10\n" done: end hanoi: # hanoi(n, f, u, t) pushi # input save I0 # n save I1 # f save I2 # u save I3 # t gt I0, 0, run # if (n > 0) branch out # if (n <= 0) GOTO out run: dec I0 # n = n - 1 set I4, I3 # tmp = t set I3, I2 # t = u set I2, I4 # u = tmp bsr hanoi # hanoi(n - 1, f, t, u) print I1 # printf("%d", f) print " --> " # printf(" --> ") print I2 # printf("%d", t) print "\n" # printf("\n") set I4, I1 # tmp = f set I1, I3 # f = u set I5, I2 # tmp1 = t set I2, I4 # u = f set I3, I5 # t = tmp1 bsr hanoi # hanoi(n - 1, u, f, t) out: popi # input restore I3 # t restore I2 # u restore I1 # f restore I0 # n ret