A simple virtual machine that can interpret programs written in a basic assembly language
AbstractVm is able to run programs from a file passed as parameter and from the standard input. When reading from the standard input, the end of the program is indicated by the special symbol ;;
otherwise absent.
- Comment start with a
;
abd finish with a newline. Acomment can ve either at th start of a line, or after an instruction push
+ v : Pushes the value v at the top of the stackpop
: Unstacks the value from the top of the stackdump
: Displays eaach values of the stackassert
+ v : Asserts that the value at the top of the stack is equal to the one passedadd
: Unstacks the first two values on the stack, adds them together and stacks the resultsub
: Unstacks the first two values on the stack, substracts them and stacks the resultmul
: Unstacks the first two values on the stack, multiplies them and stacks the resultdiv
: Unstacks the first two values on the stack, divides them and stacks the resultmod
: Unstacks the first two values on the stack, calculates the modulus and stacks the resultprint
: Assert that the value at the top of the stack is an 8-bit integer, then interprets it as an ASCII value and displays the corresponding character on the standard outputexit
: Terminate the execution of the current rogram.
sin
: Unstacks the first value on the stack, calculates the sine in radian and stacks the resultcos
: Unstacks the first value on the stack, calculates the cosine in radian and stacks the resulttan
: Unstacks the first value on the stack, calculates the tangent in radian and stacks the resultpow
: Unstacks the first two values on the stack, calculates the power and stacks the resultsqrt
: Unstacks the first value on the stack, calculates the square and stacks the result
The assembly language of AbstractVM is generated from the following grammar (# corresponds to the end of the input):
S := INSTR [SEP INSTR]* #
INSTR :=
push VALUE
| pop
| dump
| assert VALUE
| add
| sub
| mul
| div
| mod
| print
| exit
VALUE :=
int8(N)
| int16(N)
| int32(N)
| float(Z)
| double(Z)
N := [-]?[0..9]+
Z := [-]?[0..9]+.[0..9]+
SEP := \n +
You can find some tests files in the tests
folder.