Skip to content

Instantly share code, notes, and snippets.

@linghuiluo
Created April 19, 2020 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linghuiluo/5c7a05378b6fad5afabbc7d4f6cfc74a to your computer and use it in GitHub Desktop.
Save linghuiluo/5c7a05378b6fad5afabbc7d4f6cfc74a to your computer and use it in GitHub Desktop.
Java Bytecode Execution
public class Spin
{

    private int a;
    private String b;

    protected void spin()
    {
        int i;
        for(i=0;i<100;i++){}
    }
}
public class Spin
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#20         // java/lang/Object."<init>":()V
   #2 = Class              #21            // Spin
   #3 = Class              #22            // java/lang/Object
   #4 = Utf8               a
   #5 = Utf8               I
   #6 = Utf8               b
   #7 = Utf8               Ljava/lang/String;
   #8 = Utf8               <init>
   #9 = Utf8               ()V
  #10 = Utf8               Code
  #11 = Utf8               LineNumberTable
  #12 = Utf8               LocalVariableTable
  #13 = Utf8               this
  #14 = Utf8               LSpin;
  #15 = Utf8               spin
  #16 = Utf8               i
  #17 = Utf8               StackMapTable
  #18 = Utf8               SourceFile
  #19 = Utf8               Spin.java
  #20 = NameAndType        #8:#9          // "<init>":()V
  #21 = Utf8               Spin
  #22 = Utf8               java/lang/Object
{
  private int a;
    descriptor: I
    flags: ACC_PRIVATE

  private java.lang.String b;
    descriptor: Ljava/lang/String;
    flags: ACC_PRIVATE

  public Spin();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LSpin;

  protected void spin();
    descriptor: ()V
    flags: ACC_PROTECTED
    Code:
      stack=2, locals=2, args_size=1 // a 2-element high stack, 2 local varaibles, 1 argument which is "this" reference. 
         0: iconst_0
         1: istore_1
         2: iload_1
         3: bipush        100
         5: if_icmpge     14
         8: iinc          1, 1
        11: goto          2
        14: return
      LineNumberTable:
        line 10: 0
        line 11: 14
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      15     0  this   LSpin;
            2      13     1     i   I
      StackMapTable: number_of_entries = 2
        frame_type = 252 /* append */
          offset_delta = 2
          locals = [ int ]
        frame_type = 11 /* same */
}
Instruction Locals [0,1] Stack
[this, ? ]
iconst_0 [this, ? ] 0
istore_1 [this, 0 ]
i=0
iload_1 [this, 0 ] 0
bipush 100 [this, 0 ] 0 100
if_icmpge 14 [this, 0 ]
i=1
iinc 1,1 [this, 1 ]
goto 2 [this, 1 ]
iload_1 [this, 1 ] 1
bipush 100 [this, 1 ] 1 100
if_icmpge 14 [this, 1 ]
i=2
iinc 1,1 [this, 2 ]
goto 2 [this, 2 ]
iload_1 [this, 2 ] 2
bipush 100 [this, 2 ] 2 100
if_icmpge 14 [this, 2 ]
...
i=100
iinc 1,1 [this, 100 ]
goto 2 [this, 100 ]
iload_1 [this, 100 ] 100
bipush 100 [this, 100 ] 100 100
if_icmpge 14 [this, 100 ]
return [this, 100 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment