我在玩Java类文件和字节码。但是我在类文件中停留了条件。
从理论上讲,我理解这个概念,但是我不理解类文件中的分支是如何完成的。这是一个小例子:

public static void main(String[] args) {
  int a = 78;
  int b = 52;
  boolean c;

  if(a==b){
    c = true;
  } else {
    c = false;
  }
}


使用javap -c -verbose Equal.class出现以下清单:

flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: bipush        78
         2: istore_1
         3: bipush        52
         5: istore_2
         6: iload_1
         7: iload_2
         8: if_icmpne     16
        11: iconst_1
        12: istore_3
        13: goto          18
        16: iconst_0
        17: istore_3
        18: return
      LineNumberTable:
        line 4: 0
        line 5: 3
        line 7: 6
        line 8: 11
        line 10: 16
        line 11: 18
      StackMapTable: number_of_entries = 2
           frame_type = 253 /* append */
             offset_delta = 16
        locals = [ int, int ]
           frame_type = 252 /* append */
             offset_delta = 1
        locals = [ int ]


现在,我正在查找类文件,以查找分支。操作码if_icmpne的十六进制表示为0xA0。我假设分支标记将跟随0xA0。在我的情况下,有2个字节:0x0008。
我的问题:两个字节(0x0008)代表什么?
我试了很多例如,我通过LineNumberTable和Constant Pool遵循路径,但是找不到任何有意义的东西。

(goto当然也一样)

此外,以下是上面列出的邮政清单的完整序列:

10 4E  // bipush 78
3C     // istore_1
10 34  // bipush 52
3D     // istore_2
1B     // iload_1
1C     // iload_2
A0     // if_icmpne
00 08  // ???
04     // iconst_1
3E     // istore_3
A7     // goto
00 05  // ???
03     // iconst_0
3E     // istore_3
B1     // return


先感谢您!

最佳答案

0x0008是分支偏移量-即从当前指令向前跳转以查找下一条指令的字节数。因此从if_icmpne跳8个字节(-> 00-> 08-> 04-> 3E-> A7-> 00-> 05-> 03)到iconst_0goto相同:从goto跳5个字节(-> 00-> 05-> 03-> 3E-> B1)到return

07-24 09:38
查看更多