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
- 2): The program runs for the first time to find the 0th address (0 is obtained by the program counter)
- 3): When the program counter gets the first position 0, it is necessary to get the first corresponding save command
- 4): (int i =1) "1" is defined in the operand stack
-
- : a-offset continues to go down (1 is reassigned to our counter),
b- Execute the second save instruction (the process of copying 1 in the operand stack to the local variable table)
-
6): Next, the pointer continues to go down. Repeat the above 1-5 process (int j=2)
-
7): 1 in the local variable table is loaded into the operand stack
-
8): 2 in the local variable table is loaded into the operand stack
-
9): execute add i + j = 3
-
10): Save the result 3 after adding in the operand stack to the local variable table
-
11): return