Inside-out loops

This is a silly post. I am going to show you a contrived example of Perl code that is emulating what 8-bit assembler code does to loop through a 16-bit value. Why?

I have an ongoing researching project involving the Atari 2600 Video Console System, which has a MOS 6502 microcontroller at its heart. Assembler is not my native tongue and it helps to unpack these squirrelly bits into Perl to verify my understanding.

In a larger sense, though, seeing other ways to implement common tasks is salutary. If nothing else, you may appreciate how much lift you get from using higher level languages.

Anyway, the code:

my ($MAX_X, $MAX_Y) = (256, 256);
my ($x, $y) = (0, $MAX_Y);
printf("%-3d", $y);

   $x = $MAX_X;

   $x -= 1;
   printf("\t%-3d\n", $x);
   goto DEC_X if $x;

   print "-----------\n";
   $y -= 1;
   printf("%-3d", $y);
   goto RELOAD_X if $y;

print "\ndone\n";

This code essentially decrements the least significant byte ($x) until it is zero. It then decrements the most significant byte ($y). If $y is greater than zero, the program reloads $x to the maximum value begins decrementing it to zero again. The program ends when $y is zero.

No loop directives. No subroutines. Just goto spaghetti.

You're welcome.

Is this thing on?

This year, I begin my nineteenth year of professional programming with Perl. It seems that I just can quit you, Perl.

Not that there is a compelling reason to do so. Sure, there are a lot of "hot" newish language/frameworks out there, like node.js, ruby on rails and python/django. And those tools are worth getting to know, not the least of which reason to do so is in the pursuit of paying gig.

Popularity of a technology is mostly important when you start a project and when all technologies are equally alien to you. In my case, Perl is home. It is the tool I can use nearly…

About Joe Johnston

user-pic I blog about Perl and related technologies primarily from a webdev perspective.