Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 9 Java Bytecode & The Jasmin Assembler

Similar presentations


Presentation on theme: "Lab 9 Java Bytecode & The Jasmin Assembler"— Presentation transcript:

1 Lab 9 Java Bytecode & The Jasmin Assembler

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

3 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

4 Jasmin Jasmin is a Java Assembler that can translate Bytecode instructions to a binary class You can download jasmin from $ sudo mv jasmin.jar /usr/local/lib $ export CLASSPATH="/usr/local/lib/jasmin.jar:$CLASSPATH“

5 Using Jasmin Write your program using the bytecode instructions
Assemble the code using jasmin to generate class files Execute the class files using java

6 Jasmin Code Structure Comment Area Header Area Field Area Method Area

7 Jasmin Code Structure Comment Area Header Area ; Classfile version:
; Major: 49 ; Minor: 0 .source MyApp.java .class public MyApp .super java/lang/Object

8 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

9 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; }

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 Example Label2: ; now our dummy integer contains the
; integer read from the keyboard iload ; read function ends here istore ; store this value in x iload 1 ldc 3 iadd istore ; 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

33 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

34 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; }


Download ppt "Lab 9 Java Bytecode & The Jasmin Assembler"

Similar presentations


Ads by Google