OI
by Sandro Maffiodo

What is OI?

OI is a small interpreter of an OISC machine.
Wikipedia define an OISC like this:
"A one instruction set computer (OISC), sometimes called an ultimate reduced instruction set computer (URISC), is an abstract machine that uses only one instruction [...] an OISC is capable of being a universal computer in the same manner as traditional computers that have multiple instructions."

OI is a SUBLEQ interpreter. SUBLEQ is an instruction defined like this:
"The subleq instruction ("SUbtract and Branch if Less than or EQual to zero") subtracts the contents at address A from the contents at address B, stores the result at address B, and then, if the result is not positive, transfers control to address C (if the result is positive, execution proceeds to the next instruction in sequence)"
A SUBLEQ instruciton looks like this:
A B C
OI execute all the SUBLEQ instructions given as input. The end of the program is reached when an instruction jump to the special memory address -1.
A really simple example is:
3 4 6
7 7 7
3 4 0
The first instruction subtracts 7 (address 3) from 7 (address 4). The result in address 4 is 0, so goto 6. On address 6 is the instruction 3 4 0 which again substracts 7 from now 0 and jumps back to 0.

OI can also read from the standard input and write to the standard output, using the following special memory addresses: -1 and -2. These addresses must be used as documented here:

Numeric output
Write the number in A to the standard output and jump to C:
A -1 C
ASCII output
Write the character code in A to the standard output and jump to C. The character code must be an ASCII character code:
A -2 C
Numeric input
Read a number from the input and save (0-number) to B. If read operation fail B is set to the magic number -65535.The instruction always jump to C:
-1 B C
Reserve memory
Reserve 512 bytes of space. Save the address to the first reserved byte to B and jump to C:
-2 B C

Downloads

You can download OI binaries here (x86):
windows x86
mac x86_64
Or download sourcecode:
oi source
The program read the SUBLEQ source from the standard input. You can terminate a program with an EOF character or with the magic number -65535. This special memory address is usefull when you wanna cat more than one programs in one.

Examples

All the following examples are generated with my own OI Assembler. See the chapter about that.

1. Hello world
The following example is a program that print "HELLO WORLD!" to the output:
36 -2 3 37 -2 6 38 -2 9 38 -2 12 39 -2 15 40 -2 18 41 -2 21 39 -2 24 42 -2 27 38 -2 30 43 -2 33 44 -2 -1 72 69 76 79 32 87 82 68 33 -65535
download hello.oi
download hello.oasm
You can run this program by typing all number manually on the program or by using the unix cat:
cat hello.oi | oi
2. Add
A program that ask two numbers, sum them and display the result to the output:
49 -2 3 52 -2 6 -1 53 9 53 54 12 50 -2 15 52 -2 18 53 53 21 -1 53 24 53 55 27 54 48 30 55 48 33 56 56 36 48 56 39 51 -2 42 52 -2 45 56 -1 -1 0 65 66 67 61 0 0 0 0  -65535
download add.oi
download add.oasm
You can run this program by typing all number manually to the program and, after, inserting the two input numbers, or by using the unix cat:
echo 101 2321 > input; cat add.oi input | oi
3. Multiply
A simple program that ask two numbers, multiply them and display the result to the output:
52 52 3 53 53 6 54 54 9 60 60 12 56 -2 15 58 -2 18 -1 52 21 52 53 24 51 54 27 52 52 30 55 -2 33 58 -2 36 -1 52 39 59 59 42 52 59 45 54 53 61 60 60 42 -1 0 0 0 65 66 67 61 0 0 57 -2 64 58 -2 67 59 -1 -1 -65535
download mul.oi
download mul.oasm
You can run this program by typing all number manually to the program and, after, inserting the two input numbers, or by using the unix cat:
echo 101 2321 > input; cat mul.oi input | oi
4. SUBLEQ Interpreter!
And now, the best: A SUBLEQ interpreter writen in SUBLEQ!! This interpreter read a SUBLEQ program from the standard input, relocate the program to a new memory address and execute that, instruction by instruction:
-2 341 3 334 334 6 341 334 9 334 39 12 334 40 15 334 43 18 -1 330 21 330 -1 24 335 335 27 330 335 30 336 336 33 347 336 36 336 335 57 0 0 42 330 0 45 337 43 48 337 39 51 337 40 54 334 334 18 346 -2 60 346 -2 63 346 -2 66 334 334 69 341 334 72 334 102 75 334 105 78 334 108 81 337 105 84 337 108 87 337 108 90 343 343 93 331 331 96 332 332 99 333 333 102 0 331 105 0 332 108 0 333 111 334 334 114 338 334 117 331 334 159 334 334 123 337 334 126 331 334 159 334 334 132 335 335 135 341 334 138 334 335 141 331 335 144 237 237 147 334 334 150 335 334 153 334 237 156 334 334 168 340 343 162 237 237 165 331 237 168 334 334 171 338 334 174 332 334 222 334 334 180 337 334 183 332 334 222 334 334 189 335 335 192 341 334 195 334 335 198 332 335 201 238 238 204 246 246 207 334 334 210 335 334 213 334 238 216 334 246 219 334 334 237 238 238 225 246 246 228 332 238 231 332 246 234 340 343 237 0 0 240 337 343 267 334 334 246 0 334 249 335 335 252 334 335 267 339 102 258 339 105 261 339 108 264 334 334 90 334 334 270 333 334 327 334 334 276 335 335 279 341 334 282 334 335 285 333 335 288 342 342 291 334 334 294 335 342 297 102 102 300 105 105 303 108 108 306 342 102 309 342 105 312 342 108 315 337 105 318 337 108 321 337 108 324 334 334 90 334 334 -1 0 0 0 0 0 0 0 -1 -2 -3 1 0 0 0 33 35 10 65535 -65535
download subleq.oi
download subleq.oasm
You can try this SUBLEQ interpreter by concatenating the interpreter and another program and pipe the resulting file to the OI interpreter, like that:
    $ cat subleq.oi hello.oi | ./oi
This SUBLEQ interpreter (writen in SUBLEQ) can also interpret itself:
    $ cat subleq.oi subleq.oi subleq.oi hello.oi | ./oi
In that case the OI interpreter run the first SUBLEQ interpreter that run the seconds that run the thirds that run hello.oi. I know... This is fucking NERD.

OI Assembler

To test my SUBLEQ machine i have writen a simple assembler that make me easy to write my tests. OI Assembler is very simple. These are all the assembler features:

Third argument omission
The third argument of the SUBLEQ instruction can be omitted using a dot (.). In this case the dot will be replaced with the address of the next instruction. Example: 0 1 . 2 4 . 0 4 -1 Comments
Comments starts with a ( and ends with ). For example: (hello this is a comment) 0 1 . 4 5 . (another comment inline) 6 7 18 (more comments on multiple lines another line and another line) Labels
Symbolic labels can be defined using a starting dot character (.). Labels can be referenced with its name without the starting dot. For example: (example of labels) 2 3 12 0 2 . 0 1 JUMPLABEL (execute instruction and jump to JUMPLABEL) 0 0 . 2 2 . .JUMPLABEL (label is here!) 2 -1 -1 Labels can be also used in the first two arguments, to declare variables or reference unaligned memory. For example: (example of variables using labels) A B . (A and B are labels) B C . C -1 -1 (program ended) (variables) .A 10 (A is here!) .B 20 (B is here!) .C 0 (C is here!) That's all.
You can download the OI Assembler binary here:
windows x86
mac x86_64
Or download sourcecode:
oiasm source
The binary file read the assembler source from the standard input and generate the SUBLEQ output to the standard output.

Warning

As you can see SUBLEQ is an ugly, evil, bad, exoteric and exotic language. If you write your programs using SUBLEQ you become a super NERD DORK. Its a fact.

Contacts

Sandro Maffiodo
www.assezeta.com/sandromaffiodo
smaffer@gmail.com