Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into...
-
Upload
percival-shelton -
Category
Documents
-
view
246 -
download
1
Transcript of Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into...
Lab 9Java 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 AreaHeader Area
Field AreaMethod 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 return.end method
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;
} }
• Create the file simple.j and write the instructions.
Example
.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 ldc 0 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 ldc 0 istore 50 ; storage for a dummy integer for ; reading it by read
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
1 0
2
..
..
50 0
51
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
49 0
1 0
2
..
..
50 0
51
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
49 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
10
49
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
Ascii for LF
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
39 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
49 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
32
49
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
Ascii for Space
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
17 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
49 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
48
49
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
1 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
10
1
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
10
1
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
1
0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
1 0
1 0
2
..
..
50 0
51 49
..
99
Stack
Locals
ExampleLabel1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 iload 51 ldc 32 isub ifeq Label2 iload 51 ldc 48 isub ldc 10 iload 50 imul iadd istore 50 goto Label1
0
1 0
2
..
..
50 1
51 49
..
99
Stack
Locals
ExampleLabel2: ; 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 iload 1 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;
} }