the tetrvm bytecode format
tetrvm bytecode instructions are 10 bytes long. the maximum value this can have is 7.
the first two bytes are reserved for opcodes, the rest is for input.
if an opcode does not take input but one is given, it is ignored.
- 0o00: push
- 0o01: pop
- 0o02: peek
- 0o03: dup
- 0o04: swap
- 0o05: jump
- 0o06: stop
- 0o07: put
- 0o10: puts
- 0o11: mul
- 0o12: div
- 0o13: neg
- 0o14: add
- 0o15: sub
- 0o16: jnz
- 0o17: eq
- 0o20: eqi
- 0o21: lab
- 0o22: get
- 0o23: set
- 0o24: read
- 0o25: jgz
all programs compiling to tet have to end with stop
(0o06).
pushes to the top of the stack
- -- a
pops the top of the stack, removing it
a -- -
gets the top value on the stack, not useful except internally
a -- a
duplicates the top of the stack
a -- a a
swaps the top two values on the stack
a b -- b a
jumps to the specified label. eg: jump 0o01
jumps to label 1.
does not modify the stack
stops the current execution
writes the top of the stack to the console
a -- -
writes the top of the stack's value in ascii to the console
a -- -
multiplies the top two values on the stack
a b -- (b * a)
divides the top two values on the stack
a b -- (b / a)
makes the top of the stack negative
note: does not act like a NOT
a -- (-a)
adds the top two values on the stack
a b -- (b + a)
subtracts the top two values on the stack
a b -- (b - a)
jumps if the top of the stack is true (value 1).
a -- -
checks if the top two values on the stack are equal
this pushes either 0 or 1 to the stack
a b -- (b == a)
eq with input, checks if the top value equals the input value
a -- (i == a)
introduces a label. the value/name should be a number.
does not modify the stack.
duplicates the top - n
th element of the stack to the top
warning: this instruction is unsafe. you can very easily crash tetrvm with it
a b c d e -- e a b c d e
set top - n
th element of the stack to value on top
warning: this instruction is unsafe. you can very easily crash tetrvm with it
a b c d e -- b c d a
reads a character from stdin and pushes its ascii value to the stack
- -- a
jumps if the top value on the stack is greater than 0.
a -- -
tesm is a simple assembly dialect. it is so simple in fact, that everything is an instruction, and each instruction is a keyword.
all instructions are listed above.
an example program that prints the result of 1 + 1 to the console, would be
push 1
push 1
add
put
stop
to use labels, use the lab
instruction, and jump to the label's value:
labels start from 0, and increment by 1 for each new label. the identifiers are written to the bytecode, but not used. they are purely for readability.
lab 0
push 1
push 1
add
put
jump 0
stop
tesm has comments. you can begin comments by typing a semicolon (;
)
push 1 ; pushes 1 to the stack
put ; pops and prints the top of the stack
stop ; stops execution