Comparing and Branching ifs and loops Part B. LOOPS.
-
Upload
eunice-bennett -
Category
Documents
-
view
250 -
download
0
description
Transcript of Comparing and Branching ifs and loops Part B. LOOPS.
Comparing and Branching
ifs and loopsPart B
LOOPS
Recall the forever loop
for ( ; ; ) {
…
}
lp:
…
jmp lp
while loop
while (i < 100) {
…
}
How can we do this in Assembler?
while loop
while (i < 100) {
…
}
lp:cmp eax, 100jge done ;or
jnl…jmp lp
done:
while loop
i = 0;while (i < 100) {
…
++i;}
How can we do this in Assembler?
while loop
i = 0;while (i < 100) {
…
++i;}
mov eax, 0lp:
cmp eax, 100jge done ;or jnl…inc eaxjmp lp
done:
for loop
for (int i=0; i<100; i++) {
…
}
How can we accomplish this in Assembler?
But first, when is the condition evaluated?
for loop
for (int i=0; i<100; i++) {
…
}
How can we accomplish this in Assembler?
But first, when is the condition evaluated?
Before we enter the body of the loop.
for loop
for (int i=0; i<100; i++) {
…
}
;same as the last while loop!mov eax, 0
lp:cmp eax, 100jge done ;or jnl…inc eaxjmp lp
done:
i = 0;while (i < 100) {
…++i;
}
for loop
for (int i=10; i>0; i--) {
…
}
How can we accomplish this in Assembler?
for loop
for (int i=10; i>0; i--) {
…
}
mov eax, 10lp:
cmp eax, 0jle done ;or jng…dec eaxjmp lp
done:
do-while loop
int i = 7;do {
…
i--;while (i > 2);
mov eax, 7lp:
…
dec eaxcmp eax, 2jg lp
They are the same, for a change!
LOOP instruction
• Decrements ecx.
• Loops (branches) if ecx is not equal to zero.
LOOP instruction
int i = 7;do {
…
i--;while (i != 0);
;MUST be ecxmov ecx, 7
lp:
…
loop lp
LOOP instruction
• The LOOP instruction is NOT a direct replacement for the for loop.for (int i=7; i>0; i--) {
…}
• How is it different from the for loop?
;MUST be ecxmov ecx, 7
lp:
…
loop lp
LOOP instruction
• The LOOP instruction is NOT a direct replacement for the for loop.for (int i=7; i>0; i--) {
…}
• How is it different from the for loop?1. Test is at end.2. Test is ecx != 0.
;MUST be ecxmov ecx, 7
lp:
…
loop lp
LOOP instruction
How can we do this in Assembler w/out the loop instruction?
mov ecx, 7lp:
…
loop lp
LOOP instruction
mov ecx, 7lp:
…
dec ecxjnz lp
How can we do this in Assembler w/out the loop instruction?
mov ecx, 7lp:
…
loop lpWhat do you think is faster?
LOOP instruction
mov ecx, 7lp:
…
dec ecxjnz lp
How can we do this in Assembler w/out the loop instruction?
mov ecx, 7lp:
…
loop lpWhat do you think is faster? Let’s see!
utilities
• The utilities.asm file (on the course’s software web page) contains the dump procedure and two other procedures that can be used to time instructions:– call resetTime
• resets the timer to zero and starts the timer running
– call reportTime• reports the elapsed time in milliseconds
– Only one timer can be active at a time (no nested timers).
LOOP vs. DEC/JG instruction timing
• nop, nop, dec– repeat above via LOOP (2,000,000,000 times)
• required 12,110 milliseconds total• ~6 ns each
– repeat above via DEC/JG (2,000,000,000 times)• required 4,047 milliseconds total• ~2 ns each
– Conclusion: DEC/JG is 3x faster than LOOP!
Single NOP instruction timing
• nop, nop, dec– repeat above via DEC/JG (2,000,000,000 times)
• required 4,047 milliseconds total• ~2 ns each
• nop, nop, dec– repeat above via DEC/JG (2,000,000,000 times)
• required 2,890 milliseconds total• ~1.5ns each
– Conclusion: Each nop requires ~0.5ns.
A note regarding code line labels• It becomes onerous to continually make up unique line
labels. So . . .• @@:
– Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous @@: label, and label2 is either end of code or the next @@: label. (See @B and @F.)
• @B– Refers to the location of the previous @@: label (backward).
• @F– Refers to the location of the next @@: label (forward).
A note regarding code line labels@@:
– Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous @@: label, and label2 is either end of code or the next @@: label. (See @B and @F.)
@B– Refers to the location of the
previous @@: label.
@F– Refers to the location of the next
@@: label.
mov ecx, 7lp:
…
dec ecxjnz lp
How can we accomplish this w/out lp?
A note regarding code line labels@@:
– Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous @@: label, and label2 is either end of code or the next @@: label. See @B and @F.
@B– Refers to the location of the
previous @@: label.
@F– Refers to the location of the next
@@: label.
mov ecx, 7lp:
…
dec ecxjnz lp
We can accomplish this w/out lp:mov ecx, 7
@@:…
dec ecxjnz @b
A note regarding code line labels@@:
– Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous @@: label, and label2 is either end of code or the next @@: label. See @B and @F.
@B– Refers to the location of the
previous @@: label.
@F– Refers to the location of the next
@@: label.
; for (int i=0; i<100; i++) {mov eax, 0
lp:cmp eax, 100jge done…inc eaxjmp lp
done:
How can we accomplish this w/out lp and done?
A note regarding code line labels@@:
– Defines a code label recognizable only between label1 and label2, where label1 is either start of code or the previous @@: label, and label2 is either end of code or the next @@: label. See @B and @F.
@B– The location of the previous
@@: label.
@F– The location of the next @@:
label.
; for (int i=0; i<100; i++) {mov eax, 0
lp:cmp eax, 100jge done…inc eaxjmp lp
done:We can accomplish this w/out lp and done:
mov eax, 0@@:
cmp eax, 100jge @f…inc eaxjmp @b
@@:
Quiz: Nested for loops #1
• Write the following in Assembler.for (int i=0; i<100; i++) {
for (int j=1; j<=10; j++) {…
}}
Nested for loops #2
• Write the following in Assembler.for (int i=0; i<100; i++) {
for (int j=10; j>=1; j--) {…
}}
Nested for loops #3
• Write the following in Assembler.for (int i=0; i<100; i++) {
for (int j=1; j<=i; j+=3) {…
}}
Java expressions #4
• Convert the following to Assembler:int i = 100;int j = 1 + 3 * i;
You must define i and j.
Disjunction #5
• Convert the following to Assembler:if ( f() || g() || h() ) { //disjuncts (short circuits)
a = 10;} else {b = 99;}Note(s): Assume a and b are 32-bit integers and f, g, and h are boolean functions that are already defined for you. f, g, and h return values of 1 in eax to indicate true, 0 to indicate false.
Disjunction #6
• Convert the following to Assembler:if ( f() & g() & a<100 ) { //does not disjunct (short circuit)
a = 10;} else {b = 99;}Note(s): Assume a and b are 32-bit integers and f, g, and h are boolean functions that are already defined for you. f, g, and h return values of 1 in eax to indicate true, 0 to indicate false.
Operator precedence• The closer to the top of the table an
operator appears, the higher its precedence.
• Operators with higher precedence are evaluated before operators with relatively lower precedence.
• Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
• http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html