SimpleLang is a subset of assembler languages implemented in virtual machine
In SimpleLang we have only 4 General-Purpose registers r1, r2, r3, r4
.
One accumulator register A
and 4 conditional registers EQ
, GT
, LT
, NE
In SimpleLang only that operations is allowed and implemented:
MOV (r|a), (r|a)
- store in first operand value from second operandMOV (r|a), @r
- store in first operand value by address in second operandADD A, r
- add value from second operand to accumulatorDIV A, r
- divide value from accumulator by value in second operand (Note: Only integer division is allowe and implemented)SUB A, r
- substract accumulator by value of second operandMUL A, r
- multiply accumulator by value of second operandLABEL lbl
- make that lbl points to that line of codeJMP lbl
- jump to line that lbl pointsCMP A, r
- compare value of accumulator with second operand and set specific registersJMP_EQ, JMP_LT, JMP_GT, JMP_NE
- conditional jumpsAND, OR, XOR, NOT
- bit operationsPRINT (@|)(A|r|num)
- print value of register or memory by reg.pointer to stdoutINPUT (@|)(A|r)
- read ONE NUMBER from stdin and write to register or to memory pointCALL lbl
- call subroutine under label lblRET
- return from subroutine (Only one subroutine can be called at the monent)
Operation bytecode:
| 2 byte | 1 byte | 4 byte | 1 byte | 4 byte |
| op_code | arg_ty | op_arg | arg_ty | op_arg |
one operation will be encoded to (2+1+4+1+4) = 12 byte
if operation doesn't have any of arguments pad_sym will be used and arg_type will be set as pad_symbol!
1 byte before every argument is placeholder for argument type (e.g. reference, register or in-place value)
4 byte arguments size needed for in-place values In-place values is a 32-bit integers only!
Every bytecode file have one metadata section before real code
| 2 byte | 4 byte |
|magical number| crc |
CRC sum is used for code invalidation.
Calculate N-th fibonacci number
LABEL MAIN
INPUT r1
CALL FIBONACCI
PRINT r2
END
LABEL FIBONACCI
; Prepare fibonacci
MOV r2, 0
MOV r3, 1
LABEL FIBONACCI_LOOP
; Calculate n fibonacci number
; Store result at r2
MOV A, r2
ADD A, r3
MOV r2, r3
MOV r3, A
SUB r1, 1
; Loop while r1 != 0
CMP r1, 0
JMP_GT FIBONACCI_LOOP
RET