3.1 KiB
The Register Machine Model
The register machine has a (in)finite number of registers which can store a 32 bit, signed integer. Two registers play a special role: the accumulator, which is register number 0 by default, and the program counter, which defaults as register number 10.
The accumulator is a special register that most operations use. The program counter keeps track of which instruction we're actually executing.
This interpreter reads a single text file with one instruction per line. Whitespaces are ignored. Lines starting with # are treated as comments and are ignored.
The input is a queue (FILO), and so is the output.
Jump operations are zero-indexed. Meaning, JUMP 0 will jump to the first instruction, and JUMP 1 to the second.
Instructions (and syntax)
All instructions that take a register as argument can also take a indirect reference as argument:
This means that instred of writing READ n
to read register n
, if the number n
is stored in a register R we can write READ *R
. This will read the value n
of register R
, and perform READ n
.
READ <register_number | *register_ref>
Reads a single number from the input queue, and stores the said number in register number <register_number>
, or in the register referenced by the register number <register_ref>
WRITE <register_number | *register_ref>
Writes the number stored in register number <register_number>
or in the register referenced by the register number <register_ref>
in the output queue.
LOAD <register_number | *register_ref>
Writes the number stored in register number <register_number>
or in the register referenced by the register number <register_ref>
in the accumulator (id est, register 0).
LOAD =<constant>
(load immediate)
Writes <constant>
directly in the accumulator.
STORE <register_number | *register_ref>
Writes the number currently in the accumulator in the register number <register_number>
or in the register referenced by the register number <register_ref>
.
ADD <register_number | *register_ref>
Adds to the accumulator the number stored in register number <register_number>
or or in the register referenced by the register number <register_ref>
.
.
ADD =<constant>
(add immediate)
Adds <constant>
directly to the accumulator.
SUB <register_number | *register_ref>
Subtracts from the accumulator the number stored in register number <register_number>
or in the register referenced by the register number <register_ref>
.
.
SUB =<constant>
(sub immediate)
Subtracts <constant>
directly from the accumulator.
JUMP <instruction_number>
Sets the program counter to instruction number <instruction_number>
, thus jumping immediately to that instruction.
JZERO <instruction_number>
Sets the program counter to instruction number <instruction_number>
if and only if the accumulator has value 0.
JGTZ <instruction_number>
Sets the program counter to instruction number <instruction_number>
if and only if the accumulator has value greater than 0
HALT
Stops the program.