Loop Instructions 8086 Microprocessor

Loop using DEC and JMP Instructions 

We are familiar with the conditional branch instructions which as shown below could be used for repetitive tasks. Looping using these technique involve more than one instruction as shown in following format

MOV CX, N

Begin :        do some operation

operations

DEC CX JNZ Begin

Loop Instructions in 8086: 

loop instructions are used to simplify the decrementing, testing and branching portion of the loop. In the above case this portion required two instructions, but in more complicated situation may require more than two instructions.
The loop instruction for 8086 all have the form:

OPCODE D8

where D8 is byte displacement from current IP. The loop instruction is simplified to:
With loop instruction the format for branch become:

MOV CX, N

Begin: — —

LOOP Begin

The Loop Instructions supported by 8086 are given below:

name Mnemonic and format Alternate Form Test Condition
LOOP LOOP Opr CX not equal to ‘0’
Loop while zero, or equal LOOPZ Opr LOOPE Opr ZF=1 and CX not zero
Loop while non zero or not equal LOOPNZ OPR LOOPNE OPR ZF=0 and CX not 0
Branch on CX JCXZ OPR CX = 0

 

Programming Examples

  1. Program for adding an array of binary numbers

Address Hex Code Mnemonics Comment
Data SEGMENT
0000 NUM   DB 12h,23h,44h,67h,32h,89h,38h,98h,65h,55h
000A N DB 5
000B Total DB 10 dup(?)
CODE SEGMENT
ASSUME   CS: CODE, DS:Data
0000 MOV AX, data
MOV DS, AX
MOV CX, N ;set counter
MOV AX, 0
MOV SI, 0
START: ADD AX, NUM[SI]
ADD SI, 2
LOOP START
MOV Total, AX
CODE ENDS
END

 

  1. Program to add two 16 digit packed BCD numbers
    The flow chart below shows the procedure to add packed BCD numbers

Address Hex Code Mnemonics Comment
Data SEGMENT
0000 Augend    DB 12,23,44,67,32,89,38,98,65,55,22,14,43,56,66,77
0010h Addend    DB 22,23,43,61,12,19,48,68,33,55,26,28,82,92,88,19
0020h Sum DB 16 dup(?)
CODE SEGMENT
ASSUME   CS: CODE, DS:Data
0000 MOV AX, data
MOV DS, AX
MOV CX, 8 ;set counter
MOV AX, 0
MOV SI, 0
START: MOV AL, Augend[SI]
ADC AL, Addend[SI]
DAA
MOV Sum[SI], AL
INC SI
LOOP START
CODE ENDS
END

 

  1. Program to search space character in a string
    The following flowchart shows the method of searching a character (space) in an array of characters(string)

Address Hex Code Mnemonics Comment
Data SEGMENT
0000 my_name    DB “Ravinder Nath”
000Dh msg DB “Space Found”
0019h msg1 DB “Space Not Found”
CODE SEGMENT
ASSUME   CS: CODE, DS:Data
0000 MOV AX, data
MOV DS, AX
MOV CX, LENGTH my_name ;set counter
MOV SI, -1
MOV AL, 20h
Next:
INC SI
CMP AL, my_name[SI]
LOOPNE Next
JNZ Not_found
LEA BX, msg
MOV AH, 09h
INT 21h
JMP Last
Not_found:
LEA BX, msg1
MOV AH, 09h
INT 21h
Last:
CODE ENDS
END

 

  1. Sort an array of numbers in descending order using Bubble sort
    The flow chart below shows the sorting technique using bubble sort.

Address Hex Code Mnemonics Comment
Data SEGMENT
0000 num    DB 12,34,76,1,45,34,76,56,78,23,21,31
000Bh sorted_num DB 10 dup(?)
CODE SEGMENT
ASSUME   CS: CODE, DS:Data
0000 MOV AX, Data
MOV DS, AX
MOV CX, 06h ;set counter
DEC CX
for_Loop1:
MOV DI, CX
MOV BX, 0
for_Loop2:
MOV AX, num[BX]
CMP AX, num[BX+2]
JGE CONTINUE
XCHG AX, num[BX+2]
MOV sorted_array[BX], AX
CONTINUE:
ADD BX, 2
LOOP for_Loop2
MOV CX, DI
LOOP for_Loop1
CODE ENDS
END

 

 

 

Updated: December 14, 2020 — 1:52 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

care4you © 2020 Connect On Facebook DMCA.com Protection Status