Funstacktic!

Thu, Feb 23, 2023 | tags: stack dc forth uxn varvara programming

In the last year or so I have found myself fascinated by stack-based programming. I think my first experience with stack-based communication with a computer actually harkens back about 10 years or so when I discovered that the venerable bc calculator actually used to be a frontend for a program called dc.

dc

Dc is a so-called reverse polish notation (RPN) calculator available as a command line program on Unix-like systems. When I first discovered it I knew nothing about RPN-based calculators but the fact that they allow you to get rid of groupings with brackets in your arithmetic expressions (as well as an “equal” button, potentially) fascinated me. I don’t remember how or when it happened but gradually I started to use dc instead of bc for all my everyday arithmetic needs. It took another eight years or so until I learned (through a Hacker News entry, perhaps) that RPN calculators used to be a thing in HP’s history. While HP doesn’t seem to make any RPN calculators anymore, I found out that there is a small company based in Switzerland (where I currently reside as well), that builds HP RPN calculator clones. The company is called “SwissMicros”.

SwissMicros

I don’t remember how I found SwissMicros and their products but most likely it was while reading about the HP calculators. Somebody must have mentioned their products online because they seem to be the one that come closest to the RPN glory of the old HP ones.

What SwissMicros is doing is to recreate the HP RPN calculators of yore very faithfully using Open Source software. The hardware itself is not cheap (and sadly also not Open) but, according to reviews, of very convincing quality (one would hope so if you pay more than 200$ USD for one of their flagship products).

Their calculators are more or less modernised copies of the old RPN HP ones. They go as far as basing the naming on them (the clone of the old “HP-42S” is called “DM42” for example) and linking to the old HP RPN calculator documentation on their site. These are programmable RPN calculators that are 20+ times faster than the old HP ones and sport a much nicer display as well.

If I wouldn’t have access to dc every time I have to do some (very basic) math, I could potentially justify the purchase but unfortunately as things stand currently, I cannot …

Uxn and Varvara

Uxn is described as a virtual stack machine (or CPU) that is programmable in its own assembly language called “uxntal”. Both the virtual CPU as well as Varvara, the computer built with it, have been devised by a community that grew out of the 100 Rabbits collective.

The Uxn CPU is stack-based which means all CPU operations manipulate values on the stack (this CPU doesn’t have any registers). These operations are written in the uxntal assembly language and compiled to binaries by the uxnasm assembler. The compiled files are called “ROM”s and can be directly executed by the uxnemu emulator.

What is especially cute about this effort is that the Varvara virtual computer running on Uxn has I/O devices like a controller and a (virtual) screen built-in as some sort of “memory-mapped” devices. This makes it quite easy to get some graphical output from the screen on one of the Varvara implementations by writing only a few uxntal assembly instructions.


|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]

@paint-pixel
    #0020 .Screen/x DEO2      ( set x* position )
    #0030 .Screen/y DEO2      ( set y* position )
    #42 .Screen/pixel DEO     ( draw 4-fg 2-color2 )
JMP2r

The above assembly instructions will write one pixel at the x=0x20, y=0x30 position on the screen (note that the “Screen” here just denotes a memory region to write to and the “DEO” instruction writes a value on the stack to the device page).

While the easiest way to get started with Uxn/Varvara is the desktop emulator (which is implemented in C and uses SDL2 for the implementation of Varvara’s devices like the screen), there exist a number of other implementations of the Varvara computer, some of which are running on non-x86 hardware.

Forth

The most commonly known stack-oriented programming language is probably Forth and the design of Uxn was also inspired by it.

I knew next to nothing about it but its simplicity and low resource usage intrigued me enough to invest some time into learning the basics of the language.

It took me way longer than I would like to admit but I did manage to write a simple fizzbuzz implementation in Forth (see below).

: divisible3 ( a -- a%3 == 0 )
  3 mod 0 =
  ;

: divisible5 ( a -- a%5 == 0 )
  5 mod 0 =
  ;

: output { a b -- }
  a invert b invert and
    if
      dup .
    else
      a
        if
          s" fizz" type
        endif
      b
        if
          s" buzz" type
        endif
    endif cr ;

: divisible5and3 { a -- }
  a divisible3 a divisible5 ;

: fizzbuzz ( a -- )
  0 begin
    1+ dup dup divisible5and3 output
    99 >
  until
  ;

fizzbuzz bye

My implementation definitely doesn’t follow the common Forth conventions but I think it helps to illustrate how this programming language can be used to solve problems (tested only with Gforth).

I hope you enjoyed reading about my discovery of stack-based computing and found something intriguing for yourself to look into further as well!

Don’t hesitate to send comments and/or questions to my public-inbox (view).