1 / 13

Assembly Language Lecture 7 (Looping Structures)

Assembly Language Lecture 7 (Looping Structures). Lecture Outline Introduction Looping Structures FOR Loop WHILE Loop REPEAT Loop. Looping Structures. 1. Introduction. A loop is a sequence of instructions that is repeated. The number of times to repeat may:

heinz
Download Presentation

Assembly Language Lecture 7 (Looping Structures)

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. Assembly Language Lecture 7 (Looping Structures)

  2. Lecture Outline • Introduction • Looping Structures • FOR Loop • WHILE Loop • REPEAT Loop Looping Structures 1

  3. Introduction • A loop is a sequence of instructions that is repeated. • The number of times to repeat may: • Be known in advance, or • Depend on conditions. • Looping structures: • FOR loop. • WHILE loop. • REPEAT loop. Looping Structures 2

  4. Initialize count Statements Count = count -1 Count=0 False True FOR Loop • This is a loop structure in which the loop statements are repeated a • known number of times. • Pseudocode: • FOR loop_count times DO • statements • END_FOR Looping Structures 3

  5. FOR Loop • The LOOP instruction can be used to implement a for loop. • Syntax: • LOOP destination_label • Destination_label must precede the LOOP instruction by no more • than 126 bytes. • The counter for the loop is the register CX, which is initialized to • loop_count. • Execution of the LOOP instruction causes CX to be decremented • automatically. • If (CX < > 0) control transfers to destination_label • else the next instruction after LOOP is done. Looping Structures 4

  6. FOR Loop • Using the instruction LOOP, a FOR loop can be implemented as • follows: • ; initialize CX to loop_count • TOP: • ; body of the loop • LOOP TOP Looping Structures 5

  7. ; what if CX =0? MOV CX, 80 MOV AH, 2 MOV DL, '*' JCXZ SKIP ;jump if CX=0 TOP: INT 21h LOOP TOP SKIP: FOR Loop • Example: Write some code to display a row of 80 stars. • Solution: • Pseudocode: • FOR 80 times DO • display '*' • END_IF It can be coded as follows: MOV CX, 80 MOV AH, 2 MOV DL, '*' TOP: INT 21h LOOP TOP Looping Structures 6

  8. False True Condition Statements WHILE Loop • This loop depends on a condition. • Pseudocode: • WHILE condition DO • statements • END_WHILE Looping Structures 7

  9. continue WHILE Loop • Example: Write some code to count the number of characters in an • input line. • Solution: • Pseudocode: • initialize count to 0 • read a character • WHILE character <> carriage_return DO • count = count + 1 • read character • END_WHILE Looping Structures 8

  10. WHILE Loop It can be coded as follows: WHILE_: END_WHILE: MOV DX, 0 ; DX counts characters MOV AH, 1 ; prepare to read INT 21h ; character in AL CMP AL, 0Dh; CR? JE END_WHILE ; yes, exit INC DX ; not CR, increment count INT 21h ; read a character JMP WHILE_ ; loop back Looping Structures 9

  11. REPEAT Loop • This loop depends on a condition. • Pseudocode: • REPEAT • Statements • UNTIL conditions Looping Structures 9

  12. REPEAT Loop • Example: write code to read characters until a blank is read • Pseudocode: • REPEAT • Read character • UNTIL character is blank The code is: MOV AH,1 REAPEAT: INT 21H CMP AL,’ ‘ JNE REAPEAT: Looping Structures 9

  13. WHILE Versus REPEAT • Use of a WHILE loop or a REPEAT loop is a matter of personal • preference. • A WHILE loop can be bypasses if the terminating condition is • initially false. (a REPEAT loop must be done at least once) • The code for a REPEAT loop is likely to be a little shorter because • there is only one jump. (WHILE loops has two jumps) Looping Structures 12

More Related