Old Article

This is an old article which is only being kept for historical purposes.

Please navigate and update any links to the new article: SUBLEQ - A One Instructin Set Computer (OISC).

Hello, World! in SUBLEQ Assembly

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.

Share This Post

Feedback/Discuss

Related Articles

Is SUBLEQ the Right Choice for a VM?

SUBLEQ is an interesting architecture because of its simplicity, adaptability and power. It is therefore an attractive choice for a simple virtual machine. However, this comes at a cost which we will...   Read More

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 ...   Read More

SUBLEQ - A One Instruction Set Computer (OISC)

SUBLEQ has to be one of the easiest architectures to implement in either software or hardware and this is the main reason for its design as a teaching aid. It has only one instruction, hence why it is...   Read More

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

When I first came across SUBLEQ, I really liked the beauty and simplicity of the design. However, I have now been experimenting with it for quite a while and have noticed one aspect of the standard im...   Read More

Modula-2 Compilers on CP/M

Modula-2 is a great language in general and is a good choice for programming on CP/M. There are three good compilers available for CP/M which all require a Z80 processor and we'll compare each in turn...   Read More