Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
subleq/README.md
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (34 sloc)
1.38 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |