TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Write Your Own Virtual Machine

326 点作者 ChankeyPathak将近 5 年前

14 条评论

ghj将近 5 年前
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).<p>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&#x2F;datastructure tasks but half of them were actually about implementing and using a VM for a made up assembly language! The difficulty ramp was gradual enough that I did it with no background in compilers and it was lots of fun.<p>If you want to experience it, in last year&#x27;s version <a href="https:&#x2F;&#x2F;adventofcode.com&#x2F;2019" rel="nofollow">https:&#x2F;&#x2F;adventofcode.com&#x2F;2019</a> you write the VM in Days 2, 5, 7, 9 then apply it to solving problems in Days 11, 13, 15, 17, 19, 21, 23, 25.<p>I ended up loving the VM puzzles and ended up doing the AoC author&#x27;s other challenges which have a similar theme: <a href="https:&#x2F;&#x2F;challenge.synacor.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;challenge.synacor.com&#x2F;</a>
评论 #24167510 未加载
评论 #24167619 未加载
cecilpl2将近 5 年前
I have written a virtual CPU from very simple building blocks and highly recommend doing it.<p>I started with class Wire (which just wraps a bool) and class Transistor (which accepts two input Wire&amp; and has an output Wire). It has an Update() function which sets the output state.<p>From those I built up gates, then flipflops, registers, mux&#x2F;demuxes, counters, an ALU, and memory banks. Then I wrote a machine language, connected the cpu together, wrote a loader to load machine language files into ROM, then an assembler to allow me to write assembly code.<p>I then added VRAM, async buses, and wrote a working implementation of Game of Life.<p>It has 32bit instructions and runs about 10kHz with 8KB of RAM.
评论 #24305500 未加载
评论 #24170664 未加载
评论 #24169902 未加载
stingraycharles将近 5 年前
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.<p>The knowledge you gain from implementing a virtual machine translates reasonably well to inner workings of a CPU, and you’ll have a much better understanding of things like stacks, frame pointers and the overhead of calling a function. It will be completely obvious to you why “i++” is slower than “++i”.<p>Thanks for sharing this article.
评论 #24167264 未加载
评论 #24167435 未加载
评论 #24167314 未加载
评论 #24168386 未加载
jon-wood将近 5 年前
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.
评论 #24170153 未加载
nailuj将近 5 年前
If you want a really in-depth intro into the topic (with video lectures and guest appearances from legendary VM implementers!), check out CS294 from UC Berkeley that has been made available under a Creative Commons license: <a href="http:&#x2F;&#x2F;www.wolczko.com&#x2F;CS294&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.wolczko.com&#x2F;CS294&#x2F;</a><p>It‘s great for following along self-paced and has hands-on exercises for each topic. You should be a bit familiar with compilers already though.
danjc将近 5 年前
I&#x27;ve just about finished writing a VM for the ZMachine (the VM that Zork was written for to make it portable). It&#x27;s been tremendously enjoyable and I&#x27;ve learnt a lot.<p>I&#x27;m planning a series of how-to videos out of the project and will definitely be referencing this article to ensure my presentation of concepts is accurate. Thanks for sharing this.
aSplash0fDerp将近 5 年前
VMs have so much utility, so +1 to the author on the nerd nugget for compartimentalizing the use as a game cartridge.<p>VMs and VEs (environments) have gotten so portable that IOT and 5G will have to compete with raw storage (TB&#x27;s) for the best user experience in a post-filtered world.<p>Possibly OT (or use case), but I&#x27;ve demod LTSP [0] to run multiple environments on a single server (PXE menu) and clustered multiple LTSP servers (bouncing to other LTSP menus by manually adding a server list to boot menu(s)) and treated them as books with chapters (each LTSP being a book).<p>Customizing each chapter by OS needs and media&#x2F;content&#x2F;apps has so many uses (immersive news,learning,entertainment) and paves the way for offnet productivity&#x2F;entertainment that currently doesn&#x27;t exist in the market (ala Home Library&#x2F;Encyclopedia Server).<p>LTSP was not as PnP as it needs to be for consumer use (and external PCIe form factors were pricey), but anyone writing a VM (or decorating the interior) is already riding the next wave of physical data subscriptions&#x2F;refills that bypass metered networks (SDCs or SSDs in TB capacities with OS agnostic data) for 8k video, VR and compilation&#x2F;mixedtape datasets.<p>Portable VMs are a trend waiting to happen, so anything you can learn about it (inside-out) is not a waste.<p>[0] <a href="https:&#x2F;&#x2F;ltsp.org" rel="nofollow">https:&#x2F;&#x2F;ltsp.org</a>
codr7将近 5 年前
For those who haven&#x27;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.<p>I rarely run into solutions that fundamentally expand my perspective these days but this one did.
stevekemp将近 5 年前
Virtual machines are definitely fun, and can be useful things to know about if you ever design&#x2F;implement a scripting language or an emulator.<p>I wrote a toy system a few years ago, a simple interpreter (C) along with a compiler&#x2F;decompiler (Perl) to match it. Unfortunately my system didn&#x27;t have a terribly well-designed instruction set. If I were to start over I&#x27;d probably implement 8086 instruction-set, or similar.<p>That said even a toy system is fun, the biggest issue with writing your own instruction-set is that you have to write the actual programs too. Which is often less fun! I rewrote my interpreter in golang recently, keeping the same instruction-set but adding a better trap-system. Of course re-implementing it meant that I still have the problem of no real programs being written for the machine!<p><a href="https:&#x2F;&#x2F;github.com&#x2F;skx&#x2F;go.vm&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;skx&#x2F;go.vm&#x2F;</a>
teleforce将近 5 年前
FYI, Edouard Bugnion one of co-founders of VMware has written a book on VM technology &quot;Hardware and Software Support for Virtualization&quot;[1].<p>[1]<a href="https:&#x2F;&#x2F;research.vmware.com&#x2F;publications&#x2F;hardware-and-software-support-for-virtualization" rel="nofollow">https:&#x2F;&#x2F;research.vmware.com&#x2F;publications&#x2F;hardware-and-softwa...</a>
retrac将近 5 年前
Slight tangent. To learn a bit of Zig, I&#x27;ve been doing the usual PDP-8 simulator task I do for systems languages. Zig allows arbitrary fixed width integers, from 1 to 2^16 bits long, signed and unsigned. It is &#x2F;delightful&#x2F; to have those data types when doing an emulator. I&#x27;m convinced more languages should have them.
Const-me将近 5 年前
Never wrote virtual machines, because I know why Sun (now Oracle) and Microsoft both spent a billion each to create theirs. You can write something that works over a weekend, but the performance won’t be good without JIT, generational GC, and many other extremely complicated optimizations.<p>If you think you need to develop a VM, I recommend to reconsider, and think how you can reuse something that’s already there. For instance, modern .NET VM is open source with MIT license, the code quality is more or less OK, and it’s relatively easy to generate .NET bytecode from something else, Reflection.Emit from the inside, or Mono.Cecil from outside.
评论 #24169964 未加载
评论 #24169947 未加载
andreareina将近 5 年前
Extra credit: implement OP_HCF
konjin将近 5 年前
Note that this is _just_ a virtual machine.<p>You can only run the already compiled binary files for it.<p>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.