Virtual Machines - Nand2Tetris

Virtual Machines - Nand2Tetris

The Universal Translators.

Note that, as per Wikipedia, we are now talking about Process Virtual Machines and not System Virtual Machines. Although, the idea is similar.

What they actually mean.

Keeping the terminology aside, let's think about a piece of software we use every day, the browser. We give a link to the browser, or in other senses, we tell the browser the location of a piece of code it can fetch using the internet. Once the browser fetches the code, it starts to understand (parse) and execute the instructions in the code.

On whichever device we type "amazon.com" on, the device browser fetches the same code. Maybe you are opening it on a mobile phone that runs Android and has an ARM architecture that has a RISC instruction set or you are running it on a Windows PC with an AMD/Intel processor that is executing the x86_64 instruction set, but it doesn't matter.

We have a special application here that's taking care of it all for us. The mighty browser. In a sense, a browser is a Virtual Machine. It is providing our code with a platform to run on that is entirely virtual and is independent of the underlying architecture or jargon.

This is how Modern Languages like Java, Dart, Kotlin or Python work on different machines and devices.

Everyone who took a Java course or wrote some code in Java is aware that Java first compiles to a middle language i.e. Java Byte Code which is then compiled in runtime to Machine Code (or maybe the Assembly language in this case). This is called "Just In Time" (JIT) compilation. This JIT compilation is performed by a Virtual Machine (Java Virtual Machine). This JVM is akin to the browser.

The Java Virtual Machine (JVM) and the Python Virtual Machine are stack machines. What this means is that it stores data and performs operations on these data using a stack.

Android Virtual Machine

Android apps can be written in Java, meaning the syntax is there but Android apps cannot run on the JVM because they are not compiled to Java byte code.

Android apps run on the Dalvik Virtual Machine(DVM). At least they used to (Now, it moved to Android Run Time - ART) but the soul is there. It is a less resource-intensive Virtual Machine to run Dalvik Bytecode.

But there's another catch. The JVM is a Stack Machine but the DVM is a Register Machine. Meaning it works by manipulating the registers.

A JVM code to add two numbers 3 and 4 might look like this:

push 3
push 4
add

The same may be achieved in a Register Machine this way:

LOAD 3, R1
LOAD 4, R2 
ADD R1, R2, R3

We deal directly with the memory in a Register Machine.

So that's Process Virtual Machines and how computers understand stuff while we continue to write code in High-Level Languages. It's a boon that we are able to.