Sometimes while executing a program there will be a need for repeated execution of some instructions a specified number of times. In all the programming languages there are some specific structures to express this repeated execution of instructions.
Similarly, the designers of the 8086 microprocessor came up with a group of instructions to perform repeated execution of a series of instructions a specified number of times much easier. These instructions are called LOOP instructions.
LOOP Instructions of 8086 Microprocessor :
The loop instructions cause the microprocessor to execute a series of instructions repeatedly. Basically, the LOOP instructions are short jump instructions on a condition i.e., when the condition satisfies a short jump is taken whose destination or target address is in the range of -128 bytes to +127 bytes from the instruction address after LOOP instruction. The various LOOP instructions and their functions are listed below.
Mnemonic | Meaning | Format | Operation |
---|---|---|---|
LOOP | Loop | LOOP short-label | Decrements CX by one, if CX ≠ 0, jump to “short-label”. |
LOOPE/LOOPZ | loop while equal/Loop while zero | LOOPE/LOOPZ short-label | Decrements CX by one, if CX ≠ 0 and ZF = 1, jump to “short-label”. |
LOOPNE/LOOPNZ | loop while not equal/Loop while not zero | LOOPNE/LOOPNZ short-lablel | Decrements CX by one, if CX ≠ 0 and ZF = 0, jump to “short-label”. |
JCXZ | Jump if CX is zero | JCXZ short-label | If CX = 0, branch to “short-label”. |
All the above instructions are iteration control instructions and are used to execute a series of instructions repeatedly on the successful satisfaction of the condition. The condition is to check the CX register and zero flag (ZF) or can be only to check the CX register. CX register must be loaded with the number of iterations to be performed, prior to entering the section of the code terminated by LOOP instructions. Let us see briefly each LOOP instruction.
LOOP Instruction :
The LOOP instruction executes the group of instructions a number of times and it uses relative addressing mode. The number of iterations will depend on the condition to be satisfied. The CX register will perform the LOOP operation on the instructions to be iterated.
For every execution of LOOP instruction, the CX is automatically decremented by one without affecting flags and performs a short jump to the target address. This loop will continue until the CX becomes zero. When CX = 0, the execution of the loop will stop and the instructions after the LOOP will start execution.
LOOPE/LOOPZ Instruction :
This instruction is similar to LOOP except that it checks for both CX and ZF conditions to be satisfied before the jump can take place. Firstly the count register CX is loaded with a value of the number of times the instructions are to be repeated. For every iteration or every time the LOOPE/LOOPZ instruction executes, it decrements the CX register by one without affecting flags and performs a short jump to the target address (i.e., to the address with signed 8-bit relative displacement in the range of -128 bytes to +127 bytes from the instruction address after LOOPE/LOOPZ instruction) until the condition CX ≠ 0 and ZF = 1 is maintained.
If CX equals 0, or if the zero flag gets cleared within the loop, the loop will terminate. In other words, the two ways to exit the loop are CX = 0 or ZF = 0, then after the processor starts executing the next instruction present after LOOPE/LOOPZ.
Example for LOOPE/LOOPZ :
The following code segment compares the elements of ARRAY with 00FFH, it exits if they are unequal.
LOOPNE/LOOPNZ Instruction :
These instructions are the opposite of LOOPE/LOOPZ. It decrements the CX register by one without affecting flags and performs a short jump to the target address if CX ≠ 0 and ZF = 0. In other words, the number of iterations is loaded into the count register CX and after every time the instruction LOOPE/LOOPZ executes the CX will be autodecremented by one. Until the conditions, CX ≠ 0 and ZF = 0 satisfies, the execution performs a short jump to the target address given in the instruction.
When either of the condition CX ≠ 0 or ZF = 0 fails to satisfies i.e., zero flag (ZF) is set or CX becomes zero after autodecrement, the execution of the loop will exit, and the instruction after LOOPE/LOOPZ in the program will be executed. A group of instructions can be repeated a number of times (i.e., until CX = 0 and ZF ≠ 0) by using LOOPE/LOOPZ.
Example for LOOPNE/LOOPNZ :
JCXZ Instruction :
The JCXZ loop instruction jumps to the target address if CX = 0 without affecting flags. This instruction is useful at the beginning of a loop to bypass the loop if CX = 0. IF the CX register is not equal to zero, no jump in the program is performed, thereby executing the next instructions. As compared to LOOPE/LOOPZ and LOOPNE/LOOPNZ instructions, in JCXZ the jump is not based on the zero flag (ZF) rather it only depends on the CX register. In the example shown below, JCXZ instruction is used to bypass the AGAIN loop whenever CX = 0.