Live data from Hacker News

Write Your Own Virtual Machine

justinmeiners.github.io

11–20 of 44 posts

Re: Write Your Own Virtual Machine

#11
post #10
post #7

I was tricked into writing a VM while doing Advent of Code last year and it really deepened my understanding of programming (e.g., on how to implement simple coroutines). Advent of Code is supposed to be 25 daily programming puzzles leading up to christmas. I went in thinking they were going to be algo/datastructure tasks but half of them were actually about implementing and using a VM for a made up assembly language…

Funny that VM related days are all prime numbers :). EDIT: No they're not, I wasn't fully awake, sorry.

Not all of them, but a lot seem to be.

Re: Write Your Own Virtual Machine

#12
post #10

Earlier quoted context omitted.

Funny that VM related days are all prime numbers :). EDIT: No they're not, I wasn't fully awake, sorry.

Not all of them, but a lot seem to be.

Haha right, I saw the list without really watching it and jumped to the conclusion before actually paying attention to all the days. It was before my morning coffee, if that's any excuse.

Re: Write Your Own Virtual Machine

#13
post #10

Earlier quoted context omitted.

Funny that VM related days are all prime numbers :). EDIT: No they're not, I wasn't fully awake, sorry.

Not all of them, but a lot seem to be.

A mathematician, physicist, and engineer are taking a math test. One question asks "Are all odd numbers prime?"

The mathematician thinks, "3 is prime, 5 is prime, 7 is prime, 9 is not prime -- nope, not all odd numbers are prime."

The physicist thinks, " 3 is prime, 5 is prime, 7 is prime, 9 is not prime -- that could be experimental error -- 11 is prime, 13 is prime, yes, they're all prime."

The engineer thinks, " 3 is prime, 5 is prime, 7 is prime, 9 is prime, 11 is prime, ..."

https://users.cs.northwestern.edu/~riesbeck/mathphyseng.html

Re: Write Your Own Virtual Machine

#15
Note that this is _just_ a virtual machine.

You can only run the already compiled binary files for it.

If you want to write something new you will need to do it in binary. If you want to write in assembly you will need to write an assembler, or find one online.

Re: Write Your Own Virtual Machine

#16

I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here. The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a…

> It will be completely obvious to you why “i++” is slower than “++i” ...but it's not! All you have to do is perform the most basic of optimizations: check, before generating code for an expression, if that expression is used. If not, then don't bother generating an intermediate for the result. Source: making a c compiler, just implemented this optimization today. (In an 'industrial-grade compiler', you probably want…

Better yet, every part of the C spec that has undefined behavior throws an error at compile time.

Re: Write Your Own Virtual Machine

#17
If you want to go really deep on this I highly recommend NAND To Tetris, a book which starts from the basics of combining NAND gates to build basic logic. You’ll gradually work through building a CPU from those components, building an assembler to program it, and finally putting a virtual machine on top of that.

Re: Write Your Own Virtual Machine

#18

I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here. The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a…

> It will be completely obvious to you why “i++” is slower than “++i” ...but it's not! All you have to do is perform the most basic of optimizations: check, before generating code for an expression, if that expression is used. If not, then don't bother generating an intermediate for the result. Source: making a c compiler, just implemented this optimization today. (In an 'industrial-grade compiler', you probably want…

Technically, that's a bit off topic because this is about compilers and code generation, while a VM defines the basic semantics compilers work with.

For VMs, what is relevant is whether you implement an "inc (reg)" or not, that is if you choose to take the RISC path (small set of mostly orthogonal instructions) or the CISC path (lots of microcode, things like "repnz scasb" in x86 assembly or elaborate addressing schemes).

This actually somewhat a false dichotomy, as you can have a RISC-style instruction set plus a few "high level" instructions for things you expect to do a lot - like, for instance, array operators à la APL/J.

Re: Write Your Own Virtual Machine

#19

I once wrote my own virtual machine in college, complete with compiler and assembler, and I cannot recommend doing this enough. Especially the virtual machine part is not nearly as difficult as you would imagine, and to this day (15 years later) I still rely on the things i learned here. The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a…

I thought that i++ ++i difference is related to the inner data structures of C++ iterators and that when using the former you need to keep 2 states compared to just one for that fraction of execution.

Re: Write Your Own Virtual Machine

#20
For those who haven't read the post yet, I would definitely recommend doing so if only to understand the striped instruction set implementation using C++ generics at the end.

I rarely run into solutions that fundamentally expand my perspective these days but this one did.

Post reply on HN