A dumb virtual machine

DumbVM in action
DumbVM in action, running a 21-line programme

Last year, my course had a module called “Computer Hardware and Operating System”. For someone like me who loves understanding how things work, it was a blast: we went from the concept of basic logic gates to building a simplistic working processor. The fact that our lecturer was passionate about his subject probably helped a lot as well.

Since I have been slacking off my game-a-month project, I though it could be interesting to try and implement what I learnt in that class to build a very simple virtual machine in C that could read, assemble and run very simple programmes.

It was easier than I first expected it to be, and in two days I managed to get at least an assembler and a processor working. The whole virtual machine is quite simple: it has a cpu, four 8-bit registers used for storage and calculation, one output register, and should eventually have an input register, storing whichever key was last pressed. It doesn’t even have RAM for the moment, except for the one in which the program is loaded when the machine is booted.

The instruction set is very simple as well: two registers can be added (add), subtracted (sub), multiplied (mul) or divided (div). Unconditional and conditional jumps are very basic.

Programmes are written in a custom assembly language, and the assembler converts them to machine code before loading them in the programme memory. Machine instructions are represented as four-byte words.

The whole “documentation” for the language can be found on GitHub, along with the (probably awful) source code for the machine and the assembler. Feel free to fork it or send me pull request if you think I’ve done something horrible or simply want to make it better.