Take the 2-minute tour ×

I'm trying to learn about how compilers, operating systems, and computer systems work internally. Here's my current plan:

  1. Write a simple virtual machine in C, that can accept input written in its machine language ( a custom language of my specification. )
  2. Write a an assembler for that virtual machine.
  3. Write a very basic compiler for a basic-like language for the machine.
  4. Extend the interactivity of the virtual machine by including a screen (I think I'll use SDL), and an interface to that screen via video memory. I could also add mouse/keyboard functionality.

Any tips or recommendations would be nice. Thank you in advance!

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

My first advice is to read a high level book on the subject. That is, I am assuming you haven't done this yet, and were planning on simply working along with some online tutorials or something. At least for me, I tend to want to dive head first into things like this, but then I quickly feel in over my head and then just give up the project. Making sure I have a really good high level understanding of projects before starting helps me tremendously.

One series I might recommend are the Write Great Code books. I can't vouch for the whole series as I haven't read them all, but my office has them at work and I have used them a number of times to get a pretty good grasp on the subject before I dove headfirst into something. For instance, and one example that may relate directly to your plan, I was needing to understand how the GCC compiler organized the ELF binary it generates, what each section is, and what is stored there. (This was for an embedded system and we were expanding our RAM so I had to reorganize some stuff...)

You said "Nothing too hard"... In my opinion, I think your steps are already pretty difficult, especially if your end goal is to learn about compilers and operating systems. I would skip this whole virtual machine, at least for now. In reality, processors are pretty simple, and based on the fact that that you already know that it just processes a 'machine language' you probably already have a good starting grasp.

I would instead start with step 3 and just writing your own compiler. I took a compilers class in college and by the end of the semester, I had a working Pascal compiler that I built from the ground up using LEX and YACC. It was quite enlightening. You might also look at Bison as its used in conjunction with yacc for such things. I've never used it though.

Also, simply doing little exercises in your free time, like figuring out how to manipulate GCC to compile hello world into less than X number of bytes will teach you a lot more than you think about how that stuff works. (There are quite a few examples of this on the web, by the way)

Have Fun!

share|improve this answer
Thanks. I've pretty much done the lexing / parsing .. my challenge now is to build the AST and do the code generation (I didn't really want to learn x86 assembly so I decided to try to write my own VM with my own custom instructions that I would be familiar with.) –  ouiliame Aug 19 '11 at 3:07
You're farther than I had realized then! Yeah, I remember the code generation being the most difficult, for the obvious reason than I knew very little x86 assembly, but luckily you only really need to know a subset of the instructions. –  Lncn Aug 19 '11 at 3:16
add comment

How much programming do you already know?

Writing a FORTH interpreter is a good exercise. It's comparatively simple, and the language and semantics are already well-defined, so you don't need to go and design your own system from scratch. FORTH typically also has a compiler (although it is nothing like a C compiler) and may have an assembler built in, so you could investigate those also. It will give you the mental tools for managing memory, handling pointers, resolving references, and such.

Looking at an existing simple compiler will be helpful also. Once you have internalized what it is that a compiler does - translate one set of symbols into another - then you might want to start looking at parsing grammars and related topics. There's an awful lot of information out there, take it a little at a time or you'll get overwhelmed very easily.

share|improve this answer
add comment

That's a great list of goals. Some folks go to college for 4 years to learn that. I have no idea of your background, but I might assume you've done some basic programming (BASIC programming?) and assembly language. If you haven't, that's a place to start. Learning something about grammars and regular expressions and then using that to develop a parser and interpreter for parts of a simple language with an easy grammar, like Pascal, would be a way to learn the front-end. Then move on and add code that generates assembly...the back end.

share|improve this answer
add comment

Your Answer


