html5-img
1 / 45

Comparing and Branching

Comparing and Branching. if s and loops part A. JMP instruction. Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?. JMP instruction. jump/branch unconditionally (always)

dafydd
Download Presentation

Comparing and Branching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. JMP instruction Consider the forever loop: for ( ; ; ) { … } How can we accomplish this in Assembler?

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

  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) { … } cmp x, 1 jne more … more:

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

  20. Comparing and branching Consider a few ifs in Java: if (x == 1) { … } cmp x, 1 jne more … 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) { … } cmp x, 1 jne more … more: Avoid this: cmp x, 1 je doIt jne more 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: cmp x, 5 jnl else1 … jmp end1 else1: … end1:

  24. Comparing and branching Consider a few ifs in Java: if (x < 5) { … } else { … } Another possible solution: cmp x, 5 jge else1 ;same! … jmp end1 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 { … } cmp x, 1 jne elif1 … jmp done elif1: cmp x, 1000 jl elif2 ; or jnge … jmp done elif2: cmp x, 100 jl el1 ;or jnge … jmp done 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 { … } cmp x, 2 jle el cmp x, 5 jg el … jmp done el: … done:

  29. Comparing and branching Consider a few ifs in Java: if (x < 5 || y > 2) { … } else { … }

  30. Comparing and branching Consider a few ifs in Java: if (x < 5 || y > 2) { … } else { … } cmp x, 5 jl yes cmp y, 2 jle no yes: … jmp done 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 { … } mov eax, x cmp eax, y je el … jmp done el: … done:

  33. Avoid double jumps! if (x>10) { i = 12; } else { i = 9; } correct: cmp x, 10 jle no mov i, 12 jmp done no: mov i, 9 done: wrong: cmp x, 10 jg yes jmp no yes: mov i, 12 jmp done no: mov i, 9 jmp done done:

  34. Danger, Will Robinson! Advanced topic: Disjunction

  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_edition/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 • How can we demonstrate that this is true in Java? • 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. javap –c test • The JVM spec can be found here: 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

More Related