Subleq
A computer with one instruction: subtract, and jump if the result is zero or less. No opcodes, just numbers in threes.
Farhad Mavaddat and Behrooz Parhami, 1988 · Turing complete: with unbounded memory · ●●○ moderate · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
A real CPU has hundreds of instructions. How many does it need? One is enough, if it is the right one. Machines like this are called one-instruction set computers (OISC).
Read the story
Subleq ("subtract and branch if less than or equal to zero") is the best known. A nearly identical machine appears in a 1988 paper by Farhad Mavaddat and Behrooz Parhami, "URISC: the ultimate reduced instruction set computer". Since there is only one instruction, it needs no opcode: a program is just a list of integers, read three at a time.
Subleq has become the hobbyist's OISC. There are assemblers, compilers from C-like languages, and even FPGA hardware that runs it.
The machine
Memory is one array of integers. Code and data live in the same array. An instruction pointer starts at 0.
a b c: mem[b] = mem[b] - mem[a]
if mem[b] <= 0, jump to c, else go on to the next three numbers
Everything else is a trick built from this:
| Want | Subleq |
|---|---|
| Unconditional jump to c | Z Z c, where Z is a cell holding 0 (0 − 0 = 0, always jumps) |
| Clear cell x | x x next |
| Add a to b | a Z, Z b, Z Z: b − (0 − a) = b + a, then reset Z |
| Halt | jump to a negative address |
Input and output are not in the core instruction. A common convention, used by this implementation, is that b = -1 prints mem[a] as a character and a = -1 reads a character into mem[b].
Example
Print A three times.
Print A three times.
addr 0: 12 -1 3 print mem[12] ('A')
addr 3: 14 13 9 counter -= 1; if counter <= 0, jump to 9
addr 6: 15 15 0 Z -= Z: always 0, so jump back to 0
addr 9: 15 15 -1 same trick, jumping to -1: halt
addr 12: 65 3 1 0 data: 'A', the counter, the constant 1, Z
The counter goes 3, 2, 1, 0. The first two times the result is positive, so the program falls through to the jump at 6. The third time it reaches 0 and branches to the halt.
Patterns it teaches
- One instruction: the textbook case. Subtraction gives arithmetic, the branch gives control, and together they give everything.
- Self-modifying code: Subleq has no indirect addressing. To read
mem[mem[p]](an array, a pointer) you must write the pointer into the operand of a later instruction, then run it. - Code as data: instructions are integers in the same memory as the numbers they work on. Nothing stops a program from subtracting from its own instructions, by design or by accident.
- Turing completeness: universal with unbounded memory and integers; real implementations use fixed-size words and are bounded.
Try this
- Change the example to print
CBA(subtract 1 from the letter each time round). - Write
cat: read a character, stop if it is 0, otherwise print it and loop. - Hard: copy a block of 5 cells to another place in memory using self-modification.
Further
- esolangs.org/wiki/Subleq
- Oleg Mazonka's Subleq pages and the "Higher Subleq" compiler.
- A bit-copying cousin: BitBitJump.
See also
OISC
Self-modifying
Write your own in Run Convert a program from Subleq
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: subleq.
Command line
shib run subleq -e '9 -1 3 10 -1 6 0 0 -1 72 105 0'shib manual subleqSource
- Module
langs/museum/ports.js- Interpreter
langs/museum/subleq.js,langs/common.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org