However, given the lower level of operations within the Parrot engine, a different algorithm to solve this problem was employed, than that used in my signature previously. The equivalent perl code for the algorithm employed follows the Parrot code.
set S0, "00000000000000000000001001000010" length I0, S0 set I1, 1 set I2, 0 LOOP: dec I0 substr S1, S0, I0, 1 if S1, INC RETURN: mul I1, I1, 2 ge I0, 0, LOOP print I2 print "\n" end INC: add I2, I1 branch RETURN
And then compiling the Parrot assembler to bytecode and executing this:
rob@kathmandu:~/parrot-0.0.10$ perl assemble.pl count.pasm >count.pbc rob@kathmandu:~/parrot-0.0.10$ ./parrot count.pbc 578 rob@kathmandu:~/parrot-0.0.10$
For those unfamiliar with Parrot assembly, the Parrot code above is roughly equivalent to the following Perl code:
$s0 = "00000000000000000000001001000010"; $i0 = length $s0; $i1 = 1; $i2 = 0; while ( $i0 >= 0 ) { --$i0; $s1 = substr( $s0, $i0, 1 ); if ( $s1 eq '1' ) { $i2 += $i1; } $i1 *= 2; }; print $i2, "\n";
From my foray into Parrot assembler this weekend, I must say that I am quite impressed with its development and documentation to date. For those interesting in following my lead and investigating Parrot, I would recommend the following pages and resources:
rob@kathmandu:~/parrot-0.0.10$ perl assemble.pl count.pasm >count.pbc ; ./parrot count.pbc
|
|---|