Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comparing and Branching ifs and loops part A. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

Similar presentations


Presentation on theme: "Comparing and Branching ifs and loops part A. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?"— Presentation transcript:

1 Comparing and Branching ifs and loops part A

2 JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

3 JMP instruction jump/branch unconditionally (always) Transfers program control to a different point in the instruction stream without recording return information. The destination (target) operand specifies the address of the instruction being jumped to. This operand can be an immediate value, a general-purpose register, or a memory location.

4 JMP instruction

5 Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

6 JMP instruction Consider the forever loop: for ( ; ; ) { … } lp: … jmplp

7 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } if (x < 5) { … } else { … } if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … } We need to develop a technique to accomplish this in Assembler.

8 CMP instruction Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction. Operation: – temp ← SRC1 − SignExtend(SRC2); – ModifyStatusFlags; (* Modify status flags in the same manner as the SUB instruction*) Flags Affected: – The CF, OF, SF, ZF, AF, and PF flags are set according to the result.

9 CMP instruction

10 Jcc instructions (jump/branch conditionally) Checks the state of one or more of the status flags in the EFLAGS register (CF, OF, PF, SF, and ZF) and, if the flags are in the specified state (condition), performs a jump to the target instruction specified by the destination operand.

11 Jcc instructions (jump/branch conditionally) A condition code (cc) is associated with each instruction to indicate the condition being tested for. If the condition is not satisfied, the jump is not performed and execution continues with the instruction following the Jcc instruction.

12 Jcc instructions (jump/branch conditionally) Notes: – The terms “less” and “greater” are used for comparisons of signed integers. – The terms “above” and “below” are used for unsigned integers.

13 Jcc instructions (jump/branch conditionally) Note: je and jz are exactly the same but are provided for readability.

14 Note: JGE and JNL are exactly the same but are provided for readability.

15 Most useful Jcc’s JE / JZ JG JGE JL JLE JNE / JNZ

16 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } if (x < 5) { … } else { … } if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … } So how can we code each of these in assembler?

17 Comparing and branching Consider a few ifs in Java: if (x == 1) { … }

18 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more:

19 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more: Jumps (takes the branch) only when ZF=0.

20 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more: Jumps (takes the branch) only when ZF=0. jnz also takes the branch when ZF=0.

21 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmpx, 1 jnemore … more: Avoid this: cmpx, 1 jedoIt jnemore doIt: … more:

22 Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … }

23 Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … } One possible solution: cmpx, 5 jnlelse1 … jmpend1 else1: … end1:

24 Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … } Another possible solution: cmpx, 5 jgeelse1;same! … jmpend1 else1: … end1:

25 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … }

26 Comparing and branching Consider a few ifs in Java: if (x == 1) { … } else if (x >= 1000) { … } else if (x >= 100) { … } else { … } cmpx, 1 jneelif1 … jmpdone elif1: cmpx, 1000 jlelif2; or jnge … jmpdone elif2: cmpx, 100 jlel1;or jnge … jmpdone el1: … done:

27 Comparing and branching Consider a few ifs in Java: if (x > 2 && x <= 5) { … } else { … }

28 Comparing and branching Consider a few ifs in Java: if (x > 2 && x <= 5) { … } else { … } cmpx, 2 jleel cmpx, 5 jgel … jmpdone el: … done:

29 Comparing and branching Consider a few ifs in Java: if (x 2) { … } else { … }

30 Comparing and branching Consider a few ifs in Java: if (x 2) { … } else { … } cmpx, 5 jlyes cmpy, 2 jleno yes: … jmpdone no: … done:

31 Comparing and branching Consider a few ifs in Java: if (x != y) { … } else { … } (Hint: We don’t have cmp m32, m32!)

32 Comparing and branching Consider a few ifs in Java: if (x != y) { … } else { … } moveax, x cmpeax, y jeel … jmpdone el: … done:

33 Avoid double jumps! if (x>10){ i = 12; } else { i = 9; } correct: cmpx, 10 jleno movi, 12 jmpdone no: movi, 9 done: wrong: cmpx, 10 jgyes jmpno yes: movi, 12 jmpdone no: movi, 9 jmpdone done:

34 ADVANCED TOPIC: DISJUNCTION Danger, Will Robinson!

35 Disjunction Java (and other languages as well) support a variety of similar boolean operators: if (a && b)… if (a & b)… if (a || b)…Are they exactly the same, or are they different? if (a | b)…

36 Disjunction http://java.sun.com/docs/books/jls/third_edit ion/html/expressions.html#15.23 http://java.sun.com/docs/books/jls/third_edit ion/html/expressions.html#15.23 – “The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.” – “The || operator is like | (§15.22.2), but evaluates its right-hand operand only if the value of its left- hand operand is false.”

37 Disjunction 1.How can we demonstrate that this is true in Java? 2.How can we implement this in Assembler?

38 Disjunction public static void main ( String[] s ) { if ( true || false ) { System.out.println( "main: in first if" ); } if ( true | false ) { System.out.println( "main: in second if" ); } These are the cases we wish to test. But we need more.

39 Disjunction private static boolean T ( ) { System.out.println( "T()" ); return true; } private static boolean F ( ) { System.out.println( "F()" ); return false; } public static void main ( String[] s ) { if ( true || false ) { System.out.println( "main: in first if" ); } if ( true | false ) { System.out.println( "main: in second if" ); } How can these functions help?

40 Disjunction private static boolean T ( ) { System.out.println( "T()" ); return true; } private static boolean F ( ) { System.out.println( "F()" ); return false; } public static void main ( String[] s ) { if ( T() || F() ) { System.out.println( "main: in first if" ); } if ( T() | F() ) { System.out.println( "main: in second if" ); } How can these functions help?

41 DISASSEMBLY, DISJUNCTION, AND THE JVM

42 Disjunction Let’s look at some JVM (Java Virtual Machine) code. javap (see http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html) run on a.class file will disassemble it for us into JVM code.http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html javap –c test The JVM spec can be found here: http://java.sun.com/docs/books/jvms/.http://java.sun.com/docs/books/jvms/

43 Disjunction public static void main(java.lang.String[]); Code: 0: invokestatic #6;//Method T:()Z 3: ifne 12//br if true 6: invokestatic #7;//Method F:()Z 9: ifeq 20//br if false 12: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 15: ldc #8;//String main: in first if 17: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: invokestatic #6;//Method T:()Z 23: invokestatic #7;//Method F:()Z 26: ior 27: ifeq 38//br if false 30: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 33: ldc #9;//String main: in second if 35: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 38: return

44 Disjunction private static boolean T ( ) { System.out.println( "T()" ); return true; } private static boolean F ( ) { System.out.println( "F()" ); return false; } public static void main ( String[] s ) { if ( T() || F() ) { System.out.println( "main: in first if" ); } if ( T() | F() ) { System.out.println( "main: in second if" ); } public static void main(java.lang.String[]); Code: 0: invokestatic #6;//Method T:()Z 3: ifne 12 6: invokestatic #7;//Method F:()Z 9: ifeq 20 12: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 15: ldc #8;//String main: in first if 17: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: invokestatic #6;//Method T:()Z 23: invokestatic #7;//Method F:()Z 26: ior 27: ifeq 38 30: getstatic #2;//Field java/lang/System.out:Ljava/io/PrintStream; 33: ldc #9;//String main: in second if 35: invokevirtual #4;//Method java/io/PrintStream.println:(Ljava/lang/String;)V 38: return

45 NEXT TOPIC: LOOPS


Download ppt "Comparing and Branching ifs and loops part A. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?"

Similar presentations


Ads by Google