Virtual machine emulating a zero operand CPU

by L Schmidt

Version 14 (July 25, 2021)

Download (187 downloads)

Here's a threaded code, stack based virtual machine, emulating a zero operand Forth like processor. It currently allows execution of low- and hilevel code, subroutine nesting, hi level code creation, basic arithmetic and comparisons, bitwise and boolean operations, stack manipulation, flow control, basic memory access, rudimentary I/O. To be useful, a compiler, generating code for this VM, is needed at this point, but the VM is meant to eventually host its own compiler and interpreter.
The compiler, along with a bunch of test- and demonstration programs is available on github, at -
VM, compiler and test programs need to match: the VM checks uploaded code and refuses to run versions created with an incompatible compiler.
The compiler requires bash, as it is written as shell script.
*.prog files are source files, human readable (perhaps) and serving as input to the compiler. *.code files are compiler output. The VM can load and execute those.
Currently there's a hard-coded path Automate/code in the VM where it will expect program files to load from (on Android device), and write the resulting execution trace files to.

I'm currently working on interfacing the VM with standard in- and output, a precondition for moving the compiler and interpreter to the VM and allow interactive compilation and command execution from a console/terminal. I rather work on that before tackling the compiler itself because of the benefits such interactively offers for testing and debugging.

If you only want to try out the precompiled test programs, create the Automate/code directory on your Android device, copy the *.code files into it, and launch the "Virtual Machine" flow, which will open a file selector to choose a program.
For editing and compiling .prog files, you need to change the "base" variable in the compiler (that's the script "compile") to the directory where you copied all the .prog files to. Invoke it with ./compile name or ./compile name.prog, copy the generated .code file to mobile device. A flow entry point to watch a pointer file named "program" for automatic launch exists.

Here's a screenshot of the VM in action:
On the left are code of a test program (just testing flow control construct), below is the VM generated trace output. On the right the result of the last compiler directive "words", showing part of the vocabulary the VM is currently able to execute (and the compiler generating code for it).

Replies to comments:

4.8 average rating from 4 reviews

5 stars
4 stars
3 stars
2 stars
1 star

Rate and review within the app in the Community section.