Assembly Language Lecture 7 (Looping Structures)
description
Transcript of Assembly Language Lecture 7 (Looping Structures)
Assembly Language
Lecture 7(Looping Structures)
Looping Structures 1
Lecture Outline
• Introduction• Looping Structures
• FOR Loop• WHILE Loop• REPEAT Loop
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.
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
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.
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
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:
7Looping Structures
WHILE Loop
• This loop depends on a condition.
• Pseudocode: WHILE condition DO
statements END_WHILE Condition
Statements
False True
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
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
9Looping Structures
REPEAT Loop
• This loop depends on a condition.
• Pseudocode: REPEATStatementsUNTIL conditions
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:
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)