Upload
hugo-hensley
View
218
Download
0
Embed Size (px)
Citation preview
Comparing and Branching
ifs and loopspart 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.
JMP instruction
JMP instruction
Consider the forever loop:
for ( ; ; ) {…
}
How can we accomplish this in Assembler?
JMP instruction
Consider the forever loop:
for ( ; ; ) {…
}
lp:…jmp lp
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.
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.
CMP instruction
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.
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.
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.
Jcc instructions (jump/branch conditionally)
Note: je and jz are exactly the same but are provided for readability.
Note: JGE and JNL are exactly the same but are provided for readability.
Most useful Jcc’s
• JE / JZ• JG• JGE• JL• JLE• JNE / JNZ
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?
Comparing and branching
Consider a few ifs in Java:
if (x == 1) {…
}
Comparing and branching
Consider a few ifs in Java:
if (x == 1) {…
}
cmp x, 1jne more…
more:
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.
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.
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:
Comparing and branching
Consider a few ifs in Java:
if (x < 5) {…
} else {…
}
Comparing and branching
Consider a few ifs in Java:
if (x < 5) {…
} else {…
}
One possible solution:
cmp x, 5jnl else1…jmp end1
else1:…
end1:
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:
Comparing and branching
Consider a few ifs in Java:
if (x == 1) {…
} else if (x >= 1000) {…
} else if (x >= 100) {…
} else {…
}
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:
Comparing and branching
Consider a few ifs in Java:
if (x > 2 && x <= 5) {…
} else {…
}
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:
Comparing and branching
Consider a few ifs in Java:
if (x < 5 || y > 2) {…
} else {…
}
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:
Comparing and branching
Consider a few ifs in Java:
if (x != y) {…
} else {…
}
(Hint: We don’t have cmp m32, m32!)
Comparing and branching
Consider a few ifs in Java:
if (x != y) {…
} else {…
}
mov eax, xcmp eax, yje el…jmp done
el:…
done:
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:
ADVANCED TOPIC: DISJUNCTIONDanger, Will Robinson!
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)…
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.”
Disjunction
1. How can we demonstrate that this is true in Java?
2. How can we implement 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.
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?
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?
DISASSEMBLY, DISJUNCTION, AND THE JVM
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/.
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
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
NEXT TOPIC: LOOPS