Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into...

34
Lab 9 Java Bytecode & The Jasmin Assembler

Transcript of Lab 9 Java Bytecode & The Jasmin Assembler. Java Bytecode javac compiles your java source code into...

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

• 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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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;

} }