Transcript FOR Loop

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:
• Be known in advance, or
• Depend on conditions.
• Looping structures:
• FOR loop.
• WHILE loop.
• REPEAT loop.
Looping Structures
2
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
Initialize count
Statements
Count = count -1
Count=0
False
True
Looping Structures
3
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
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
FOR Loop
• Example: Write some code to display a row of 80 stars.
• Solution:
Pseudocode:
; what if CX =0?
FOR 80 times DO
display '*'
MOV CX, 80
END_IF
MOV AH, 2
It can be coded as follows:
MOV DL, '*'
MOV CX, 80
JCXZ SKIP ;jump if CX=0
MOV AH, 2
TOP:
MOV DL, '*'
INT 21h
TOP:
LOOP TOP
INT 21h
SKIP:
LOOP TOP
Looping Structures
6
WHILE Loop
• This loop depends on a condition.
• Pseudocode:
WHILE condition DO
statements
END_WHILE
False
Condition
True
Statements
Looping Structures
7
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
continue
Looping Structures
8
WHILE Loop
It can be coded as follows:
MOV
MOV
INT
DX, 0
AH, 1
21h
; DX counts characters
; prepare to read
; character in AL
AL, 0Dh
END_WHILE
DX
21h
WHILE_
; CR?
; yes, exit
; not CR, increment count
; read a character
; loop back
WHILE_:
CMP
JE
INC
INT
JMP
END_WHILE:
Looping Structures
9
REPEAT Loop
• This loop depends on a condition.
• Pseudocode:
REPEAT
Statements
UNTIL conditions
Looping Structures
9
REPEAT Loop
• Example: write code to read characters until a blank is read
• Pseudocode:
REPEAT
Read character
UNTIL character is blank
The code is:
REAPEAT:
REAPEAT:
Looping Structures
MOV AH,1
INT 21H
CMP AL,’ ‘
JNE
9
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