Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review on Program Challenge 1 - 4 CSc3210 Yuan Long.

Similar presentations


Presentation on theme: "Review on Program Challenge 1 - 4 CSc3210 Yuan Long."— Presentation transcript:

1 Review on Program Challenge 1 - 4 CSc3210 Yuan Long

2 The purpose of program challenges Program Challenge 1: Setting up environment. Program Challenge 2: Checking bytecode file. Program Challenge 3: Basic program for addition in Jasmin. Program Challenge 4: Simple program for calculating one expression in Jasmin.

3 Program Challenge 1: Setting up environment Connecting with account in server ssh Compiling and running Java program javac *.java java * Compiling and running Jasmin program java –jar Path_Of_Jasmin.jar *.j java *

4 Program Challenge 2: Checking bytecode file Checking the hex code content in a bytecode file xxd *.class Why we use symbol “>” ? Redirect the output to a file

5 XXD

6 Program Challenge 3: Basic program for addition in Jasmin Instructions for adding two integer numbers

7 Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 ; push 12 into stack 12 stack

8 Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 bipush 5;push 5 into stack 5 12 stack

9 Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 bipush 5 iadd ;pop the top two elements form stack and push the addition result into stack 17 stack =12+5

10 Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 bipush 5 iadd 17 stack To be printed out.

11 Program Challenge 3: Basic program for adding in Jasmin Final program- plus.j.class public plus.super java/lang/Object.method public static main([Ljava/lang/String;)V.limit stack 3 getstatic java/lang/System/out Ljava/io/PrintStream; ldc "This is Yuan." invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V getstatic java/lang/System/out Ljava/io/PrintStream; bipush -22 bipush 33 iadd invokevirtual java/io/PrintStream/println(I)V return.end method

12 Program Challenge 4: Simple program for calculating expression in Jasmin To calculate an expression in Jasmin, you need to write its postfix notation first. 12 5 3 - / 3 4 * 5 1 * 1 + / + Follow the order in postfix notation, For numbers, using bipush number For operator, using corresponding instruction. E.g. iadd, idiv, imul, isub

13 Program Challenge 4: Simple program for calculating expression in Jasmin Final codes bipush 12 bipush 5 bipush 3 isub idiv bipush 3 bipush 4 imul bipush 5 bipush 1 imul bipush 1 iadd idiv iadd

14 Program Challenge 4: Simple program for calculating expression in Jasmin Task 3 3 10 / 3 10 / * 110 12 9 * - * It is easy to write a Jasmin program for it. But for division bipush 10 bipush 3 idiv Result=3 fpush 10 fpush 3 fdiv Result=3.333 bipush 10 bipush 3 fdiv Result=3 Not precise Wrong. More precise.

15 Program Challenge 4: Simple program for calculating expression in Jasmin Task 3 3 10 / 3 10 / * 110 12 9 * - * It is easy to write a Jasmin program for it. But for division bipush 10 bipush 3 idiv Result=3 Fpush ldc 10 Fpush ldc 3 fdiv Result=3.333 bipush 10 bipush 3 fdiv Result=3 Not precise Wrong. More precise. However, there is no fpush in JVM. You can use ldc instead.

16 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack

17 Program Challenge 5 Program counter Contains the address for instruction currently being executed. How to control the execution flow of program Set PC’s content depending on condition PC next =PC+Offset if_icmpxx branch_instruction ifxx branch_instruction Condition true  PC next =PC+ Offset_Of_branch_Instruction Condition false  PC next =PC+ X (number of bytes for current instruction) Set PC’s content directly without condition PC next =PC+Offset goto branch_instruction PCnext=PC+ Offset_Of_branch_Instruction

18 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a00

19 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC+1 PC=0x1a01

20 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a01

21 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC+2 PC=0x1a03

22 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a03

23 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions x Local Variable Array Stack PC+? PC=? x<=-1?

24 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 0 Local Variable Array Stack PC+5 PC=0x1a08 x=0 x<=-1 is true

25 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a08

26 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 5 Local Variable Array Stack PC+3 PC=0x1a06 x=5 x<=-1 is false

27 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a06

28 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 0 5 Local Variable Array Stack PC+1 PC=0x1a07

29 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a08

30 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 0 Local Variable Array Stack PC+1 PC=0x1a08

31 Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a08

32 Program Challenge 5 goto branch_instruction iload_1 bipush -1 goto FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC=0x1a03

33 Program Challenge 5 goto branch_instruction iload_1 bipush -1 goto FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC+5 PC=0x1a08


Download ppt "Review on Program Challenge 1 - 4 CSc3210 Yuan Long."

Similar presentations


Ads by Google