Hello‚ World! in SUBLEQ Assembly
Written by Lawrence Woodman on 29th March 2009     [2 Comments]
Tags: 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 TechTinkering is licensed under a Creative Commons Attribution 2.0 UK: England & Wales License.
Comments


Oleg   (29 March 2009 21:44)
This is amazing how terse your program is! And it takes a while to comprehend it. 
Just two points related to the assembler: 
- semicolon is a separator between instructions placed on the same line (not the next address)
- parenthesis indicate another expression [Otherwise hello -1 means (hello-1)‚ not hello (-1)‚ like (checkEnd+1) is not the same as checkEnd (+1)]



Lawrence Woodman   (01 April 2009 05:01)
Oleg‚

Thanks for the feedback.  I have now corrected the article.


Add a Comment

Please leave a comment.  I would love to hear what you think.
All comments are checked before displaying to prevent SPAM.

Your Name (Optional)

Your Email (Optional)
Not displayed, just used to pickup a Gravatar

Your Website (Optional)

Comment

All comments preformatted i.e. newlines and spaces display properly.
All HTML will be displayed literally e.g. <br /> will not create a line break.
URLs beginning with http:// or ftp:// will be converted to links.

        Privacy Policy

Contact Details

I would love to hear from you and welcome your feedback on the site.

You can keep in contact with me through the following services:
Click to get email address
Twitter
Delicious
StumbleUpon
YouTube
Technorati
FriendFeed
MyBlogLog