Review on Program Challenge CSc3210 Yuan Long
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.
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 *
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
XXD
Program Challenge 3: Basic program for addition in Jasmin Instructions for adding two integer numbers
Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 ; push 12 into stack 12 stack
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
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
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.
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
Program Challenge 4: Simple program for calculating expression in Jasmin To calculate an expression in Jasmin, you need to write its postfix notation first / 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
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
Program Challenge 4: Simple program for calculating expression in Jasmin Task / 3 10 / * * - * 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.
Program Challenge 4: Simple program for calculating expression in Jasmin Task / 3 10 / * * - * 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.
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
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
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
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
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
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
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
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?
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
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
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
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
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
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
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
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
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
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