assembly language lecture 7 (looping structures)
DESCRIPTION
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: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/1.jpg)
Assembly Language
Lecture 7(Looping Structures)
![Page 2: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/2.jpg)
Looping Structures 1
Lecture Outline
• Introduction• Looping Structures
• FOR Loop• WHILE Loop• REPEAT Loop
![Page 3: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/3.jpg)
Introduction
2Looping Structures
• 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.
![Page 4: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/4.jpg)
3Looping Structures
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
Count=0
Initialize count
Statements
Count = count -1
FalseTrue
![Page 5: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/5.jpg)
4Looping Structures
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.
![Page 6: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/6.jpg)
5Looping Structures
FOR Loop
• Using the instruction LOOP, a FOR loop can be implemented as follows:
; initialize CX to loop_count TOP:
; body of the loopLOOP TOP
![Page 7: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/7.jpg)
6Looping Structures
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, 80MOV AH, 2MOV DL, '*'
TOP:INT 21hLOOP TOP
; what if CX =0?
MOV CX, 80MOV AH, 2MOV DL, '*'JCXZ SKIP ;jump if CX=0
TOP:INT 21hLOOP TOP
SKIP:
![Page 8: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/8.jpg)
7Looping Structures
WHILE Loop
• This loop depends on a condition.
• Pseudocode: WHILE condition DO
statements END_WHILE Condition
Statements
False True
![Page 9: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/9.jpg)
8Looping Structures
WHILE Loop
• Example: Write some code to count the number of characters in an input line.
• Solution: Pseudocode:
initialize count to 0read a characterWHILE character <> carriage_return DO count = count + 1
read characterEND_WHILE
continue
![Page 10: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/10.jpg)
9Looping Structures
WHILE Loop
It can be coded as follows:
WHILE_:
END_WHILE:
MOV DX, 0 ; DX counts charactersMOV AH, 1 ; prepare to readINT 21h ; character in AL
CMP AL, 0Dh ; CR?JE END_WHILE ; yes, exitINC DX ; not CR, increment countINT 21h ; read a characterJMP WHILE_ ; loop back
![Page 11: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/11.jpg)
9Looping Structures
REPEAT Loop
• This loop depends on a condition.
• Pseudocode: REPEATStatementsUNTIL conditions
![Page 12: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/12.jpg)
9Looping Structures
REPEAT Loop
• Example: write code to read characters until a blank is read
• Pseudocode: REPEATRead characterUNTIL character is blank The code is:
MOV AH,1REAPEAT: INT 21H CMP AL,’ ‘ JNE REAPEAT:
![Page 13: Assembly Language Lecture 7 (Looping Structures)](https://reader036.vdocuments.site/reader036/viewer/2022062422/56814016550346895dab5fd5/html5/thumbnails/13.jpg)
Looping Structures 12
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)