`

ASM4.0源码走读之二 指令的类型

阅读更多

        在深入分析ClassReader.readCode()方法之前,我们需要大概了解下ASM把jvm的指令分的类型,这在readCode会用到。先看ClassReader的代码:

    static {
        int i;
        byte[] b = new byte[220];
        String s = "AAAAAAAAAAAAAAAABCLMMDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
                + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                + "AAAAAAAAAAAAAAAAANAAAAAAAAAAAAAAAAAAAAJJJJJJJJJJJJJJJJDOPAA"
                + "AAAAGGGGGGGHIFBFAAFFAARQJJKKJJJJJJJJJJJJJJJJJJ";
        for (i = 0; i < b.length; ++i) {
            b[i] = (byte) (s.charAt(i) - 'A');
        }
        TYPE = b;
    }

         这其中的TYPE是一个字节数组,索引是jvm指令的byte值,值从0开始,分了16类,简化后是:

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 2,11,12, 
12,3, 3, 3, 3, 3, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 
0, 0, 0, 0, 3, 3, 3, 3, 3, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0,13, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 9, 9, 9, 9, 9, 9, 9, 
9, 9, 9, 9, 9, 9, 9, 9, 9, 3,
14,15,0, 0, 0, 0, 0, 0, 6, 6, 
6, 6, 6, 6, 6, 7, 8, 5, 1, 5, 
0, 0, 5, 5, 0, 0, 17,16,9, 9,
10,10, 9, 9, 9, 9, 9,9, 9, 9, 
9, 9, 9, 9, 9, 9, 9, 9, 9, 9

      补全下,指令跟指令类型的对照表:

指令操作码 16进制    指令类型                指令
##类型为0的指令没有操作数
0    =    0x0    =    0    ...............nop
1    =    0x1    =    0    ...............aconst_null
2    =    0x2    =    0    ...............iconst_m1
3    =    0x3    =    0    ...............iconst_0
4    =    0x4    =    0    ...............iconst_1
5    =    0x5    =    0    ...............iconst_2
6    =    0x6    =    0    ...............iconst_3
7    =    0x7    =    0    ...............iconst_4
8    =    0x8    =    0    ...............iconst_5
9    =    0x9    =    0    ...............lconst_0
10    =    0xa    =    0    ...............lconst_1
11    =    0xb    =    0    ...............fconst_0
12    =    0xc    =    0    ...............fconst_1
13    =    0xd    =    0    ...............fconst_2
14    =    0xe    =    0    ...............dconst_0


15    =    0xf    =    0    ...............dconst_1

##类型为1的指令操作数长度为1个字节
16    =    0x10    =    1    ...............bipush

##类型为2的指令操作数长度为2个字节
17    =    0x11    =    2    ...............sipush

18    =    0x12    =    11    ...............ldc
19    =    0x13    =    12    ...............ldc_w
20    =    0x14    =    12    ...............ldc2_w

##类型为3的指令操作数长度为1个字节
21    =    0x15    =    3    ...............iload
22    =    0x16    =    3    ...............lload
23    =    0x17    =    3    ...............fload
24    =    0x18    =    3    ...............dload
25    =    0x19    =    3    ...............aload

##类型为4的指令操作数长度为1个字节
26    =    0x1a    =    4    ...............iload_0
27    =    0x1b    =    4    ...............iload_1
28    =    0x1c    =    4    ...............iload_2
29    =    0x1d    =    4    ...............iload_3
30    =    0x1e    =    4    ...............lload_0
31    =    0x1f    =    4    ...............lload_1
32    =    0x20    =    4    ...............lload_2
33    =    0x21    =    4    ...............lload_3
34    =    0x22    =    4    ...............fload_0
35    =    0x23    =    4    ...............fload_1
36    =    0x24    =    4    ...............fload_2
37    =    0x25    =    4    ...............fload_3
38    =    0x26    =    4    ...............dload_0
39    =    0x27    =    4    ...............dload_1
40    =    0x28    =    4    ...............dload_2
41    =    0x29    =    4    ...............dload_3
42    =    0x2a    =    4    ...............aload_0
43    =    0x2b    =    4    ...............aload_1
44    =    0x2c    =    4    ...............aload_2
45    =    0x2d    =    4    ...............aload_3
46    =    0x2e    =    0    ...............iaload
47    =    0x2f    =    0    ...............laload
48    =    0x30    =    0    ...............faload
49    =    0x31    =    0    ...............daload
50    =    0x32    =    0    ...............aaload
51    =    0x33    =    0    ...............baload
52    =    0x34    =    0    ...............caload
53    =    0x35    =    0    ...............saload
54    =    0x36    =    3    ...............istore
55    =    0x37    =    3    ...............lstore
56    =    0x38    =    3    ...............fstore
57    =    0x39    =    3    ...............dstore
58    =    0x3a    =    3    ...............astore
59    =    0x3b    =    4    ...............istore_0
60    =    0x3c    =    4    ...............istore_1
61    =    0x3d    =    4    ...............istore_2
62    =    0x3e    =    4    ...............istore_3
63    =    0x3f    =    4    ...............lstore_0
64    =    0x40    =    4    ...............lstore_1
65    =    0x41    =    4    ...............lstore_2
66    =    0x42    =    4    ...............lstore_3
67    =    0x43    =    4    ...............fstore_0
68    =    0x44    =    4    ...............fstore_1
69    =    0x45    =    4    ...............fstore_2
70    =    0x46    =    4    ...............fstore_3
71    =    0x47    =    4    ...............dstore_0
72    =    0x48    =    4    ...............dstore_1
73    =    0x49    =    4    ...............dstore_2
74    =    0x4a    =    4    ...............dstore_3
75    =    0x4b    =    4    ...............astore_0
76    =    0x4c    =    4    ...............astore_1
77    =    0x4d    =    4    ...............astore_2
78    =    0x4e    =    4    ...............astore_3
79    =    0x4f    =    0    ...............iastore
80    =    0x50    =    0    ...............lastore
81    =    0x51    =    0    ...............fastore
82    =    0x52    =    0    ...............dastore
83    =    0x53    =    0    ...............aastore
84    =    0x54    =    0    ...............bastore
85    =    0x55    =    0    ...............castore
86    =    0x56    =    0    ...............sastore
87    =    0x57    =    0    ...............pop
88    =    0x58    =    0    ...............pop2
89    =    0x59    =    0    ...............dup
90    =    0x5a    =    0    ...............dup_x1
91    =    0x5b    =    0    ...............dup_x2
92    =    0x5c    =    0    ...............dup2
93    =    0x5d    =    0    ...............dup2_x1
94    =    0x5e    =    0    ...............dup2_x2
95    =    0x5f    =    0    ...............swap
96    =    0x60    =    0    ...............iadd
97    =    0x61    =    0    ...............ladd
98    =    0x62    =    0    ...............fadd
99    =    0x63    =    0    ...............dadd
100    =    0x64    =    0    ...............isub
101    =    0x65    =    0    ...............lsub
102    =    0x66    =    0    ...............fsub
103    =    0x67    =    0    ...............dsub
104    =    0x68    =    0    ...............imul
105    =    0x69    =    0    ...............lmul
106    =    0x6a    =    0    ...............fmul
107    =    0x6b    =    0    ...............dmul
108    =    0x6c    =    0    ...............idiv
109    =    0x6d    =    0    ...............ldiv
110    =    0x6e    =    0    ...............fdiv
111    =    0x6f    =    0    ...............ddiv
112    =    0x70    =    0    ...............irem
113    =    0x71    =    0    ...............lrem
114    =    0x72    =    0    ...............frem
115    =    0x73    =    0    ...............drem
116    =    0x74    =    0    ...............ineg
117    =    0x75    =    0    ...............lneg
118    =    0x76    =    0    ...............fneg
119    =    0x77    =    0    ...............dneg
120    =    0x78    =    0    ...............ishl
121    =    0x79    =    0    ...............lshl
122    =    0x7a    =    0    ...............ishr
123    =    0x7b    =    0    ...............lshr
124    =    0x7c    =    0    ...............iushr
125    =    0x7d    =    0    ...............lushr
126    =    0x7e    =    0    ...............iand
127    =    0x7f    =    0    ...............land
128    =    0x80    =    0    ...............ior
129    =    0x81    =    0    ...............lor
130    =    0x82    =    0    ...............ixor
131    =    0x83    =    0    ...............lxor
132    =    0x84    =    13    ...............iinc
133    =    0x85    =    0    ...............i2l
134    =    0x86    =    0    ...............i2f
135    =    0x87    =    0    ...............i2d
136    =    0x88    =    0    ...............l2i
137    =    0x89    =    0    ...............l2f
138    =    0x8a    =    0    ...............l2d
139    =    0x8b    =    0    ...............f2i
140    =    0x8c    =    0    ...............f2l
141    =    0x8d    =    0    ...............f2d
142    =    0x8e    =    0    ...............d2i
143    =    0x8f    =    0    ...............d2l
144    =    0x90    =    0    ...............d2f
145    =    0x91    =    0    ...............i2b
146    =    0x92    =    0    ...............i2c
147    =    0x93    =    0    ...............i2s
148    =    0x94    =    0    ...............lcmp
149    =    0x95    =    0    ...............fcmpl
150    =    0x96    =    0    ...............fcmpg
151    =    0x97    =    0    ...............dcmpl
152    =    0x98    =    0    ...............dcmpg


#类型为9的指令,主要是if、goto、jsr三大类指令
153    =    0x99    =    9    ...............ifeq
154    =    0x9a    =    9    ...............ifne
155    =    0x9b    =    9    ...............iflt
156    =    0x9c    =    9    ...............ifge
157    =    0x9d    =    9    ...............ifgt
158    =    0x9e    =    9    ...............ifle
159    =    0x9f    =    9    ...............if_icmpeq
160    =    0xa0    =    9    ...............if_icmpne
161    =    0xa1    =    9    ...............if_icmplt
162    =    0xa2    =    9    ...............if_icmpge
163    =    0xa3    =    9    ...............if_icmpgt
164    =    0xa4    =    9    ...............if_icmple
165    =    0xa5    =    9    ...............if_acmpeq
166    =    0xa6    =    9    ...............if_acmpne
167    =    0xa7    =    9    ...............goto
168    =    0xa8    =    9    ...............jsr
169    =    0xa9    =    3    ...............ret

#14    tableswitch    对齐变长,high(4)    /    low(4)    /    offsets[(high-low+1)*4]
170    =    0xaa    =    14    ...............tableswitch

#15    lookupswitch    对齐变长    default(4)    /    npairs(4)    /    match(4)    -    offset(4)    pairs
171    =    0xab    =    15    ...............lookupswitch


172    =    0xac    =    0    ...............ireturn
173    =    0xad    =    0    ...............lreturn
174    =    0xae    =    0    ...............freturn
175    =    0xaf    =    0    ...............dreturn
176    =    0xb0    =    0    ...............areturn
177    =    0xb1    =    0    ...............return
178    =    0xb2    =    6    ...............getstatic
179    =    0xb3    =    6    ...............putstatic
180    =    0xb4    =    6    ...............getfield
181    =    0xb5    =    6    ...............putfield
182    =    0xb6    =    6    ...............invokevirtual
183    =    0xb7    =    6    ...............invokespecial
184    =    0xb8    =    6    ...............invokestatic
185    =    0xb9    =    7    ...............invokeinterface
186    =    0xba    =    8    ...............invokedynamic
187    =    0xbb    =    5    ...............new
188    =    0xbc    =    1    ...............newarray
189    =    0xbd    =    5    ...............anewarray
190    =    0xbe    =    0    ...............arraylength
191    =    0xbf    =    0    ...............athrow
192    =    0xc0    =    5    ...............checkcast
193    =    0xc1    =    5    ...............instanceof
194    =    0xc2    =    0    ...............monitorenter
195    =    0xc3    =    0    ...............monitorexit
196    =    0xc4    =    17    ...............wide
197    =    0xc5    =    16    ...............multianewarray
198    =    0xc6    =    9    ...............ifnull
199    =    0xc7    =    9    ...............ifnonnull

#goto、jsr扩展指令
200    =    0xc8    =    10    ...............goto_w
201    =    0xc9    =    10    ...............jsr_w

202    =    0xca    =    9    Reserved    opcodes:
203    =    0xcb    =    9    ...............breakpoint
204    =    0xcc    =    9    ...............impdep1
205    =    0xcd    =    9    ...............impdep2
206    =    0xce    =    9
207    =    0xcf    =    9
208    =    0xd0    =    9
209    =    0xd1    =    9
210    =    0xd2    =    9
211    =    0xd3    =    9
212    =    0xd4    =    9
213    =    0xd5    =    9
214    =    0xd6    =    9
215    =    0xd7    =    9
216    =    0xd8    =    9
217    =    0xd9    =    9
218    =    0xda    =    9
219    =    0xdb    =    9

 

分享到:
评论
4 楼 demoxshiroki 2014-12-08  
yueyemaitian 写道
demoxshiroki 写道
貌似ASM5.0 现在字节码区分不是这样


现在是啥样的?最近都没接触asm了

 /**
     * The type of instructions without any argument.
     */
    static final int NOARG_INSN = 0;

    /**
     * The type of instructions with an signed byte argument.
     */
    static final int SBYTE_INSN = 1;

    /**
     * The type of instructions with an signed short argument.
     */
    static final int SHORT_INSN = 2;

    /**
     * The type of instructions with a local variable index argument.
     */
    static final int VAR_INSN = 3;

    /**
     * The type of instructions with an implicit local variable index argument.
     */
    static final int IMPLVAR_INSN = 4;

    /**
     * The type of instructions with a type descriptor argument.
     */
    static final int TYPE_INSN = 5;

    /**
     * The type of field and method invocations instructions.
     */
    static final int FIELDORMETH_INSN = 6;

    /**
     * The type of the INVOKEINTERFACE/INVOKEDYNAMIC instruction.
     */
    static final int ITFMETH_INSN = 7;

    /**
     * The type of the INVOKEDYNAMIC instruction.
     */
    static final int INDYMETH_INSN = 8;

    /**
     * The type of instructions with a 2 bytes bytecode offset label.
     */
    static final int LABEL_INSN = 9;

    /**
     * The type of instructions with a 4 bytes bytecode offset label.
     */
    static final int LABELW_INSN = 10;

    /**
     * The type of the LDC instruction.
     */
    static final int LDC_INSN = 11;

    /**
     * The type of the LDC_W and LDC2_W instructions.
     */
    static final int LDCW_INSN = 12;

    /**
     * The type of the IINC instruction.
     */
    static final int IINC_INSN = 13;

    /**
     * The type of the TABLESWITCH instruction.
     */
    static final int TABL_INSN = 14;

    /**
     * The type of the LOOKUPSWITCH instruction.
     */
    static final int LOOK_INSN = 15;

    /**
     * The type of the MULTIANEWARRAY instruction.
     */
    static final int MANA_INSN = 16;

    /**
     * The type of the WIDE instruction.
     */
    static final int WIDE_INSN = 17;
3 楼 yueyemaitian 2014-12-07  
demoxshiroki 写道
貌似ASM5.0 现在字节码区分不是这样


现在是啥样的?最近都没接触asm了
2 楼 demoxshiroki 2014-12-04  
并且这样区分貌似 也没jvm 官方文档里面说的助记符,那样容易区分,不明白,这样区分的好处在什么地方
1 楼 demoxshiroki 2014-12-04  
貌似ASM5.0 现在字节码区分不是这样

相关推荐

    ASM4.0 中文教程与多种字节码编程实战案例

    内容包含ASM4.0中文手册,以及四种ASM常见的字节码操作应用范例,包含最新版本的ASM9.2的jar包,包含asm-9.2.jar,asm-commons-9.2.jar,asm-util-9.2.jar。 学习文章地址 ...

    asm4.0全家福

    标题中的“asm4.0全家福”指的是ASM库的一个完整版本集合,ASM是一个Java字节码操控和分析框架,主要用于动态生成类或者增强已有类的功能。这个“全家福”包括了ASM库的主要组件,分别是: 1. **asm-4.0**: 这是ASM...

    asm4.0_RC1-bin

    ASM4.0_RC1是ASM框架的一个版本,RC1代表Release Candidate 1,意味着这是一个发布前的候选版本,可能在正式发布之前用于测试和调试。 ASM的核心功能包括: 1. **字节码生成**:ASM提供了生成和解析Java字节码的...

    ASM4使用指南 - ASM 4 Guide 中文版

    ASM 4 使用指南中文版。ASM是Java字节码的工业级库。长期以来一直没有中文版。这下好了,中文版横空出世。感兴趣的同学可以投递简历 mars # oneapm ....ASM 4.0 A Java bytecode engineering library

    asm-tree-4.0.zip_asm4.0

    标题中的"asm-tree-4.0.zip_asm4.0"提到了ASM库的特定版本,ASM是一个Java字节码操控和分析框架,主要用于动态代理、字节码转换以及代码分析等场景。ASM 4.0是这个库的一个版本,可能包含了一些针对性能和功能的改进...

    spring-asm-repack-4.0.jar

    spring 3.2.5版本源码使用的,构建的时候使用如下描述 因为英文不太好,大概意思可能是什么asm4.0版本使用的spring 在spring4中已经重新包装了asm在spring-core的核心包里,当然我这解释不标准 看下满原文构建的描述...

    asm-4.0.jar

    asm-4.0.jar

    asm5.1源码-eclipse工程

    asm5.1源码 eclipse工程

    asm操作指南(中文)

    通过直接生成二进制`.class`文件,ASM能够在类被加载到Java虚拟机之前动态地改变类的行为。 - **应用场景**: - **程序分析**:用于查找应用程序中的潜在错误、检测未使用的代码以及实现代码逆向工程。 - **程序...

    asm-all-4.0.jar.zip

    标题中的"asm-all-4.0.jar.zip"指的是一个压缩包文件,该文件包含了ASM库的一个集合,版本为4.0。ASM是一个Java字节码操控和分析框架,主要用于动态生成类或者增强已有类的功能。ASM库广泛应用于字节码级别的编程,...

    asm.rar_单片机asm指令_机器指令_汇编机器码

    汇编指令是单片机编程的基础,每一个指令通常对应一个特定的机器码,也就是二进制代码,这种二进制代码可以直接被硬件执行。 汇编语言的基本结构包括指令、伪指令、宏指令和符号。其中,指令是实际执行操作的代码,...

    asm-util-4.0_RC1.jar.zip

    标题中的"asm-util-4.0_RC1.jar.zip"是一个归档文件,它是一个ZIP格式的压缩包,其中包含了ASM工具库的一个特定版本——ASM Util 4.0 Release Candidate 1(RC1)。ASM是一个Java字节码操控和分析框架,广泛用于动态...

    asm-4.2-API文档-中文版.zip

    赠送jar包:asm-4.2.jar; 赠送原API文档:asm-4.2-javadoc.jar; 赠送源代码:asm-4.2-sources.jar; 赠送Maven依赖信息文件:asm-4.2.pom; 包含翻译后的API文档:asm-4.2-javadoc-API文档-中文(简体)版.zip; ...

    asm5.0 源码+demo+doc

    ASM 可以直接产生二进制 class 文件,也可以在类被加载入 Java 虚拟机之前动态改变类行为。Java class 被存储在严格格式定义的 .class 文件里,这些类文件拥有足够的元数据来解析类中的所有元素:类名称、方法、属性...

    asm5.1源码和jar

    "asm5.1源码和jar"指的是ASM库的5.1版本,包含了该版本的源代码和编译后的JAR文件。 源代码部分是学习和理解ASM工作原理的关键,通过阅读源代码,开发者可以深入到ASM的内部机制,了解如何解析和生成字节码。ASM的...

    ASM源码

    ASM源码很好用,可以破解任何软件

    asm-4.1.jar

    ASM 是一个 Java 字节码操纵框架。它可以直接以二进制形式动态地生成 stub 类或其他代理类,或者在装载时...ASM 提供类似于 BCEL 和 SERP 之类的工具包的功能,但是被设计得更小巧、更快速,这使它适用于实时代码插装。

    Mader.C.ASM(中国龙语言学机器码式编译器4.0)源码

    《Mader.C.ASM(中国龙语言学机器码式编译器4.0)源码》是关于计算机编程领域的一款特殊编译器的源代码,主要用于将高级语言转化为机器码,以便计算机能够直接执行。这款编译器是4.0版本,意味着它经过了多次迭代和...

    asm-4.0_RC1.jar.zip

    "asm-4.0_RC1.jar.zip" 是ASM库的4.0_RC1版本的压缩包,其中包含ASM的主要实现类和接口。 这个版本的ASM主要提供了以下几个方面的功能: 1. **字节码解析**:ASM能够解析Java字节码,将其转化为一棵抽象语法树...

    asm3.1.jar

    我学习spring程序,测试程序时.老出bug, 后来从网上找原因说asm.jar版本太低 现把好不容易找来的jar包共享给大家

Global site tag (gtag.js) - Google Analytics