27 Jan 2021

Java Bytecode , OpCodes Simple Example


What is Java Bytecode

Java Bytecode is an instruction set for JVM (Java Virtual Machine).

Bytecode's OPCODE

Byte sized opcodes.
So , only 256 possible opcodes.
Oprand can be of 8 bits (1 byte) or 16 bits(2 byte).

Stack Machine

Push 1 --> Push 2 --> add
1 + 2 => 3

Example 1
    void spin() {
        int i;
        for(i = 0; i < 100; i++){
            ; //empty
        }
        
    }
    

        void spin();
        Code ;
        stack = 2, locals = 2, args_size = 1
        0 : iconst_0
        1 : istore_1
        2 : iloaf_1
        3 : bipush      100
        5 : if_iempge    14
        8 : linc       1, 1
       11 : goto       2
       14 : return
       
       
stack
Example 2

A method as an example, the method adding to integers datatype - int.
AFTER Completion To Bytecode

            int addTwo(int i , int j){
                return i+j;
            }
        

            int addTwo(int, int)
            stack = 2, local = 3; arge_size= 1
            
            0 : iload_1
            1 : iload_2
            3 : ireturn
            
            
locals no. = [i , j , this];
Example 3

A method as an example, the method adding two int datatype variables which is local to this method.
AFTER Completion To Bytecode
It is different from EXAMPLE 2.

            int add(){
                int x = 10;
                int y = 20;
                return x+y;
            }
            
            

            int add(int , int):.
            
            0 : bipush 10
            2 : istore_1
            3 : bipush 20
            5 : isore_2
            6 : iload_1
            7 : iload_2
            8 : iadd
            6 ; ireturn
        

Reaction:

No comments:

No Comments

COMMENT FROM MESSAGE