kernelthread.com

Parrot Virtual Machine Assembly

The Towers of Hanoi as a program using the Parrot Virtual Machine Assembly language.

Parrot is the name of a new (as of January, 2003 at least) virtual machine. Parrot is object-oriented, introspective, and language agnostic. Like other modern virtual machines, Parrot is driven by bytecode and supports continuations. However, unlike almost all of today's popular, stack-based, RISC-like virtual machines (such as Java's and .NET's), Parrot is register-based and a CISC-analog.

While Parrot was originally conceived of as the virtual machine for Perl 6, it now encompasses the whole family of dynamic programming languages.

More information on Parrot can be found on the Parrot website.

# 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

Download

hanoi.pasm