Skip to content
PixelCursor(opens in a new tab)
Computer Science

Register Machine Simulator

Program the classic register machine step by step

Example
  • Addition
  • Multiplication
  • Power
  • Factorial
  • Division with remainder
  • Maximum
  • Fibonacci
  • Array sum (indirect)
Speed
  • Slow
  • Normal
  • Fast
  • Turbo
Run

Multiplication

c(0) = c(1) · c(2) by repeated addition

// Input: c(1), c(2) - Result: c(0)
// Only ADD and SUB - no MULT allowed!
1
CLOAD 0
2
STORE 3 // result := 0
3
LOAD 2 // loop: while c(2) > 0
4
IF c(0) = 0 GOTO 11
5
CSUB 1
6
STORE 2 // c(2) := c(2) - 1
7
LOAD 3
8
ADD 1
9
STORE 3 // result := result + c(1)
10
GOTO 3
11
LOAD 3
12
END
Counter b1Steps0Ready
Next instructionCLOAD 0

Registers

Set the input values, then run
c(0)
c(1)
c(2)
c(3)
c(4)
c(5)
c(6)
c(7)
c(8)
c(9)

Last steps

Nothing executed yet. Press Step or Run.

Halted0
Steps0in total
Correct0

Run a program to the end; finished runs are counted here.

Instruction set

One instruction per line, numbered from 1. // starts a comment. Registers hold natural numbers, so subtraction never drops below 0.

LOAD i
c(0) := c(i)
CLOAD i
c(0) := i
INDLOAD i
c(0) := c(c(i))
STORE i
c(i) := c(0)
INDSTORE i
c(c(i)) := c(0)
ADD i
c(0) := c(0) + c(i)
CADD i
c(0) := c(0) + i
INDADD i
c(0) := c(0) + c(c(i))
SUB i
c(0) := max(0, c(0) − c(i))
CSUB i
c(0) := max(0, c(0) − i)
INDSUB i
c(0) := max(0, c(0) − c(c(i)))
MULT i
c(0) := c(0) · c(i)
CMULT i
c(0) := c(0) · i
INDMULT i
c(0) := c(0) · c(c(i))
DIV i
c(0) := ⌊c(0) / c(i)⌋
CDIV i
c(0) := ⌊c(0) / i⌋
INDDIV i
c(0) := ⌊c(0) / c(c(i))⌋
GOTO j
b := j
IF c(0) ? x GOTO j
Jump to j if the comparison holds, else go on with b + 1. Comparisons: =, <, <=, >, >=
END
Halt

About this tool

  • Accumulator c(0)

    Every calculation runs through register c(0). LOAD fetches a value, STORE writes it back to any register c(i).

  • Three ways to address

    ADD 3 adds register c(3), CADD 3 adds the constant 3, INDADD 3 adds the register whose number is stored in c(3).

  • Turing-complete

    With just these few instructions a register machine can compute everything a Turing machine can, and it is much closer to a real CPU.

Keyboard

Space
Run or pause
←→
Step back or forward
E
Run to the end
R
Reset the machine