英文:
Does a Virtual Machine have to interpret Bytecode or can it interpret the VM's assembly directly?
问题
我计划使用Go语言实现一个虚拟机。我看到一些教程中,人们设计了自己的汇编语言来为他们的虚拟机编写代码,但是虚拟机并不直接执行汇编代码。他们将每个指令编码为一个数字,并形成了特殊的字节码来表示机器指令。
在解释执行字节码和直接解释汇编代码之间,哪种方式更好呢?能否解释汇编代码并获得相同的结果?
英文:
I am planning on implementing a VM in Go. I saw tutorials where people designed their own type of assembly for their VM, but the Virtual Machine didn't execute the assembly code directly. They encoded each of the instructions for their VM assigning them each a number and forming a special bytecode for their machine.
Is it better to interpret the bytecode or can you interpret the assembly code and achieve the same results?
答案1
得分: 3
如果您想在不同的客户平台上使用您的虚拟机,则是的。
字节码给您带来的优势是可移植性(因此有时也称为“p-code”,即“可移植代码”的缩写)。
如果您计划在不同的平台上使用您的虚拟机,您应该选择字节码。然后,您需要将程序编译为字节码指令,并让虚拟机处理其余部分。
英文:
If you want to use your VM in different guest platforms, then yes.
The advantage that bytecode gives you is portability (therefore the alternate naming "p-code", which is short for "portable code").
If you plan to use your VM in different platforms, you should go for bytecode. Then you would have to take care of compiling the program into bytecode instructions and the VM would take care of the rest.
答案2
得分: 1
语言运行时执行编译后的字节码,而不是文本汇编指令,因为解析文本很慢。一个良好编写的运行时可以在几个处理器周期内解析和执行一个字节码指令,但解析文本语句需要更多的工作。最明智的做法是解析整个汇编文件,并将其存储在内存中的中间表示中,这正是字节码的作用。此外,一些跳转和寻址等操作在字节码中才真正有意义。
不要将字节码视为汇编的二进制表示,而应将汇编视为字节码的文本表示。
英文:
Language runtimes execute compiled bytecode rather than textual assembly instructions because parsing text is slow. A well written runtime can parse and execute a bytecode instruction in a few processor cycles - but parsing textual statements requires a lot more work. The sanest way to do it would be to parse the entire assembly file and store it in memory in an intermediate representation - which is exactly what bytecode is. Further, some things such as jumps and addressing only really make sense in bytecode.
Instead of thinking as bytecode as a binary representation of assembly, consider assembly as a textual representation of bytecode.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论