By definition, as soon as it computes, it implements some model of computation. The question then is:
By Christian Stigen Larsen Posted 28 Jan — updated 23 Jan I was asked if I could write a simple blog post on how to make a process virtual machine — specifically, a stack machine. Using Python, I'm going to convince you how easy it is!
Thanks to robin-gvx, bs4h and Dagur! The code in this post is now available on GitHub. A stack machine doesn't have registers. Instead, it puts values on a stack and operate on that.
Stack machines are extremely simple, but very powerful.
Anyway, let's talk a little bit more about the stacks. We need an instruction pointer stack, which we'll use to store return addresses. This is so that we can call a subroutine a function, for instance and then jump back from where we came.
We could have used self-modifying code instead, like Donald Knuth's original MIX did, but then you'd have to manage a stack yourself if you wanted recursion to work. In this post, I won't really implement subroutine calls, but they're trivial anyway consider it an exercise.
With the data stack you get a lot of stuff for free.
Next, push four on the stack, then pop two values and push their product. So, let's start writing a simple class for a stack. We'll just inherit from collections.
Next, let's make a class for the virtual machine itself. As noted, we need two stacks and also some memory for the code itself. We'll rely on Python's dynamic typing so we can put anything in the list. Only problem is that we really don't discern between strings and built-in functions.
The correct way would be to insert actual Python functions in the list. Perhaps I'll do that in a future update. We also need an instruction pointer, pointing to the next item to execute in the code. But first, let's just create the interpreter loop: Fetch the next instruction, increment the instruction pointer and then dispatch based on the opcode.
The dispatch function is a tad more longer: It used to be constructed in dispatch, but as EnTerr pointed out in the comments below, that is grossly inefficient.
By the way, those are Forth words, a brilliant language that you should check out. In fact, the code you'll see here is basically a simple Forth. It looks like this: If it can't find the operation in the dispatch map, it will first see if it's a number.Creating a Stack Machine with Haskell About the Project.
This is a small demonstration project showing how a simple byte code interpreting stack machine (virtual machine) can be built with Haskell. Writing a compiler; which VM? Ask Question.
up vote 8 down vote favorite. what pros and cons are there to stack-based vs register-based VMs? Performance and tool support would be important.
I'll be writing the compiler in Haskell, so a good interface with that is a plus. compiler-construction. share. This happens more often for virtual stack machines than for other styles of virtual machine.  Android's Dalvik virtual machine for Java uses a virtual-register bit instruction set instead of Java's usual 8-bit stack code, to minimize instruction count and opcode dispatch stalls.
In order to do this I wrote an even simpler VM that runs inside of useless called even-more-useless VM (emuvm). EmuVM is a very simple stack-based virtual machine . I was asked if I could write a simple blog post on how to make a (process) virtual machine — specifically, a stack machine.
Using Python, I'm going to convince you how easy it is! Update: Based on comments from r/Python, I've made some small code changes.
Thanks to robin-gvx, bs4h and Dagur! Stack based vs Register based Virtual Machine Architecture, and the Dalvik VM A virtual machine (VM) is a high level abstraction on top of the native operating system, that emulates a physical machine.