Copyright © tutorialspoint.com
Parrot programing is similar to assembly language programing and you get a chance to work at lower level. here is the list of programming examples to make you aware of the various aspects of Parrot Programming.
Create a file called hello.pir that contains the following code.
Then run it by typing:
As expected, this will display the text Hello world! on the console, followed by a new line (due to the \n).
In this above example, .sub _main states that the instructions that follow make up a subroutine named _main, until a .end is encountered. The second line contains the print instruction. In this case, we are calling the variant of the instruction that accepts a constant string. The assembler takes care of deciding which variant of the instruction to use for us. The third line contains the end instruction, which causes the interpreter to terminate.
We can modify hello.pir to first store the string Hello world!\n in a register and then use that register with the print instruction.
Here we have stated exactly which register to use. However, by replacing S1 with $S1 we can delegate the choice of which register to use to Parrot. It is also possible to use an = notation instead of writing the set instruction.
To make PIR even more readable, named registers can be used. These are later mapped to real numbered registers.
The .local directive indicates that the named register is only needed inside the current compilation unit (that is, between .sub and .end). Following .local is a type. This can be int (for I registers), float (for N registers), string (for S registers), pmc (for P registers) or the name of a PMC type.
This example introduces some more instructions and PIR syntax. Lines starting with a # are comments.
PIR provides a bit of syntactic sugar that makes it look more high level than assembly. For example:
Is just another way of writing the more assembly-ish:
Is the same as:
Is the same as:
As a rule, whenever a Parrot instruction modifies the contents of a register, that will be the first register when writing the instruction in assembly form.
As is usual in assembly languages, loops and selection are implemented in terms of conditional branch statements and labels, as shown above. Assembly programming is one place where using goto is not bad form!
The Fibonnaci series is defined like this: take two numbers, 1 and 1. Then repeatedly add together the last two numbers in the series to make the next one: 1, 1, 2, 3, 5, 8, 13, and so on. The Fibonacci number fib(n) is the n'th number in the series. Here's a simple Parrot assembler program that finds the first 20 Fibonacci numbers:
This is the equivalent code in Perl:
NOTE: As a fine point of interest, one of the shortest and certainly the most beautiful ways of printing out a Fibonacci series in Perl is perl -le '$b=1; print $a+=$b while print $b+=$a'
In this example we define a factorial function and recursively call it to compute factorial.
Let's look at the _fact sub first. A point that was glossed over earlier is why the names of subroutines all start with an underscore. This is done simply as a way of showing that the label is global rather than scoped to a particular subroutine. This is significant as the label is then visible to other subroutines.
The first line, .param int n, specifies that this subroutine takes one integer parameter and that we'd like to refer to the register it was passed in by the name n for the rest of the sub.
Much of what follows has been seen in previous examples, apart from the line reading:
This single line of PIR actually represents quite a few lines of PASM. First, the value in register $I0 is moved into the appropriate register for it to be received as an integer parameter by the _fact function. Other calling related registers are then set up, followed by _fact being invoked. Then, once _fact returns, the value returned by _fact is placed into the register given the name result.
Right before the .end of the _fact sub, a .return directive is used to ensure the value held in the register named result is placed into the correct register for it to be seen as a return value by the code calling the sub.
The call to _fact in main works in just the same was as the recursive call to _fact within the sub _fact itself. The only remaining bit of new syntax is the :main, written after .sub _main. By default, PIR assumes that execution begins with the first sub in the file. This behavior can be changed by marking the sub to start in with :main.
To compile PIR to bytecode, use the -o flag and specify an output file with the extension .pbc.
PIR can be turned into PASM by running:
The PASM for the final example looks like this:
PASM does not handle register allocation or provide support for named registers. It also does not have the .sub and .end directives, instead replacing them with a label at the start of the instructions.
Copyright © tutorialspoint.com