Creating a Virtual Machine/Introduction
This book is a tutorial on creating several different types of virtual machines. The intent is to show the basics of virtual machine design and implementation. The authors will do their best to keep the program code simple and easy to read and understand.
What this book is not about:
- Creating an emulator of the quality exhibited by MAME, MESS, or VICE.
- Creating a virtualization environment like QEMU, VirtualBox, or VMware.
This book assumes that you have some programming experience and understand the basics of binary numbers and hexadecimal notation. Familiarity with machine code or assembly language programming for one CPU or other would also be beneficial, but is not strictly necessary. In fact, I have found that studying a virtual machine to be an effective means of learning an assembly language.
In order to compile and run the examples you will need to have certain software installed on your computer.
For the Register VM in C you will need to have a basic C compiler (a C++ compiler will also work fine).
- Windows users: I recommend Quincy. It is free and easy to use.
- Linux users: Install GCC on your system if it's not there already. Code::Blocks also works fine.
- Debian (& derivative) users open a command shell, become root, and type apt-get install gcc.
- Mac users: Install the Xcode package from the extra disk that came with your computer or via the AppStore, for free.
For the Register VM in Swift you will need to have a Swift interpreter.
- Mac users: Install Xcode (free). The easiest way to use the interpreter is to paste the code into a new "playground."
- Other platforms: In June, 2015 Apple announced it would "making Swift open source later this year." Since none of Foundation, Cocoa or UIKit are used by Register VM in Swift, it should work in any compliant port of the language.
For the Stack-Register VM in Java you will need to have a fairly recent Java compiler.
- Windows users: Download and install the Java SE JDK from Sun's Java download page.
- Linux users: You may use OpenJDK, but I recommend downloading and installing the Java SE JDK from Sun's Java download page.
- Mac users: You should have Java installed already, or download it from java.com.
For the Register VM in Erlang you will need to install Erlang.
- Windows users:
- Linux users:
- Mac users: (1) install MacPorts, (2) install Erlang
Terminology and Definitions
Run and execute
When a computer follows a program's instructions it is said to be running the program. Another term for run in this context is execute. This use of the word execute means to carry out, to make happen, or to put into effect. Computer programmers also talk about killing or terminating a program, but in this context the word execute is never used.
We count from 0
If you have done any Java, C, or C++ programming you will have noticed that when a program does any sort of counting, it usually starts counting from 0. We will do the same in these program examples, not by convention, but by necessity. Some high level languages encourage (or require) counting from 0 because they are based on machine languages which require counting from 0. The reason for this has to do with register indexing, something that should become clear to you as you follow through this book.
Words are numbers
When writing programs at a low level such as this, programmers often speak of words. A word in this context is a number, but one that lacks both cardinality and ordinality in the normal numeric sense. Instead a numeric word contains lexicographic information. Words are often written in hexadecimal notation.
Before we proceed I thought it would be useful to agree on some terms that tend to be confused or misused in the field. This is not so much to impose my will upon you as it is to establish a context.
- abstract machine
- the definition of some machine, usually just 'on paper'
- virtual machine
- a software implementation of an abstract machine
- a software implementation of a real machine
(Note to self: I think this should probably be moved to a glossary and expanded.)