comparing and branching
DESCRIPTION
Comparing and Branching. if s and loops Part B. loops. Recall the forever loop. for ( ; ; ) { … }. lp: … jmplp. while loop. while (i < 100) { … }. How can we do this in Assembler?. while loop. while (i < 100) { … }. lp: cmpeax, 100 jgedone;or jnl … - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/1.jpg)
Comparing and Branching
ifs and loopsPart B
![Page 2: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/2.jpg)
LOOPS
![Page 3: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/3.jpg)
Recall the forever loop
for ( ; ; ) {
…
}
lp:
…
jmp lp
![Page 4: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/4.jpg)
while loop
while (i < 100) {
…
}
How can we do this in Assembler?
![Page 5: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/5.jpg)
while loop
while (i < 100) {
…
}
lp:cmp eax, 100jge done ;or
jnl…jmp lp
done:
![Page 6: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/6.jpg)
while loop
i = 0;while (i < 100) {
…
++i;}
How can we do this in Assembler?
![Page 7: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/11.jpg)
for loop
for (int i=10; i>0; i--) {
…
}
How can we accomplish this in Assembler?
![Page 12: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/14.jpg)
LOOP instruction
• Decrements ecx.
• Loops (branches) if ecx is not equal to zero.
![Page 15: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/15.jpg)
LOOP instruction
int i = 7;do {
…
i--;while (i != 0);
;MUST be ecxmov ecx, 7
lp:
…
loop lp
![Page 16: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/18.jpg)
LOOP instruction
How can we do this in Assembler w/out the loop instruction?
mov ecx, 7lp:
…
loop lp
![Page 19: Comparing and Branching](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56813291550346895d9927ec/html5/thumbnails/35.jpg)
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