JVM operand stack - detailed explanation of the running process

The operand stack (Operand Stack), also known as the operation stack, is a last-in, first-out (LIFO) stack. With the execution of methods and bytecode instructions, constants or variables will be copied from the local variable table or fields of object instances and written to the operand stack, and then the elements in the stack will be popped to the local variable table as the calculation proceeds Or return to the method caller, that is, pop/pull operations.
Demonstrate the execution of the operator station by the following code

public class StackDemo2 {
    
    
    public static void main(String[] args) {
    
    
        int i = 1;
        int j = 2;
        int z = i + j;
    }
}

Program running

  • 1): The program counter finds the corresponding offset to carry out the pointer
    insert image description here
  • 2): The program runs for the first time to find the 0th address (0 is obtained by the program counter)
    insert image description here
  • 3): When the program counter gets the first position 0, it is necessary to get the first corresponding save command
    insert image description here
  • 4): (int i =1) "1" is defined in the operand stack
    insert image description here
    1. : a-offset continues to go down (1 is reassigned to our counter),

insert image description here
b- Execute the second save instruction (the process of copying 1 in the operand stack to the local variable table)
insert image description here

  • 6): Next, the pointer continues to go down. Repeat the above 1-5 process (int j=2)
    insert image description here
    insert image description here
    insert image description here

  • 7): 1 in the local variable table is loaded into the operand stack
    insert image description here

  • 8): 2 in the local variable table is loaded into the operand stack
    insert image description here

  • 9): execute add i + j = 3
    insert image description here

  • 10): Save the result 3 after adding in the operand stack to the local variable table
    insert image description here

  • 11): return
    insert image description here

Guess you like

Origin blog.csdn.net/qq_31686241/article/details/128063634