SUBLEQ on the Commodore VIC-20

I have created a SUBLEQ Virtual Machine for the Commodore VIC-20. SUBLEQ is a computer architecture that has only one instruction: SUBLEQ. The instruction stands for SUbtract and Branch if Less than or EQual to zero. Code for the VM can be found in the subleq_vic20 repo on GitHub.

SUBLEQ (SUbtract and Branch if Less than or EQual to zero)

The SUBLEQ instruction stands for SUbtract and Branch if Less than or EQual to zero. Because there is only one instruction, only the operands are specified, which consist of 3 memory addresses that are acted on as follows:

SUBLEQ a, b, c
Mem[b] := Mem[b] - Mem[a]
if (Mem[b] ≤ 0) goto c

To find out more about SUBLEQ have a look at my previous article: SUBLEQ - A One Instruction Set Computer (OISC) and its accompanying video.

Example: echo

The following executes a simple SUBLEQ program that loops continuously, accepting a key as input and then displaying it on the screen. It is taken from an example in the subleq_vic20 repo.

            jmp main

; Point the SUBLEQ VM's memory to the SUBLEQ program code
SL_MEM      = prog

; Include the SUBLEQ virtual machine
#include "subleq.a65"

main        ; SL_run starts the VM executing at SL_MEM
            jsr SL_run

; The SUBLEQ program code
prog        .word -1, -1, 3
            .word 6, 6, 0
            .word 0

This SUBLEQ program would look like the following in my sblasm assembly language, which uses sble to indicate a SUBLEQ instruction.

.equ        IN  -1
.equ        OUT -1

loop:       sble  IN, OUT
            sble  z, z, loop
z:          .word 0

The assembler assumes that if the last argument of the sble instruction isn't supplied then it will be equal to the next location in memory. It doesn't actually store an opcode for the sble instruction as there is only one instruction so only the operands are needed.

Creating a New SUBLEQ Program Loader

This SUBLEQ VM for the Vic uses 16-bit words and to load a new SUBLEQ program into it you can follow these steps.

  1. Create a file containing you're SUBLEQ program as a series of numbers. To do this you can use my sblasm assembler. Which also contains a number of examples to try and contains a file to link to '' to provide correct values for the VIC-20.
  2. Use the sqtoword.tcl script to convert the output from sblasm to .word entries, using someting like the following for a fizzbuzz program:
    $ tclsh sqtoword.tcl -n 12 fizzbuzz.sq > fizzbuzz.words
    The -n 12 indents the lines by 12 spaces to improve text alignment.
  3. There are included skeleton files for unexpanded and 8k+ systems. Append the output of sqtoword.tcl to one of them:
    $ cat skeleton_8k.a65 fizzbuzz.words > fizzbuzz.a65
  4. Assemble the file using XA, making sure that the path to 'subleq.a65' is in the include path:
    $ xa -o fizzbuzz.prg fizzbuzz.a65
  5. Load the program on your VIC-20 with 8K+ ram expansion and type RUN to start it.

Video Demonstrating the SUBLEQ VM on the VIC-20

You can see the SUBLEQ VM being used on a Commodore VIC-20 in the following video. It shows the process of assembling a 'rock, paper, scissors' game written in SUBLEQ using sblasm, creating a .a65 file, assembling that with XA and then running it on a VIC.

Creative Commons License
SUBLEQ on the Commodore VIC-20 by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post


Sign up to get new articles straight to your inbox.

Delivered by FeedBurner

Related Articles

Sweet 16 (The 6502 Dream Machine) Ported to the VIC-20

Sweet 16 was created by Steve Wozniak to reduce code size and make it easier to handle 16-bit pointers and arithmetic for his Apple Integer BASIC. He wrote it around 1977 and referred to it in an arti...   Read More

Position Independent Code (6502) on the Commodore VIC-20

If we are writing 6502 machine code and want to to create a routine or program that can be placed in any location then we have to create Position Independent Code (PIC) or make the code relocatable. H...   Read More

Adding Basic Stubs to Assembly Language on the Commodore VIC-20

To make machine language programs more friendly it's nice to add a Basic stub which contains a line with a SYS statement to start the code. This is easy to do on the VIC-20 and the process gives you a...   Read More

Hand Assembling to Machine Code on the Commodore VIC-20

I quite enjoy designing machine language routines on paper and then hand assembling them. For many people this would have been their only option until they got a more advanced machine language monitor...   Read More

Creating a TTY Simulator in Assembly Language on the VIC-20

The Vicmon machine language cartridge is an excellent tool for exploring the Vic-20. This article and its associated video will show you how to use it to create a simple program that will change the n...   Read More