This project provides 3 features:
The interpreter works with:
The following structures are needed:
Each byte of the program is an instruction that has to be executed, an instruction is described by the following structure:
| 1 bit | 5 bits | 2 bits |
|---|---|---|
| has operand | opcode | size |
Instructions that takes parameters can have them on the stack
(has operand = 0) or in the bytes following the instruction
(has operand = 1). If the instruction produce a result, it
will always be pushed on the stack.
The opcode bits identifies the instruction.
The size bits identifies the size of the parameters/result:
| Value | 00 | 01 | 10 | 11 |
|---|---|---|---|---|
| Size | 8 bits | 16 bits | 32 bits | 64 bits |
NB: There are some instructions that are size independant, their
opcode is then 7 bits long.
PUSH8 42 NOT8 PUSH8 1 INT
The above code push 42 on the stack, then apply a binary not on this
value, and calls the interuption number 1 (interuptions are special
functions built in the interpreter), which prints the stack head.
It is translated in the following byte array:
PUSH8 (1 00000 00) |
42 | NOT8 (0 01001 00) |
PUSH8 (1 00000 00) |
1 | INT (0 1111110) |
NOT8 42 PUSH8 1 INT
The above code behave identically but we pass the parameters directly instead of via the stack.
It is translated in the following byte array:
NOT8 (1 01001 00) |
42 | INT (1 1111110) |
1 |
NB: Using the C headers stdlib.h, stdint.h and stdbool.h,
we have access to the following types:
| Type | Description | Bytes |
|---|---|---|
size_t | An (unsigned) integer type capable of storing a memory size (ie. with sizeof()) | Architecture dependent |
(u)int8_t | An (unsigned) integer type capable of storing 8 bits | 1 |
(u)int16_t | An (unsigned) integer type capable of storing 16 bits | 2 |
(u)int32_t | An (unsigned) integer type capable of storing 32 bits | 4 |
(u)int64_t | An (unsigned) integer type capable of storing 64 bits | 8 |
(u)intptr_t | An (unsigned) integer type capable of storing any pointer | Architecture dependent |
bool | boolean type with values false and true defined | 1 |
NB: Using the C headers stdlib.h and string.h we are able to use the
following functions:
0
For the complete list of instructions and what they do:
NB: When there is not enough space left on the stack, it must be reallocated with more space: