210 likes | 589 Views
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:
E N D
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
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
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
; 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
False True Condition Statements WHILE Loop • This loop depends on a condition. • Pseudocode: • WHILE condition DO • statements • END_WHILE Looping Structures 7
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
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
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: MOV AH,1 REAPEAT: INT 21H CMP AL,’ ‘ JNE REAPEAT: Looping Structures 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