Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top