SUBLEQ SIC/OISC
This is a simple interpreter for a single-insutruction computer.
The single instruction is SUBLEQ (subtract and branch if less-than-or-equal-to 0)
Example: subleq a b c
- Subtract value in memory address a from value in memory address b
- store result in memory address b
- if the value is less than or equal to 0, jump to c
- Programs are made of groups of 3 values (a, b, c)
- Code and data share the same memory
- A jump to -1 terminates
- A jump to -2 just moves forward to the next group of 3 (essentially means "no jump", just continue)
- Writing to address -1 outputs an ascii value
Example:
If we have a piece of subleq code: 9 10 3 11 9 0 12 12 0 14 100 -1 0
It would be executed like this:
9 10 3, 11 9 0, 12 12 0, 14 100 -1, 0
execute at 0
9 10 3, 11 9 0, 12 12 0, 14 86 -1, 0
execute at 3 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 15 86 -1, 0
execute at 6 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 15 86 -1, 0
execute at 0 (jumped!)
9 10 3, 11 9 0, 12 12 0, 15 71 -1, 0
execute at 3 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 16 71 -1, 0
execute at 6 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 16 71 -1, 0
execute at 0 (jumped!)
9 10 3, 11 9 0, 12 12 0, 16 55 -1, 0
...and so on
Note that any instruction that doesn't jump is automatically followed by a jump forward by 3 to get to the next set of three values.