JVM Tutorials - Herong's Tutorial Examples - v5.14, by Herong Yang
JASM Example - Calculator.jasm
This section provides a tutorial example on how to write a simple calculator in JASM: Calculator.jasm.
Now re-write our Jasmin program, Calculator.j in JASM: Calculator.jasm.
1. JASM version: Calculator.jasm - Write the JASM version by following the Jasmin version:
/* Calculator.jasm
* Copyright (c) HerongYang.com. All Rights Reserved.
*/
public class Calculator {
public static main([java/lang/String)V {
// define a try-catch block
try {
aload 0 // load address from variable 0, the first argument: args
arraylength // get args.length
bipush 3 // load int 3
if_icmpeq Parse // skip error messages, if args.length == 3
// print out error messages
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
Parse:
// parse the args[0] as a double value
aload 0 // load args address
iconst 0 // load index 0
aaload // load args[0]
invokestatic java/lang/Double.parseDouble(java/lang/String)D
dstore 1 // store it in variable 1 and 2 as x, double requires 2 slots
aload 0 // load args address
iconst 1 // load index 1
aaload // load args[1]
iconst 0 // load index 0
invokevirtual java/lang/String.charAt(I)C
istore 3 // store it in variable 3 as op
aload 0 // load args address
iconst 2 // load index 2
aaload // load args[2]]
invokestatic java/lang/Double.parseDouble(java/lang/String)D
dstore 4 // store it in variable 4 and 5 as y, double requires 2 slots
} catch (java/lang/NumberFormatException) {
// When an exception hits, the exception object reference is pushed onto the stack.
// We pop it to clear the stack, then print our custom error.
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
}
iload 3 // load op from variable 3
lookupswitch InvalidOp {
42: DoMul, // '*' is ASCII 42
43: DoAdd, // '+' is ASCII 43
45: DoSub, // '-' is ASCII 45
47: DoDiv // '/' is ASCII 47
}
DoAdd: // load x and y, perform addition, store r
dload 1
dload 4
dadd
dstore 6
goto PrintResult
DoSub: // load x and y, perform subtraction, store r
dload 1
dload 4
dsub
dstore 6
goto PrintResult
DoMul: // load x and y, perform multiplication, store r
dload 1
dload 4
dmul
dstore 6
goto PrintResult
DoDiv: // load y, make sure it's not 0.0
dload 4 // load y
dconst 0 // load 0.0
dcmpl // compare and generate -1, 0 or 1
ifeq DivZero // go to error handling, if 0
dload 1 // load x and y, perform division, store r
dload 4
ddiv
dstore 6
goto PrintResult
PrintResult:
getstatic java/lang/System.out java/io/PrintStream
dload 6
invokevirtual java/io/PrintStream.println(D)V
return
InvalidOp:
getstatic java/lang/System.out java/io/PrintStream
ldc "Error: Invalid operator. Use +, -, *, or /."
invokevirtual java/io/PrintStream.println(java/lang/String)V
return
DivZero:
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
}
}
2. Test - Compile it with JASM and test it.
herong> ./jasm-0.7.0/bin/jasm Calculator.jasm herong> java Calculator 0.9999 + 0.0001 1.0 herong> java Calculator 0.9999 - 0.0001 0.9998 herong> java Calculator 0.9999 '*' 0.0001 9.999000000000001E-5 herong> java Calculator 0.9999 / 2 0.49995 herong> java Calculator 0.9999 2 Error: Invalid number of arguments. Usage: java Calculator <number> <operator> <number> herong> java Calculator 0.9999 / two Error: Please provide valid numeric values. herong> java Calculator 0.9999 % 2 Error: Invalid operator. Use +, -, *, or /. herong> java Calculator 0.9999 / 0 Error: Division by zero is not allowed.
Cool! Calculator.jasm seems to be working correctly.
3. Compare Bytecode - Now let's compare the bytecodes generated from Calculator.jasm (JASM version) with Calculator.j (Jasmin version) and Calculator.java (Java version):
herong> ./jasm-0.7.0/bin/jasm Calculator.jasm herong> ls -l Calculator.class -rw-r--r-- 1 herong staff 968 Nov 11 16:04 Calculator.class herong> java -jar jasmin-2.4/jasmin.jar Calculator.j Generated: Calculator.class herong> ls -l Calculator.class -rw-r--r-- 1 herong staff 943 Nov 10 20:54 Calculator.class herong> javac Calculator.java herong> ls Calculator.* -rw-r--r-- 1 herong staff 1375 Nov 10 21:06 Calculator.class
The bytecode generated from the JASM version has almost the same size as the Jasmin version. They both are about 30% smaller than the Java version.
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"