Download presentation
Presentation is loading. Please wait.
Published byEunice Bennett Modified over 9 years ago
1
Comparing and Branching ifs and loops Part B
2
LOOPS
3
Recall the forever loop for ( ; ; ) { … } lp: … jmplp
4
while loop while (i < 100) { … } How can we do this in Assembler?
5
while loop while (i < 100) { … } lp: cmpeax, 100 jgedone;or jnl … jmplp done:
6
while loop i = 0; while (i < 100) { … ++i; } How can we do this in Assembler?
7
while loop i = 0; while (i < 100) { … ++i; } moveax, 0 lp: cmpeax, 100 jgedone;or jnl … inceax jmplp done:
8
for loop for (int i=0; i<100; i++) { … } How can we accomplish this in Assembler? But first, when is the condition evaluated?
9
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.
10
for loop for (int i=0; i<100; i++) { … } ;same as the last while loop! moveax, 0 lp: cmpeax, 100 jgedone;or jnl … inceax jmplp done: i = 0; while (i < 100) { … ++i; }
11
for loop for (int i=10; i>0; i--) { … } How can we accomplish this in Assembler?
12
for loop for (int i=10; i>0; i--) { … } moveax, 10 lp: cmpeax, 0 jledone;or jng … deceax jmplp done:
13
do-while loop int i = 7; do { … i--; while (i > 2); moveax, 7 lp: … deceax cmpeax, 2 jglp They are the same, for a change!
14
LOOP instruction Decrements ecx. Loops (branches) if ecx is not equal to zero.
15
LOOP instruction int i = 7; do { … i--; while (i != 0); ;MUST be ecx movecx, 7 lp: … looplp
16
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 ecx movecx, 7 lp: … looplp
17
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 ecx movecx, 7 lp: … looplp
18
LOOP instruction How can we do this in Assembler w/out the loop instruction? movecx, 7 lp: … looplp
19
LOOP instruction movecx, 7 lp: … dececx jnzlp How can we do this in Assembler w/out the loop instruction? movecx, 7 lp: … looplp What do you think is faster?
20
LOOP instruction movecx, 7 lp: … dececx jnzlp How can we do this in Assembler w/out the loop instruction? movecx, 7 lp: … looplp What do you think is faster? Let’s see!
21
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).
22
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!
23
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.
24
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).
25
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. movecx, 7 lp: … dececx jnzlp How can we accomplish this w/out lp?
26
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. movecx, 7 lp: … dececx jnzlp We can accomplish this w/out lp: movecx, 7 @@: … dececx jnz@b
27
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++) { moveax, 0 lp: cmpeax, 100 jgedone … inceax jmplp done: How can we accomplish this w/out lp and done?
28
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++) { moveax, 0 lp: cmpeax, 100 jgedone … inceax jmplp done: We can accomplish this w/out lp and done: moveax, 0 @@: cmpeax, 100 jge@f … inceax jmp@b @@:
29
Quiz: Nested for loops #1 Write the following in Assembler. for (int i=0; i<100; i++) { for (int j=1; j<=10; j++) { … }
30
Nested for loops #2 Write the following in Assembler. for (int i=0; i<100; i++) { for (int j=10; j>=1; j--) { … }
31
Nested for loops #3 Write the following in Assembler. for (int i=0; i<100; i++) { for (int j=1; j<=i; j+=3) { … }
32
Java expressions #4 Convert the following to Assembler: int i = 100; int j = 1 + 3 * i; You must define i and j.
33
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.
34
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.
35
Operator precedence The closer to the top of the table an operator appears, the higher its 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 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. 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 http://download.oracle.com/javase/ tutorial/java/nutsandbolts/operators.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.