2014-02-17 11:07
浏览 41


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?

图片转代码服务由CSDN问答提供 功能建议

我计划在Go中实现VM。 我看到了一些教程,在这些教程中,人们为虚拟机设计了自己的程序集类型,但是虚拟机并未直接执行程序集代码。 他们为虚拟机的每条指令编码,为每条指令分配一个数字,并为它们的机器形成一个特殊的字节码。 更好地解释字节码还是可以解释汇编代码并获得相同的结果?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanqun9618 2014-02-17 12:12

    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.

    点赞 评论
  • dongtan8979 2014-02-18 08:43

    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.

    点赞 评论

相关推荐 更多相似问题