Register machine interpreter written in C.
Register machines and packed integer form are implemented as described here.
Can be compiled with make
. You might have to change the compiler in Makefile
.
interpreter
reads a program from stdin, and takes initial register values as arguments.
For example, cat add.r | ./interpreter 0 7 8
, will run a register machine that computes R0=R1+R2=7+8.
packer [-p|-u]
packs or unpacks a program from stdin and outputs the result. -p
converts from a normal program to a packed integer. -u
converts from a packed integer into a program.
For example, cat add.r | ./packer -p
will convert the add program into an integer representation of it.
cat add.r | ./packer -p | ./packer -u | ./interpreter 0 81 129
will convert add to an integer and back again, then run the program.
The syntax of register programs is from here. Thank you 🙂
Each line of a file must be in one of the following formats:
HALT
will stop the programINC REGISTER NEXT_LABEL
will incrementREGISTER
and jump toNEXT_LABEL
DEC REGISTER LABEL_1 LABEL_2
will decrementREGISTER
and jump toLABEL_1
ifREGISTER
is greater than zero, otherwise it will jump toLABEL_2
REGISTER
, NEXT_LABEL
, LABEL_1
, and LABEL_2
must be nonnegative integers.
The first line of the file is L0 in the register machine, the second line is L1, and so on.