Comparing and Branching ifs and loops Part B. LOOPS.

35
Comparing and Branching ifs and loops Part B

description

Recall the forever loop for ( ; ; ) { … } lp: … jmplp

Transcript of Comparing and Branching ifs and loops Part B. LOOPS.

Page 1: Comparing and Branching ifs and loops Part B. LOOPS.

Comparing and Branching

ifs and loopsPart B

Page 2: Comparing and Branching ifs and loops Part B. LOOPS.

LOOPS

Page 3: Comparing and Branching ifs and loops Part B. LOOPS.

Recall the forever loop

for ( ; ; ) {

}

lp:

jmp lp

Page 4: Comparing and Branching ifs and loops Part B. LOOPS.

while loop

while (i < 100) {

}

How can we do this in Assembler?

Page 5: Comparing and Branching ifs and loops Part B. LOOPS.

while loop

while (i < 100) {

}

lp:cmp eax, 100jge done ;or

jnl…jmp lp

done:

Page 6: Comparing and Branching ifs and loops Part B. LOOPS.

while loop

i = 0;while (i < 100) {

++i;}

How can we do this in Assembler?

Page 7: Comparing and Branching ifs and loops Part B. LOOPS.

while loop

i = 0;while (i < 100) {

++i;}

mov eax, 0lp:

cmp eax, 100jge done ;or jnl…inc eaxjmp lp

done:

Page 8: Comparing and Branching ifs and loops Part B. LOOPS.

for loop

for (int i=0; i<100; i++) {

}

How can we accomplish this in Assembler?

But first, when is the condition evaluated?

Page 9: Comparing and Branching ifs and loops Part B. LOOPS.

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.

Page 10: Comparing and Branching ifs and loops Part B. LOOPS.

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;

}

Page 11: Comparing and Branching ifs and loops Part B. LOOPS.

for loop

for (int i=10; i>0; i--) {

}

How can we accomplish this in Assembler?

Page 12: Comparing and Branching ifs and loops Part B. LOOPS.

for loop

for (int i=10; i>0; i--) {

}

mov eax, 10lp:

cmp eax, 0jle done ;or jng…dec eaxjmp lp

done:

Page 13: Comparing and Branching ifs and loops Part B. LOOPS.

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!

Page 14: Comparing and Branching ifs and loops Part B. LOOPS.

LOOP instruction

• Decrements ecx.

• Loops (branches) if ecx is not equal to zero.

Page 15: Comparing and Branching ifs and loops Part B. LOOPS.

LOOP instruction

int i = 7;do {

i--;while (i != 0);

;MUST be ecxmov ecx, 7

lp:

loop lp

Page 16: Comparing and Branching ifs and loops Part B. LOOPS.

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

Page 17: Comparing and Branching ifs and loops Part B. LOOPS.

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

Page 18: Comparing and Branching ifs and loops Part B. LOOPS.

LOOP instruction

How can we do this in Assembler w/out the loop instruction?

mov ecx, 7lp:

loop lp

Page 19: Comparing and Branching ifs and loops Part B. LOOPS.

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?

Page 20: Comparing and Branching ifs and loops Part B. LOOPS.

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!

Page 21: Comparing and Branching ifs and loops Part B. LOOPS.

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

Page 22: Comparing and Branching ifs and loops Part B. LOOPS.

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!

Page 23: Comparing and Branching ifs and loops Part B. LOOPS.

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.

Page 24: Comparing and Branching ifs and loops Part B. LOOPS.

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

Page 25: Comparing and Branching ifs and loops Part B. LOOPS.

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?

Page 26: Comparing and Branching ifs and loops Part B. LOOPS.

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

Page 27: Comparing and Branching ifs and loops Part B. LOOPS.

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?

Page 28: Comparing and Branching ifs and loops Part B. LOOPS.

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

@@:

Page 29: Comparing and Branching ifs and loops Part B. LOOPS.

Quiz: Nested for loops #1

• Write the following in Assembler.for (int i=0; i<100; i++) {

for (int j=1; j<=10; j++) {…

}}

Page 30: Comparing and Branching ifs and loops Part B. LOOPS.

Nested for loops #2

• Write the following in Assembler.for (int i=0; i<100; i++) {

for (int j=10; j>=1; j--) {…

}}

Page 31: Comparing and Branching ifs and loops Part B. LOOPS.

Nested for loops #3

• Write the following in Assembler.for (int i=0; i<100; i++) {

for (int j=1; j<=i; j+=3) {…

}}

Page 32: Comparing and Branching ifs and loops Part B. LOOPS.

Java expressions #4

• Convert the following to Assembler:int i = 100;int j = 1 + 3 * i;

You must define i and j.

Page 33: Comparing and Branching ifs and loops Part B. LOOPS.

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.

Page 34: Comparing and Branching ifs and loops Part B. LOOPS.

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.

Page 35: Comparing and Branching ifs and loops Part B. LOOPS.

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