1 / 35

Comparing and Branching

Comparing and Branching. if s and loops Part 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, 100 jge done ;or jnl …

Download Presentation

Comparing and Branching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comparing and Branching ifs and loops Part B

  2. loops

  3. Recall the forever loop for ( ; ; ) { … } lp: … jmp lp

  4. while loop while (i < 100) { … } How can we do this in Assembler?

  5. while loop while (i < 100) { … } lp: cmp eax, 100 jge done ;or jnl … jmp lp 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; } mov eax, 0 lp: cmp eax, 100 jge done ;or jnl … inc eax jmp lp 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! mov eax, 0 lp: cmp eax, 100 jge done ;or jnl … inc eax jmp lp 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--) { … } mov eax, 10 lp: cmp eax, 0 jle done ;or jng … dec eax jmp lp done:

  13. do-while loop int i = 7; do { … i--; while (i > 2); mov eax, 7 lp: … dec eax cmp eax, 2 jg lp 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 mov ecx, 7 lp: … loop lp

  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 mov ecx, 7 lp: … loop lp

  17. LOOP instruction • The LOOP instruction is NOT a direct replacement for the for loop. for (inti=7; i>0; i--) { … } • How is it different from the for loop? • Test is at end. • Test is ecx != 0. ;MUST be ecx mov ecx, 7 lp: … loop lp

  18. LOOP instruction How can we do this in Assembler w/out the loop instruction? mov ecx, 7 lp: … loop lp

  19. LOOP instruction mov ecx, 7 lp: … dec ecx jnz lp How can we do this in Assembler w/out the loop instruction? mov ecx, 7 lp: … loop lp What do you think is faster?

  20. LOOP instruction mov ecx, 7 lp: … dec ecx jnz lp How can we do this in Assembler w/out the loop instruction? mov ecx, 7 lp: … loop lp Let’s see! What do you think is faster?

  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. mov ecx, 7 lp: … dec ecx jnz lp 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. mov ecx, 7 lp: … dec ecx jnz lp We can accomplish this w/out lp: mov ecx, 7 @@: … dec ecx 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++) { mov eax, 0 lp: cmp eax, 100 jge done … inc eax jmp lp 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++) { mov eax, 0 lp: cmp eax, 100 jge done … inc eax jmp lp done: We can accomplish this w/out lp and done: mov eax, 0 @@: cmp eax, 100 jge @f … inc eax 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. • 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

More Related