views:

157

answers:

3

I'm wondering how to create a minimal virtual machine that'll be modeled after the Intel 16 bit system. This would be my first actual C project, most of my code is 100 lines or less, but I have the core fundamentals down, read K&R, and understand how things ought to work, so this pretty much is a test of wits.

Could anyone guide me in as far as documentation, tools, tutorials, or plain old tips/pointers on how to go about this, thus far I understand that I require somewhere to store data, a CPU of sorts and some sort of mechanism that functions as an interrupt controller.

I'm doing this to learn: Systems internals, ASM internals and C - three facets of computing that I want to learn in a singular project.

Please be kind enough not to tell me to do something simpler - that would only be annoying. :)

Thanks for reading, and hopefully writing!

+1  A: 

Well, for starters I would pick up a reference book for assembly language for the processor you intend to virtualize, like 80286 or similar.

Lasse V. Karlsen
+3  A: 

Virtual machines fall into two categories: those that interpret the code instruction at a time and those that compile the code to native instructions (e.g. "JIT").

The interpretation category is usually built around an instruction execution loop, using a switch statement, computed gotos or function pointers to determine how to execute each instruction.

There is a fun platform that is worth studying for its simplicity and fun: Corewars.

Corewars is a programming challenge game where programs written in "Redcode" run on a MARS VM. There are many MARS VMs, typically written in C.

It has also inspired 8086-based versions, where programs written in 8086 assembler battle.

Will
+1  A: 

For a JIT, you might want to dynamically generate and execute x86 code.

Magnus Hoff