-
Notifications
You must be signed in to change notification settings - Fork 1
The Wheel virtual machine only uses a number type. All strings are numeric indices which point to an offset in a string list.
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 |
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 |
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
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
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
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
The mod command calls a procedure in a module. This command has two parameters, the first parameter is the module and the second parameter is the procedure within the module. Data is passed to a module by pointing the src register to a block of data which you want to pass to the module. The following example code calls the fifth procedure of the third module:
mod 3, 5
The set command sets a value in a memory location. It has two parameters. The first parameter can be a global memory location, a location on the stack or a pointer location. The second parameter can be a constant, global memory location, stack or pointer location. The following example shows set commands with different parameters:
set [3], 5 ; Set a global memory location with offset 3 to 5
set [stack + 10], 7 ; Set a stack location to 7
set [ptr + 6], 2 ; Set a pointer location to 2
set [3], [5] ; Copy the value of global memory location 5 to global location 3
set [stack + 10], [ptr + 4] ; Copy the pointer value to the stack location
set [ptr + 6], [stack + 4] ; Copy the stack location to the pointer location
The add command has two parameters and adds the value of the second parameter to the value at the memory location of the first paremater. The first parameter can be a global memory location, a location on the stack or a pointer location. The second parameter can be a constant, global memory location, stack or pointer location. The following example shows add commands with different parameters:
add [3], 5 ; Adds 5 to the global memory location with offset 3
add [stack + 10], 7 ; Adds 7 to the stack location
add [ptr + 6], 2 ; Adds 2 to a pointer location
add [3], [5] ; Add the value of the global memory location 5 to the global location 3
add [stack + 10], [ptr + 4] ; Add the pointer value to the stack location
add [ptr + 6], [stack + 4] ; Add the stack location to the pointer location
The sub command has two parameters and subtracts the value of the second parameter from the value at the memory location of the first parameter. The first parameter can be a global memory location, a location on the stack or a pointer location. The second parameter can be a constant, global memory location, stack or pointer location. The following example shows sub commands with different parameters:
sub [3], 5 ; Subtracts 5 from the global memory location with offset 3
sub [stack + 10], 7 ; Subtracts 7 from the stack location
sub [ptr + 6], 2 ; Subtracts 2 from a pointer location
sub [3], [5] ; Subtract the value of the global memory location 5 from the global location 3
sub [stack + 10], [ptr + 4] ; Subtract the pointer value from the stack location
sub [ptr + 6], [stack + 4] ; Subtract the stack location from the pointer location
The mul command has two parameters and multiplies the value of the second parameter with the value at the memory location of the first parameter. The first parameter can be a global memory location, a location on the stack or a pointer location. The second parameter can be a constant, global memory location, stack or pointer location. The following example shows mul commands with different parameters:
mul [3], 5 ; Multiply the global memory location with offset 3 with 5
mul [stack + 10], 7 ; Multiply the stack location with 7
mul [ptr + 6], 2 ; Multiply the pointer location with 2
mul [3], [5] ; Multiply the global memory location 3 with the value at memory location 5
mul [stack + 10], [ptr + 4] ; Multiply the stack location with the value at the pointer location
mul [ptr + 6], [stack + 4] ; Multiply the pointer location with the value at the stack location
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 |
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 |