45
Comparing and Branching ifs and loops part A

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

Embed Size (px)

Citation preview

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

Comparing and Branching

ifs and loopspart A

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

JMP instruction

Consider the forever loop:

for ( ; ; ) {…

}

How can we accomplish this in Assembler?

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

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.

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

JMP instruction

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

JMP instruction

Consider the forever loop:

for ( ; ; ) {…

}

How can we accomplish this in Assembler?

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

JMP instruction

Consider the forever loop:

for ( ; ; ) {…

}

lp:…jmp lp

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

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.

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

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.

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

CMP instruction

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

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.

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

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.

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

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.

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

Jcc instructions (jump/branch conditionally)

Note: je and jz are exactly the same but are provided for readability.

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

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

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

Most useful Jcc’s

• JE / JZ• JG• JGE• JL• JLE• JNE / JNZ

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

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?

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

}

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

}

cmp x, 1jne more…

more:

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

}

cmp x, 1jne more…

more:Jumps (takes the branch) only when ZF=0.

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

}

cmp x, 1jne more…

more:Jumps (takes the branch) only when ZF=0.

jnz also takes the branch when ZF=0.

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

}

cmp x, 1jne more…

more:

Avoid this:

cmp x, 1je doItjne more

doIt:…

more:

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

Comparing and branching

Consider a few ifs in Java:

if (x < 5) {…

} else {…

}

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

Comparing and branching

Consider a few ifs in Java:

if (x < 5) {…

} else {…

}

One possible solution:

cmp x, 5jnl else1…jmp end1

else1:…

end1:

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

Comparing and branching

Consider a few ifs in Java:

if (x < 5) {…

} else {…

}

Another possible solution:

cmp x, 5jge else1

;same!…jmp end1

else1:…

end1:

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

} else if (x >= 1000) {…

} else if (x >= 100) {…

} else {…

}

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

Comparing and branching

Consider a few ifs in Java:

if (x == 1) {…

} else if (x >= 1000) {…

} else if (x >= 100) {…

} else {…

}

cmp x, 1jne elif1…jmp done

elif1:cmp x, 1000jl elif2 ; or jnge…jmp done

elif2:cmp x, 100jl el1 ;or jnge…jmp done

el1:…

done:

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

Comparing and branching

Consider a few ifs in Java:

if (x > 2 && x <= 5) {…

} else {…

}

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

Comparing and branching

Consider a few ifs in Java:

if (x > 2 && x <= 5) {…

} else {…

}

cmp x, 2jle elcmp x, 5jg el…jmp done

el:…

done:

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

Comparing and branching

Consider a few ifs in Java:

if (x < 5 || y > 2) {…

} else {…

}

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

Comparing and branching

Consider a few ifs in Java:

if (x < 5 || y > 2) {…

} else {…

}

cmp x, 5jl yescmp y, 2jle no

yes:…jmp done

no:…

done:

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

Comparing and branching

Consider a few ifs in Java:

if (x != y) {…

} else {…

}

(Hint: We don’t have cmp m32, m32!)

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

Comparing and branching

Consider a few ifs in Java:

if (x != y) {…

} else {…

}

mov eax, xcmp eax, yje el…jmp done

el:…

done:

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

Avoid double jumps!if (x>10) {

i = 12;} else {

i = 9;}correct:

cmp x, 10jle nomov i, 12jmp done

no:mov i, 9

done:

wrong:cmp x, 10jg yesjmp no

yes:mov i, 12jmp done

no:mov i, 9jmp done

done:

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

ADVANCED TOPIC: DISJUNCTIONDanger, Will Robinson!

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

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, orare they different?

if (a | b)…

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

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

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

Disjunction

1. How can we demonstrate that this is true in Java?

2. How can we implement this in Assembler?

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

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.

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

Disjunctionprivate 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?

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

Disjunctionprivate 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?

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

DISASSEMBLY, DISJUNCTION, AND THE JVM

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

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

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

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

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

Disjunctionprivate 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

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

NEXT TOPIC: LOOPS