JVM architecture model
The instruction stream input by the Java compiler is basically a stack-based instruction set architecture, and the other instruction set architecture is a register-based instruction set architecture. Specifically: the difference between the two architectures is
based on the characteristics of the stack architecture
- Simpler design and implementation, suitable for resource-constrained systems;
- Avoid the problem of register allocation: use the zero address instruction method to allocate.
- Most of the instructions in the instruction stream are zero-address instructions, and their execution depends on the operation stack. The instruction set is smaller and the compiler is easy to implement.
- No need for hardware support, better portability and better cross-platform
Based on the characteristics of register architecture
- The typical application is the x86 binary instruction set: such as the traditional PC and Android's Davlik virtual machine.
- The instruction set architecture is completely dependent on hardware, and portability is poor
- Excellent performance and more efficient execution
- It takes less instructions to complete an operation.
- In most cases, the instruction set based on the register architecture is often dominated by one-address instructions, two-address instructions and three-address instructions, while the instruction set based on the stack architecture is based on zero-address instructions.
Example
The logic operation 2+3 is also executed, and the instructions are as follows:
Stack-based calculation process (take the Java virtual machine as an example):
iconst_2 //常量2入栈
istore_1
iconst_3 // 常量3入栈
istore_2
iload_1
iload_2
iadd //常量2/3出栈,执行相加
istore_0 // 结果5入栈
The register-based calculation process
mov eax,2 //将eax寄存器的值设为1
add eax,3 //使eax寄存器的值加3