Why do VMs need to be "stack machines" or "register machines" etc.?
Posted
by
Prog
on Programmers
See other posts from Programmers
or by Prog
Published on 2014-06-08T20:18:33Z
Indexed on
2014/06/08
21:39 UTC
Read the original article
Hit count: 212
virtual-machine
|bytecode
(This is an extremely newbie-ish question).
I've been studying a little about Virtual Machines.
Turns out a lot of them are designed very similarly to physical or theoretical computers.
I read that the JVM for example, is a 'stack machine'. What that means (and correct me if I'm wrong) is that it stores all of it's 'temporary memory' on a stack, and makes operations on this stack for all of it's opcodes.
For example, the source code 2 + 3
will be translated to bytecode similar to:
push 2
push 3
add
My question is this:
JVMs are probably written using C/C++ and such. If so, why doesn't the JVM execute the following C code: 2 + 3
..? I mean, why does it need a stack, or in other VMs 'registers' - like in a physical computer?
The underlying physical CPU takes care of all of this. Why don't VM writers simply execute the interpreted bytecode with 'usual' instructions in the language the VM is programmed with?
Why do VMs need to emulate hardware, when the actual hardware already does this for us?
Again, very newbie-ish questions. Thanks for your help
© Programmers or respective owner