Hello, World! in SUBLEQ Assembly

  /     /   Assembly     OISC     Programming     SUBLEQ    

After writing a previous article: The SUBLEQ URISC (Ultimate RISC) / OISC (One Instruction Set Computer) Architecture. I was left thinking that I should really have given at least a "hello, world" program as a demonstration. I was then inspired after seeing John Metcalf's post: Hello World for the RSSB Virtual Computer. This showed that these OISCs don't require as much code as people may think to do what you want. So here is my version of a "hello, world" program for the SUBLEQ Architecture.

Assembly Source Code

The following assembly code is written for Oleg Mazonka's SUBLEQ assembler, which can be found on his page. I have saved it to a file called helloworld.sq

############################################
#  FileName:   helloworld.sq
#  Author:     Lawrence Woodman
#  Date:       26th March 2009
#===========================================
#  Outputs 'hello, world'
############################################
loop:       hello (-1)              # Outputs character pointed to by hello
            minusOne loop           # Increments character output pointer to next character
            minusOne checkEnd+1     # Increments end of string pointer to next character
checkEnd:   Z hello (-1)            # Halts program if character at pointer is zero
            Z Z loop                # Jumps to loop

# Data Storage
. minusOne: -1                      # Used to increment pointer
. hello: "hello, world\n" Z: 0      # Zero terminated string to print

The program is fairly simple and essentially manipulates two pointers, one to contain the address of the character to be output and the other to contain the address which is checked for zero termination of the string. You will see that the pointers are actually operands of SUBLEQ instructions, so therefore we are manipulating operands to create a pointer. This is typical of programming in SUBLEQ assembly.

To recap from my previous article on SUBLEQ Architecture. Every line is assumed to be a SUBLEQ instruction, so just the operands are specified. The SUBLEQ instruction works as follows:

SUBLEQ = Subtract and Branch if Less then or Equal to zero

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

There are a couple of points to note in the above assembly source code:

  • (-1) is used on the line labeled loop to indicate that the result should be output to STDOUT.
  • (-1) is used on the line labeled checkEnd to halt the program as -1 is an invalid address to jump to.
  • (-1) is in parenthesis to indicate another expression, as opposed to hello-1, which would be the address prior to hello.
  • The assembler assumes that the branch destination is the next address for the three lines that only have two operands.
  • The period on the last two lines indicates that these lines don't contain instructions and hence are not operands of a SUBLEQ instruction.

I assembled the file using:

$ sqasm < helloworld.sq > helloworld.out

Object Code

Assembling produces the file helloworld.out, which contains the following:

16 -1 3
15 0 6
15 10 9
30 16 -1
30 30 0
-1
104 101 108 108 111 44 32 119 111 114 108 100 33 10 0

Executing the Program

To run this you just use:

$ sqrun helloworld.out

Which produces the correct output:

hello, world

Further Information

To find out more about SUBLEQ architecture have a look at my article mentioned above: The SUBLEQ URISC (Ultimate RISC) / OISC (One Instruction Set Computer) Architecture. This offers a fuller explanation and contains a few links of interest.

Creative Commons License
Hello, World! in SUBLEQ Assembly by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Related Articles

Adding a Basic Stub to a Vic-20 Assembly Language Program

To make machine language programs more friendly it is 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 ...   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

Beginning Assembly Programming on the Commodore Vic-20

The Commodore Vic-20 is a great machine to learn an assembly language on. It was released in 1981 and was the first computer to sell one million units, which contributes to its popularity today. The ...   Read More

Improving the Standard SUBLEQ OISC (One Instruction Set Computer) Architecture

When I first came across The SUBLEQ URISC (Ultimate RISC) / OISC (One Instruction Set Computer) Architecture, I really liked the beauty and simplicity of the design. However, I have now been experimen...   Read More

The Smallest Communication Program in the World?

I was going through a backup of my dos machine, taken in 1998, and came across some source code which I haven't seen for a long time. It was great to see that old code, and I must set-up a machine so ...   Read More

Sign up to get new articles straight to your inbox.

Delivered by FeedBurner

Comments

blog comments powered by Disqus