Computer Organization & Assembly Language (CS-215L) Lab Session 06
Lab Session 06
PROGRAM FLOW CONTROL USING LOOP AND NESTED LOOP
Objective:
Implementation of the Program Flow Control using “LOOP” and “NESTED LOOP”
instructions in Assembly language.
Program Flow Control using LOOP Instruction
Loop instruction is special type of program flow control for repetition of an instruction or
block of instructions. The Loop is implemented by using the command “LOOP” followed by
the label of the instruction where to jump to. The maximum number of iterations (repetitions)
is defined in the register CX. Table 6.1 shows different loop instructions available.
Instruction Operation and Jump Condition Alternates
LOOP decrease cx, jump to label if cx not zero. DEC CX and JCXZ
LOOPE decrease cx, jump to label if cx not zero and equal (zf = 1). LOOPNE
LOOPNE decrease cx, jump to label if cx not zero and not equal (zf= 0). LOOPE
LOOPNZ decrease cx, jump to label if cx not zero and zf = 0. LOOPZ
LOOPZ decrease cx, jump to label if cx not zero and zf = 1. LOOPNZ
JCXZ jump to label if cx is zero. OR CX, CX and JNZ
Table 6.1 LOOP instructions
The LOOP instruction first decrements the value of count defined in register CX and checks
the value of CX register. If the CX register contains a non-zero value the loop continues
otherwise the loop stops. For example, following code will add 1 in the value contained in
register AL for 10 times.
MOV CX, 10 ;CX= maximum count value i.e.; 10
MOV AL, 0 ;Initializing AL with zero
SUM: ADD AL, 1 ;Add 1 to AL
LOOP SUM ;DEC CX and JNZ SUM
The alternate of the LOOP instruction can be a combination of two commands DEC and JNZ.
To demonstrate, the above code can be written as:
MOV CX, 10 MOV AL, 0
SUM: ADD AL, 1 DEC CX
JNZ SUM
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 28
Computer Organization & Assembly Language (CS-215L) Lab Session 06
;CX= maximum count ;Add 1 to AL
value i.e.; 10 ;CX=CX-1
;Initializing AL with zero ;If zero flag is not set i.e.; CX != 0, jump to SUM
It is also to be noted that the count initialization for the CX value is done outside the loop.
Loops can also be used to insert delays, where necessary, in a Program. Each instruction has
assigned a time to execute in the instruction set. The time to execute depends upon the machine
cycles it takes to complete that particular instruction by the microprocessor. For example, to
generate a delay of 1ms, LOOP instruction can be used to loop a NOP (no operation)
instruction for number of times.
Nested Loop:
Nested loop is a natural extension of the loops which has a loop within a loop, tha t is, each
loop nests insides another loop. Multiple loops can be nested within one another. The terms
outer loop and inner loop are used to describe a nested loop with two levels.
(a) Nested loop using CX register:
Since LOOP command uses only CX as counter, therefore, to implement nested loop using
LOOP instruction stack is used to store count value of outer loop. A sample code to display a
6X10 pattern of ‘*’, as shown in Figure 6.1, using nested loop is given below.
MOV CX, 6 ;Count for outer loop
OUTER_LOOP:
PUSH CX ;Store CX in the stack
MOV CX, 10 ;Count for inner loop
INNER_LOOP:
MOV AH, 2 ;Service to display character
MOV DL, ‘*’ INT
21H
LOOP INNER_LOOP
;Code for inserting a line
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV AH, 2
MOV DL, 0AH
INT 21H
;Retrieving the outer count
POP CX
LOOP OUTER_LOOP
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 29
Computer Organization & Assembly Language (CS-215L) Lab Session 06
Figure 6.1 A 6X10 pattern of ‘*’
(b) Nested loop using BX and CX registers:
Similar pattern can be displayed by using different register for holding the count of either inner
or outer loop. In the given code, BX is used with JNZ instruction.
MOV CX, 6 ;Count for outer loop
OUTER_LOOP:
MOV BX, 10 ;Count for inner loop
INNER_LOOP: MOV AH, 2 ;Service to display character
MOV DL, ‘*’
INT 21H
DEC BX ;Decrease BX value
JNZ INNER_LOOP ;Stay in the loop if BX is not zero
MOV AH, 2 ;Code for inserting a line
MOV DL, 0DH
INT 21H
MOV AH, 2
MOV DL, 0AH
INT 21H
LOOP OUTER_LOOP
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 30
Computer Organization & Assembly Language (CS-215L) Lab Session 06
LAB EXERCISE
1- Write a program to display “Computer Science” ten times on computer screen.
2- Write a program to display the numbers in the specific pattern as shown below.
12345
234
3
432 5
4321
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 31
Computer Organization & Assembly Language (CS-215L) Lab Session 06
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 32
Computer Organization & Assembly Language (CS-215L) Lab Session 06
3- Write a program to display the numbers in the specific pattern as shown below.
1234567
123456
12345
1234
123
12
1
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 33
Computer Organization & Assembly Language (CS-215L) Lab Session 06
4- Write a program to display number “1” on screen and then after each 60 second update it.
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 34
Computer Organization & Assembly Language (CS-215L) Lab Session 06
Department of Computer Science & Information Technology
Sir Syed University of University of Engineering and Technology 35