JVM Tutorials - Herong's Tutorial Examples - v5.14, by Herong Yang
JASM Disassembler: "jasm -d"
This section provides a tutorial example on how to disassemble a JVM bytecode using the JASM disassembler.
What Is JASM Disassembler? JASM disassembler is a tool provided in the JASM package that allows you to disassemble JVM bytecode back into JASM assembly code. JASM disassembler can be invoked by the "jasm -d" command.
Let's try it with Calculator.class generated in the last tutorial.
1. Disassemble Calculator.class. Back up the original Calculator.jasm file. It may get overwritten by JASM tools accidentally.
# list original JASM source and bytecode files herong> ls -l Calculator.* -rw-r--r-- 1 herong staff 968 Nov 11 16:04 Calculator.class -rw-r--r--@ 1 herong staff 3616 Nov 11 16:01 Calculator.jasm # run JASM disassembler and store output in "out" sub-directory herong> jasm-0.7.0/bin/jasm -d -o out Calculator.class # list the disassembled output herong> ls -l out -rw-r--r-- 1 herong staff 2836 Nov 11 16:40 Calculator.jasm
2. Review the disassembled JASM code.
herong> more out/Calculator.jasm
/*
* Disassembled from Calculator.class (originally Calculator.jasm) by JASM
*
* Original class version: 55
* Signature: <no signature>
*/
public class Calculator {
// <no signature>
// <no exceptions>
public static main([java/lang/String)V {
exception label0, label1, label2, java/lang/NumberFormatException
label0:
aload 0
arraylength
bipush 3
if_icmpeq label3
getstatic java/lang/System.out java/io/PrintStream
ldc "Error: Invalid number of arguments."
invokevirtual java/io/PrintStream.println(java/lang/String)V
getstatic java/lang/System.out java/io/PrintStream
ldc "Usage: java Calculator <number> <operator> <number>"
invokevirtual java/io/PrintStream.println(java/lang/String)V
return
label3:
aload 0
iconst 0
aaload
invokestatic java/lang/Double.parseDouble(java/lang/String)D
dstore 1
aload 0
iconst 1
aaload
iconst 0
invokevirtual java/lang/String.charAt(I)C
istore 3
aload 0
iconst 2
aaload
invokestatic java/lang/Double.parseDouble(java/lang/String)D
dstore 4
label1:
goto label4
label2:
pop
getstatic java/lang/System.out java/io/PrintStream
ldc "Error: Please provide valid numeric values."
invokevirtual java/io/PrintStream.println(java/lang/String)V
return
label4:
iload 3
lookupswitch label5 {
42: label6,
43: label7,
45: label8,
47: label9
}
label7:
dload 1
dload 4
dadd
dstore 6
goto label10
label8:
dload 1
dload 4
dsub
dstore 6
goto label10
label6:
dload 1
dload 4
dmul
dstore 6
goto label10
label9:
dload 4
dconst 0
dcmpl
ifeq label11
dload 1
dload 4
ddiv
dstore 6
goto label10
label10:
getstatic java/lang/System.out java/io/PrintStream
dload 6
invokevirtual java/io/PrintStream.println(D)V
return
label5:
getstatic java/lang/System.out java/io/PrintStream
ldc "Error: Invalid operator. Use +, -, *, or /."
invokevirtual java/io/PrintStream.println(java/lang/String)V
return
label11:
getstatic java/lang/System.out java/io/PrintStream
ldc "Error: Division by zero is not allowed."
invokevirtual java/io/PrintStream.println(java/lang/String)V
return
}
}
3. Re-compile the disassembled JASM code.
herong> jasm-0.7.0/bin/jasm -i out -o out Calculator.jasm herong> ls -l out -rw-r--r-- 1 herong staff 968 Nov 11 16:44 Calculator.class -rw-r--r-- 1 herong staff 2836 Nov 11 16:40 Calculator.jasm
3. Test the re-compiled bytecode.
herong> java -cp out Calculator 0.9999 + 0.0001 1.0 herong> java -cp out Calculator 0.9999 - 0.0001 0.9998 herong> java -cp out Calculator 0.9999 = 0.0001 Error: Invalid operator. Use +, -, *, or /.
Okay. The re-compiled bytecode of the disassembled version works correctly.
Note that JASM disassembler supports only JVM 11- bytecodes. It will give you an error, if you try it on a JVM 11+ bytecode. For example:
herong> java -version java version "21.0.2" 2024-01-16 LTS herong> javac Hello.java herong$ jasm-0.7.0/bin/jasm -d -o out Hello.class ERROR: There were failed tasks: Hello.class : Unsupported class file major version 65
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations
JRockit JVM 28.2.7 by Oracle Corporation
JVM Bytecode and Instruction Set
►JVM Assembler/Disassembler Tools
Compare HelloWorld.j with "javap" Output
JASM Example - Calculator.jasm
AsmTools/JASM Assembly Statements
AsmTools/JASM Example - Calculator.jasm
AsmTools/JASM Disassembler: "jdis"
AsmTools/JCOD Assembly File Format
AsmTools/JCOD Example - HelloWorld.jcod
AsmTools/JCOD Disassembler: "jdec"