Lab 9 Java Bytecode & The Jasmin Assembler
Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode. JVM (Java Virtual Machine) is a machine that interfaces with the hardware to run the byte code instructions.
Java Bytecode JVM can be used as an interface to other programming languages that translates to the byte code instruction set But, it is hard to translate to a class file which is a binary file
Jasmin Jasmin is a Java Assembler that can translate Bytecode instructions to a binary class You can download jasmin from http://sourceforge.net/projects/jasmin/files/jasmin/jasmin-2.4/jasmin-2.4.zip/download. $ sudo mv jasmin.jar /usr/local/lib $ export CLASSPATH="/usr/local/lib/jasmin.jar:$CLASSPATH“
Using Jasmin Write your program using the bytecode instructions Assemble the code using jasmin to generate class files Execute the class files using java
Jasmin Code Structure Comment Area Header Area Field Area Method Area
Jasmin Code Structure Comment Area Header Area ; Classfile version: ; Major: 49 ; Minor: 0 .source MyApp.java .class public MyApp .super java/lang/Object
Jasmin Code Structure Field Area Method Area .field public static x I .method public <init>()V aload_0 invokenonvirtual java/lang/Object/<init>()V return .end method .method public static main([Ljava/lang/String;)V .limit stack 10 .limit locals 10
Example Our task is to write the following program in bytecode instructions, public class simple { public static int main(String[] args){ int x = 0; x = System.in.read(); x = x + 3; System.out.println(x); return 0; }
Example Create the file simple.j and write the instructions. .class public simple .super java/lang/Object .method public <init>()V aload_0 invokespecial java/lang/Object/<init>()V return .end method
Example .method public static main([Ljava/lang/String;)V .limit stack 5 .limit locals 100 ldc 0 istore 1 ; initialize x to zero and store it ; in local variable 1 ; the read function starts at this point istore 50 ; storage for a dummy integer for ; reading it by read
Example .method public static main([Ljava/lang/String;)V .limit stack 5 .limit locals 100 ldc 0 istore 1 ; initialize x to zero and store it ; in local variable 1 ; the read function starts at this point istore 50 ; storage for a dummy integer for ; reading it by read
Example 1 2 .. 50 51 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 99 Stack Locals
Example 49 1 2 .. 50 51 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 99 Stack Locals
Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals
Example 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 49 99 Stack Locals
Example Ascii for LF 10 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 Ascii for LF 10 49 1 2 .. 50 51 49 99 Stack Locals
Example 39 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 39 1 2 .. 50 51 49 99 Stack Locals
Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals
Example 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 49 99 Stack Locals
Example 32 49 1 2 .. 50 51 49 99 Ascii for Space Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 32 49 1 2 .. 50 51 49 99 Ascii for Space Stack Locals
Example 17 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 17 1 2 .. 50 51 49 99 Stack Locals
Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals
Example 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 49 99 Stack Locals
Example 48 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 48 49 1 2 .. 50 51 49 99 Stack Locals
Example 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 1 2 .. 50 51 49 99 Stack Locals
Example 10 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 10 1 1 2 .. 50 51 49 99 Stack Locals
Example 10 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 10 1 1 2 .. 50 51 49 99 Stack Locals
Example 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 1 2 .. 50 51 49 99 Stack Locals
Example 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 1 2 .. 50 51 49 99 Stack Locals
Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals
Example Label2: ; now our dummy integer contains the ; integer read from the keyboard iload 50 ; read function ends here istore 1 ; store this value in x iload 1 ldc 3 iadd istore 1 ; x=x+3 getstatic java/lang/System/out Ljava/io/PrintStream; swap invokevirtual java/io/PrintStream/println(I)V ; print x return ; return from main .end method
Example Use jasmin to translate the simple.j file to the simple.class binary file $ java -cp $CLASSPATH jasmin.Main simple.j Or, $ java –jar jasmin.jar simple.j Execute the class file $ java simple
Activity Write the equivalent bytecode instructions for the following class and use jasmin to generate the class file. public class simple { public static int main(String[] args){ int x = 0; int y = 5; x = System.in.read(); x = x * y - 2; System.out.println(x); return 0; }