lab 9 java bytecode & the jasmin assembler. java bytecode javac compiles your java source code...
TRANSCRIPT
![Page 1: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/1.jpg)
Lab 9Java Bytecode & The Jasmin Assembler
![Page 2: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/2.jpg)
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.
![Page 3: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/3.jpg)
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
![Page 4: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/4.jpg)
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“
![Page 5: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/5.jpg)
Using Jasmin
• Write your program using the bytecode instructions
• Assemble the code using jasmin to generate class files
• Execute the class files using java
![Page 6: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/6.jpg)
Jasmin Code Structure
Comment AreaHeader Area
Field AreaMethod Area
![Page 7: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/7.jpg)
Jasmin Code Structure
• Comment Area
• Header Area
; Classfile version: ; Major: 49 ; Minor: 0
.source MyApp.java
.class public MyApp
.super java/lang/Object
![Page 8: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/8.jpg)
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
![Page 9: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/9.jpg)
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;
} }
![Page 10: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/10.jpg)
• 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
![Page 11: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/11.jpg)
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
![Page 12: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/12.jpg)
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
![Page 13: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/13.jpg)
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
![Page 14: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/14.jpg)
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
![Page 15: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/15.jpg)
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
![Page 16: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/16.jpg)
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
![Page 17: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/17.jpg)
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
![Page 18: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/18.jpg)
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
![Page 19: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/19.jpg)
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
![Page 20: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/20.jpg)
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
![Page 21: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/21.jpg)
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
![Page 22: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/22.jpg)
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
![Page 23: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/23.jpg)
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
![Page 24: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/24.jpg)
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
![Page 25: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/25.jpg)
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
![Page 26: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/26.jpg)
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
![Page 27: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/27.jpg)
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
![Page 28: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/28.jpg)
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
![Page 29: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/29.jpg)
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
![Page 30: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/30.jpg)
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
![Page 31: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/31.jpg)
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
![Page 32: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/32.jpg)
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
![Page 33: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/33.jpg)
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
![Page 34: Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d365503460f94a0dc86/html5/thumbnails/34.jpg)
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;
} }