Yes, Java is Stack based Architecture Virtual Machine,
A virtual machine (VM) is a high level abstraction on top of the native operating system, that emulates a physical machine. Here, we are talking about process virtual machines and not system virtual machines. A virtual machine enables the same platform to run on multiple operating systems and hardware architectures. The Interpreters for Java and Python can be taken as examples, where the code is compiled into their VM specific bytecode. The same can be seen in the Microsoft .Net architecture, where code is compiled into intermediate language for the CLR (Common Language Runtime).
What should a virtual machine generally implement? It should emulate the operations carried out by a physical CPU and thus should ideally encompass the following concepts:
- Compilation of source language into VM specific bytecode
- Data structures to contains instructions and operands (the data the instructions process)
- A call stack for function call operations
- An ‘Instruction Pointer’ (IP) pointing to the next instruction to execute
- A virtual ‘CPU’ – the instruction dispatcher that
- Fetches the next instruction (addressed by the instruction pointer)
- Decodes the operands
- Executes the instruction
Stack Based Virtual Machines
A stack based virtual machine implements the general features described as needed by a virtual machine in the points above, but the memory structure where the operands are stored is a stack data structure. Operations are carried out by popping data from the stack, processing them and pushing in back the results in LIFO (Last in First Out) fashion. In a stack based virtual machine, the operation of adding two numbers would usually be carried out in the following manner (where 20, 7, and ‘result’ are the operands):

- POP 20
- POP 7
- ADD 20, 7, result
- PUSH result
Register Based Virtual Machines
In the register based implementation of a virtual machine, the data structure where the operands are stored is based on the registers of the CPU. There is no PUSH or POP operations here, but the instructions need to contain the addresses (the registers) of the operands. That is, the operands for the instructions are explicitly addressed in the instruction, unlike the stack based model where we had a stack pointer to point to the operand. For example, if an addition operation is to be carried out in a register based virtual machine, the instruction would more or less be as follows:

- ADD R1, R2, R3 ; # Add contents of R1 and R2, store result in R3
Another advantage of the register based model is that it allows for some optimizations that cannot be done in the stack based approach. One such instance is when there are common sub expressions in the code, the register model can calculate it once and store the result in a register for future use when the sub expression comes up again, which reduces the cost of recalculating the expression.
The problem with a register based model is that the average register instruction is larger than an average stack instruction, as we need to specify the operand addresses explicitly. Whereas the instructions for a stack machine is short due to the stack pointer, the respective register machine instructions need to contain operand locations, and results in larger register code compared to stack code
Now the question can be Why JVM is stack based VM
There are a few attributes of a stack-based VM that fit in well with Java's design goals:
- A stack-based design makes very few assumptions about the target hardware (registers, CPU features), so it's easy to implement a VM on a wide variety of hardware.
- Since the operands for instructions are largely implicit, the object code will tend to be smaller. This is important if you're going to be downloading the code over a slow network link.
Now Read out full Detail about Java Stack:
Java Stack
And Read out what is Native Stack
Native Stack
No comments:
Post a Comment