0

I'm planning on creating a virtual machine for a language I'm creating and I can't understand how converting my assembly into my own instruction set can make execution faster. If I make the VM understand my assembly, is that not exactly the same as making my VM understand my byte code? Obviously the byte code will have less characters than the assembly would that really make that big of a difference? For example, if I make my own byte code, then I have to show my virtual machine how to understand it, and if the virtual machine reads assembly directly then I still have to show it how to understand the assembly so how can one be faster than the other?

--EDIT--

So if I get my virtual machine to interpret the code below directly, it should be just as faster as the code below it?

add r1, r2, r3 ; Add the values of reg2 and reg3, store the result in reg1
print r1

Encoded byte code:

c5 7c 8c d8 c8 d7

The byte code and the assembly code above are just examples of what I think it will look like.

Seki
  • 11,135
  • 7
  • 46
  • 70
user3318845
  • 301
  • 4
  • 15
  • What is it that is making you think that bytecode on a vm is faster? – old_timer Mar 08 '14 at 19:20
  • I was told that executing byte code directly was loads faster than executing a custom assembly code. – user3318845 Mar 08 '14 at 19:32
  • 5
    Don't believe people on the internet. – stark Mar 08 '14 at 19:34
  • 1
    Either the person who told you that simply didn't know what s/he was talking about, they oversimplified their explanation, or you misunderstood them. Sure, you can find a piece of bytecode that executes faster than some poorly optimized assembly due to the JIT'er being smarter than the author of the assembly, but that doesn't mean "bytecode is faster than assembly." – Ed S. Mar 08 '14 at 19:55
  • What do you mean by assembly code? Is the `add r1, r2, r3` a string that you have to parse in order to execute each time? – pat Mar 08 '14 at 20:01
  • Assembly language is text containing mnemonics and directives that tell the assembler program how to create machine code. Machine code is executed directly by your physical CPU. Byte code is machine code for a virtual machine. It is executed by a virtual CPU that is emulated by your physical CPU (either by an interpreter or translation into the native instruction set, or some mix). – pat Mar 08 '14 at 20:04

1 Answers1

4

Parsing is notoriously difficult and slow.

If you use your assembler instructions as you have specified, you have to parse the instructions, parse the arguments, strip the comments. A lot of parsing code, string comparisons, converting arguments to register offsets or values, etc.

Executing the byte code is a simple switch statement, a simple integer comparison. Register arguments can be pre-defined as offsets into a register array. Much faster.

You can still think of it as assembler -- just compile your assembler into byte code. A byte code to assembler display tool would be useful also.

Brad Lanam
  • 5,192
  • 2
  • 19
  • 29