【虚拟机从0到1】一、引言翻译

原文:https://justinmeiners.github.io/lc3-vm/
原作者:Justin Meiners and Ryan Pendleton
翻译:MsMs_

Write your Own Virtual Machine

By: Justin Meiners and Ryan Pendleton
(作者:Justin Meiners 和 Ryan Pendleton)
View the final code and other resources in the GitHub repo.
(在 GitHub repo中看最终代码以及其他资源)

1.Contents

目录内容不进行翻译

2. Introduction

In this tutorial, I will teach you how to write your own virtual machine (VM) that can run assembly language programs, such as my friend’s 2048 or my Roguelike. If you know how to program, but would like to gain a deeper understanding of what is going on inside a computer and better understand how programming languages work, then this project is for you. Writing your own VM may sound a little scary, but I promise that you will find it to be surprisingly simple and enlightening.
(在该教程中,我将会教你如何去编写可以运行汇编语言程序的虚拟机,比如我朋友的2048或者我的Roguelike。如果你了解编程,但是想要更深入理解计算机内部如何运转,更好地理解编程语言如何工作,那么这个项目十分适合你。虽然编写自己的虚拟机听起来可能有些吓人,但是我保证你将发现它是十分简单并且具有启发性。)

The final code is about 250 lines of C (unix, windows). All you need to know is how to read basic C or C++ and how to do binary arithmetic.
(最终的代码量大概是250行的C语言。你所需要知道的就是如何去阅读基本的C和C++代码以及如何进行二进制运算。)

Note: This VM is a literate program. This means you are reading the source code right now! Each piece of code from the project will be shown and explained thoroughly, so you can be sure nothing is left out. The final code was created by “weaving” the blocks of code together.
(注:该虚拟机是一个“文艺程序”。这意味着你现在正在阅读源代码。项目中每一段代码都将被彻底展示和解释,所以你可以确保所有内容未被省略。最终的代码是通过将代码块“编织”在一起所创造的。)

What is a virtual machine?(虚拟机是什么)

A VM is a program that acts like a computer. It simulates a CPU along with a few other hardware components, allowing it to perform arithmetic, read and write to memory, and interact with I/O devices, just like a physical computer. Most importantly, it can understand a machine language which you can use to program it.
(虚拟机是一款像真实计算机运行的程序。它模拟配备一些其他硬件的CPU,允许CPU进行运算、读写内存并且和输入输出设备交互,就像一台物理意义上的计算机。最重要的是,它可以理解你用来编写虚拟机的机器语言。)

The amount of computer hardware the VM attempts to simulate depends on its purpose. Some VMs are designed to reproduce the behavior of some particular computer, such as video game emulators. Most people don’t have an NES lying around anymore, but we can still play NES games by simulating the NES hardware in a program. These emulators must faithfully recreate every detail and major hardware component of the original device.
(虚拟机打算模拟的计算机硬件数量取决于该虚拟机的用途。有些虚拟机是为了复制某些特定计算机的功能,比如电子游戏模拟器。大多数人都没有红白机了,但是我们还是可以通过在软件中模拟红白机硬件来玩到红白机游戏。这样的模拟器必须如实地再现每个细节和原来设备上的主要硬件。)

Other VMs don’t act like any real computer and are entirely made up! This is primarily done to make software development easier. Imagine you wanted to create a program that ran on multiple computer architectures. A VM could offer a standard platform which provided portability for all of them. Instead of rewriting a program in different dialects of assembly for each CPU architecture, you would only need to write the small VM program in each assembly language. Each program would then be written only once in the VM’s assembly language.
(其他的虚拟机不像任何真实计算机,它是完全被虚构出来的!这主要是为了使软件开发更简单。假设你想创造一个可以运行在多个计算机体系结构上的程序。虚拟机提供了一个可为所有计算机体系结构提供可移植性的标准平台。你只要用汇编语言在系统中编写小型虚拟机,而不需要为了不同的CPU结构用不同的汇编语言重新编写程序。之后,每个程序只需用虚拟机汇编语言编写一次。)
没有虚拟机情况
虚拟机情况下
Note: A compiler solves a similar problem by compiling a standard high-level language to several CPU architectures. A VM creates one standard CPU architecture which is simulated on various hardware devices. One advantage of a compiler is that it has no runtime overhead while a VM does. Even though compilers do a pretty good job, writing a new one that targets multiple platforms is very difficult, so VMs are still helpful here. In practice, VMs and compilers are mixed at various levels.
(注:编译器通过把标准高级语言编译到多个CPU结构去解决相似的问题。虚拟机创建一个标准的CPU结构,该CPU结构可以在各种硬件设备上模拟。编译器的一个优点是它没有运行时开销,而虚拟机有。虽然编译器表现十分优秀,但是编写针对多平台的新编译器是非常困难的,所以虚拟机在多平台上仍然很管用。实际上,虚拟器和编译器在不同层次混合使用。)

The Java Virtual Machine (JVM) is a very successful example. The JVM itself is a moderately sized program that is small enough for one programmer to understand. This has made it possible to be written for thousands of devices including phones. Once the JVM is implemented on a new device, any Java, Kotlin, or Clojure program ever written can run on it without modification. The only cost is the overhead of the VM itself and the further abstraction from the machine. Most of the time, this is a pretty good tradeoff.
(Java虚拟机是个非常成功的例子。JVM自身就是个中等大小的程序,小到程序员也能很好理解。这就使得包括手机在内的众多设备内置虚拟机成为可能。一旦JVM在新设备上实现,任何Java、Kotlin或者Clojure程序都可以在它上面运行而不需要修改。唯一开销就是虚拟机自身的开销以及从机器进一步抽象的开销。大多数时候,这是十分有效的权衡)

A VM doesn’t have to be large or pervasive to provide a similar benefit. Old video games often used small VMs to provide simple scripting systems.
(虚拟机不必是要大或者普遍才能提供类似优势。老式电子游戏通常用小型虚拟机来提供简易脚本系统。)

VMs are also useful for executing code in a secure or isolated way. One application of this is garbage collection. There is no trivial way to implement automatic garbage collection on top of C or C++ since a program cannot see its own stack or variables. However, a VM is “outside” the program it is running and can observe all of the memory references on the stack.
(虚拟机以安全、隔离的方式来执行代码也是十分有效。其中一个应用就是垃圾收集。没有简单的方法在C或C++上实现自动收集垃圾,因为程序无法看到自己的堆栈或变量。然而,虚拟机是运行程序的“外部”并且可以观察到堆栈上所有内存引用。)

Another example of this behavior is demonstrated by Ethereum smart contracts. Smart contracts are small programs which are executed by each validating node in the blockchain network. This requires the node operators to run programs on their machines that have been written by complete strangers, without any opportunity to scrutinize them beforehand. To prevent a contract from doing malicious things, they are run inside a VM that has no access to the file system, network, disc, etc. Ethereum is also a good application of the portability features that result when using a VM. Since Ethereum nodes can be run on many kinds of computers and operating systems, the use of a VM allows smart contracts to be written without any consideration of the many platforms they run on.
(这一行为的另一例子是以太坊智能协议。智能协议是由区块链网络中每个验证节点所执行的小型程序。这就要求节点操作器在自己机器上运行完全由陌生人所编写的程序,而没有任何机会去提前仔细检查。为了防止协议做坏事,节点操作器运行在无法接触到文件系统、网络、硬盘等等的虚拟机内部。,以太坊也是使用虚拟机时可移植特性的良好应用。因为以太坊节点可以在多种计算机和操作系统上运行,虚拟机的使用允许智能协议被编写而不考虑它们所运行的平台。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值