Skip to content
Arno van der Vegt edited this page Oct 6, 2020 · 30 revisions

Commands

The virtual machine has 16 commands. Each command can have up to two parameters.

# Command Description
0 call Call a procedure
1 ret Return from a procedure
2 copy Copy from the source register to the destination register.
3 jmpc Jump conditional, based on the given flag value.
4 mod Call a module.
5 set Set a value.
6 add Add a value.
7 sub Subtract a value.
8 mul Multiply a value.
9 div Divide a value.
10 and Logic and a value.
11 or Logic or a value.
12 cmp Compare two values.
13 setf Set a flag.
14 sets Set a string.
15 adds Add a string.

Command parameters

There are four parameter types:

Type Value
Constant 0
Global 1
Local 2
Pointer 3

The combination of a command and two parameters can be stored in eight bits:

Bits: 8..5 4..3 2..1
Function Command First param type Second param type

call command

The call command has two parameters. The first parameter is the offset in the code, the second is the current stack usage. When a call is made then the value of the stack register and the code register values are saved on the stack. The next example calls a procedure at command 45 and adds 5 to the stack register:

call 56, 5

ret command

The ret command has a single parameter, the value of the parameter is stored in the ret register.

Some module calls, for example the round function in the math module set the return register. Modules can set a keepRet value which has the effect that the parameter of the ret command is ignored after that module call is made. This example sets the return register to 45:

ret 45

copy command

The copy command has two parameters but the first one is ignored. The second parameter is the number of values which have to be copies. The copy command copies from the address set in the src register to the address set in the dest register. The following code copies 10 values:

copy 0, 10

jmpc command

The jmpc command jumps if a given flag condition is met. It has two parameters, the first parameter is the flags which have to be set and the next parameter is the address to jump to. In the following code a jump is done if the equal flag is set:

jmpc 1, 40

Registers

Registers are memory locations with a specific purpose. Wheel uses 9 registers which are the first 9 positions in the VM memory:

Register Offset Function
stack 0 The stack pointer, used to address local variables.
src 1 The source register used to pass data to modules and to copy data.
dest 2 The destination register, used to copy data.
ptr 3 The pointer register, used to address variables.
code 4 The code offset.
return 5 The return value.
flags 6 The flags to store the result of a comparison.
range1 7 The first range check register.
range2 8 The second range check register.

Flags

When two values are compared then the first six bits of the flags register will be given a value based on the following conditions:

Flag Bit # Value Description
eq 1 1 Equal
neq 2 2 Not equal
l 3 4 Less
le 4 8 Less or equal
g 5 16 Greater
ge 6 32 Greater or equal