Based on register and Stack __java

Source: Internet
Author: User

For us, the registers are familiar, and most of the x86 and arm directives we are familiar with. So based on what the stack is. In fact, based on the stack for some resource-scarce equipment (set-top box, embedded) more use. The JVM is based on the stack because the Java design idea is to run both on the server and in the embedded device (Java ME), because the stack based versatility is better so the JVM uses it. based on the nature of the stack

Based on stack essence, the Operation Stack is used to store the operands, which replaces the way the operands are placed in the instruction (based on registers).

The following quotes: a stack based virtual machine VS a register based virtual machine
For example, execute "a = B + C", and the bytecode instruction on the stack based virtual machine looks like this:

I1:load C
I2:load B
I3:add
I4:store A

Because the operands are all implicit, the instructions can be done very briefly, typically one or two bytes. But it is obvious that the number of instruction bars will increase significantly. And there is only one instruction to perform this operation on a register virtual machine:

I1:add A, B, c

Where a,b,c are virtual registers. The changes on the operand stack are shown in the following illustration:

First read the data from the symbol table on the stack of the operand,

The operand is then popped out of the stack to perform an addition operation, which is performed by the physical machine, as shown in the following figure:

comparison between the two codes

The following quote: Android Dalvik virtual Machine First
We can see from the screenshot below the comparison of Java bytecode and Dalvid bytecode corresponding to the same section of Java code.

Description

0x04 iconst_1 pushes int 1 to the top of the stack (Operation Stack)
0x05 iconst_2 pushes int 2 to the top of the stack
0x1a Iload_0 pushes the first int-type local variable (from a local variable table) to the top of the stack
0x1b Iload_1 pushes the second int local variable to the top of the stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.