Programming journey

SBNZ, a single instruction virtual machine for fun

I have always loved playing with programming languages, compilers, and interpreters. One of my favorite languages to play with is a single instruction assembly language called SBNZ. In this programming journey, we first implement a virtual machine for SBNZ and then explore the power of the language.

If you would like company on this journey, I streamed my implementation live, follow this link to watch the video from that:

In order to have a programming language, we need to have a machine that can execute code. Therefore we start by making our own machine, a virtual machine. A virtual machine takes code as input and outputs the memory layout after executing the code.

The machine we want to implement has only a single instruction: Subtract and Branch if Not Zero (SBNZ). This virtual machine works on memory locations (or cells). The SBNZ instruction takes 4 arguments, , it subtracts the content of cell from the content of cell and stores the result in cell . If the result is not zero it jumps to the instruction at position , otherwise just go to the next instruction. We could write it is:

To terminate a program we just jump to a position outside the memory. For this demonstration, I have 1024 cells in my memory. Therefore jumping to cell 1024 terminates the program.

An SBNZ program could look like this:

This program subtracts 5 from what is in cell 1023 (the last cell), and stores the result in cell 1023. If that is zero it goes to the next instruction, otherwise it “jumps” to location 4, which is also the next instruction. The second instruction subtracts 0 from 1 and stores the result where 1 is because 1–0 is not zero it then jumps to cell 1024.

When we write we really mean a cell that contains . This means that the program actually would be:

Notice that because our machine only has one instruction there is no need to write it. The final SBNZ program becomes:

Exercise 1. Read a file containing up to 4x1024 numbers separated by spaces. Store these numbers in an int array of size 4x1024.

Exercise 2. Execute the solution to exercise 1 as an SBNZ program.

Exercise 3. Output the final memory to a file.

Exercise 4. Finish this SBNZ program:

Exercise 5. Finish this SBNZ program:

In the previous exercise we used the cell containing 5 as a variable, we can expand this principle:

Exercise 6. Finish this SBNZ program:

Because the program consists merely of numbers, and the data is also numbers, we can actually have the program change itself. Here means the cell .

Exercise 7. Finish this SBNZ program:


Exercise 8. Try putting other numbers instead of 99999. What changes?

Exercise 9. Translate this into SBNZ:

Having variables , , and .

What is the result if a = 3 and -b = -4?

Exercise 10. It is a bit weird that our variable is and not just . Add this line to the beginning of the program from the last exercise; .

Exercise 11. Translate this into SBNZ:

What is the result if a = 3 and b = 2?

If you got this far you must also enjoy compilers and programming. In my book, I discuss many useful ways to play with compilers and object-oriented programming. You can check it out:

Or directly on Manning:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christian Clausen

I live by my mentor’s words: “The key to being consistently brilliant is: hard work, every day.”