esolang
Learn · Ideas29 languages show it

One instruction

A computer with a single instruction. Since there is nothing to choose, the opcode disappears and a program is only numbers.

●●○ moderate · where to see it

The idea

A one-instruction set computer (OISC) has exactly one instruction, and it is still universal. The trick is to pick an instruction that does two jobs at once; it changes memory and it decides where to go next.

Read the story

If every instruction is the same, it need not be written down. A program becomes a list of operands. Code and data are both numbers in the same memory, so a program can rewrite itself (see code as data).

The idea is old and comes from computer architecture, not from esolangs. Mavaddat and Parhami described one such machine, which they called URISC, in 1988. Esolangs made it a hobby.

How it works

The best-known instruction is SUBLEQ, subtract, and branch if less than or equal to zero.

The best-known instruction is SUBLEQ, subtract, and branch if less than or equal to zero.

SUBLEQ a b c:   mem[b] = mem[b] - mem[a]
                if mem[b] <= 0, jump to c; otherwise go to the next instruction

Every three numbers form one instruction. By common convention, jumping to a negative address halts.

Adding X into Y

You can't add directly, only subtract. So subtract twice, Z = 0 - X, then Y = Y - Z.

addr  numbers      meaning
 0    9 11  3      Z -= X     (Z becomes -7; ≤ 0, so jump to 3: the next one anyway)
 3   11 10  6      Y -= Z     (Y becomes 5 + 7 = 12; > 0, go on)
 6   11 11 -1      Z -= Z     (Z becomes 0; ≤ 0, so jump to -1: halt)
 9    7            X
10    5            Y
11    0            Z, a scratch cell that must start at 0

The whole program is 9 11 3 11 10 6 11 11 -1 7 5 0. Its trace.

step 1  pc 0  mem[11] = -7
step 2  pc 3  mem[10] = 12
step 3  pc 6  mem[11] = 0      halt; X, Y, Z = 7, 12, 0

In steps 1 and 3 the third operand points just past the instruction, so the "branch" is "go on". Writing Z Z c is SUBLEQ's unconditional jump; Z - Z is always 0.

From these pieces you build MOV, JMP, and everything else, the same way a Turing tarpit builds everything from a loop.

Other single instructions

  • BitBitJump copies one bit to another address, then jumps. It works on bits and addresses only.
  • SUBNEG, RSSB (reverse subtract and skip if borrow). Close relatives of SUBLEQ.
  • FRACTRAN is arguably one; "multiply by the first fraction that gives a whole number" is the only thing it ever does.

Seen in

  • Subleq. The reference OISC; people have written compilers and even small operating systems for it.
  • BitBitJump. An OISC on bits, where the jump also comes from memory.
  • FRACTRAN. One rule, applied to a number, over and over.

Try this

  1. Write SUBLEQ code that sets a cell to 0 using one instruction.
  2. Write MOV a, b (copy a into b) in SUBLEQ. You will need the scratch cell Z.
  3. Hard: take the add program above and change one number at random. Run it by hand. How often does it still halt? How often does it still add?

Further

Where to see it