Loop Instructions 

The LOOP instruction is mainly used to simulate the different loops in HLL. The Loop instructions use the CX register to indicate the loop count.

The syntax of the Loop instruction is:

	LOOP label

The Loop instruction decrements CX without changing any flags
If CX is not zero after the decrement, control is transferred to the destination label
The jump is a SHORT jump only

Note that the ECX register can be used as a loop counter in 32-bit mode. However, in this case, one has to use the instruction LOOPD instead of LOOP.

The following example illustrates the use of the Loop instruction in implementing a corresponding for loop in HLL.

Example: For Loop in HLL.
	…
	for (x=9;x>0;x--) 
		n += x;
	…

Example: Implementing For Loop in Assembly.
	 …
	 ;for(x=9;x>0;x--)
  	 MOV CX,9
toploop:ADD n,CX   ;n=n+x
	 …
  	 LOOP toploop

The next example illustrates the execution of the loop instruction.
Fig. m08080.1 Illustration of the execution of the Loop instruction

LOOPZ/LOOPE and LOOPNZ/LOOPNE Instructions

In these two additional instructions, the state of the ZERO Flag may also cause loop termination in addition to the content of the CX register. Some action inside the loop should affect the zero flag (e.g. a CMP instruction) before these instructions are executed.

LOOPZ/LOOPE: Loop while (ZF = 1) && (CX<> 0)
LOOPNZ/LOOPNE: Loop while (ZF = 0) && (CX <> 0)
LOOPZ is equivalent to LOOPE
LOOPNZ is equivalent to LOOPNE

The format of the loop instructions along with the action taken is given in the following table:

Instruction Action
LOOP target CX= CX-1 ; if (CX <> 0) jump to target
LOOPE/LOOPZ target CX= CX-1 ;if (CX <> 0) AND (ZF=1) jump to target
LOOPNE/LOOPNZ target CX= CX-1 ;if (CX <> 0) AND (ZF=0) jump to target

The following example illustrates the use of the LOOPNE instruction. It accepts at most 9 characters from the keyboard, and when the 9th character is pressed (or the enter key is used) the number of key presses is displayed.

Example: Use of the LOOPNE instruction.
		‎…‎
	   MOV AH,1‎
‎  ‎	   MOV CX,9‎
NextChar: INT 21H
‎  ‎	   CMP AL,13‎
‎  ‎	   LOOPNE NextChar
	   ‎;Determine Count
‎  ‎	   MOV AX, 0239H
‎  ‎	   SUB AL,CL
	   MOV DL,AL
‎  ‎	   INT 21H
	   ‎…
‎

The JCXZ instruction

It should be observed that the Loop instructions execute one iteration before decrementing CX and checking if its value is 0. So, if the initial value of CX is 0, then CX will become FFFF and the loop will repeat for 2^16=65536 times. The instruction JCXZ provides a solution for this problem by testing the CX register and if zero, control is transferred to the target instruction. The format of this instruction is

	JCXZ target

Note that this instruction is equivalent to

	CMP CX, 0
	JZ target

except that JCXZ does not affect the flags, while the CMP/JZ combination affect the flags.

The following example demonstrates a program that asks the user to enter a character (a-z), and counts all upper case and lower case characters that match the entered character in a Table defined in the data segment. Assume that the table size is 100 bytes.

Example: Use of the LOOPNE and JCXZ instructions.
		‎
	   MOV AH, 1	; read a character
	   INT 21H
	   AND AL, 0DFH	; convert character to upper case 
	   XOR SI, SI	; character count
	   XOR BX, BX 	; table index
	   MOV CX, 100‎
Next:     JCXZ Done
‎  ‎	   MOV DL, Table[BX]	; get character from table 
	   INC BX
	   AND DL, 20H	; convert character to upper case 
	   CMP AL, DL
	   LOOPNE Next
	   JNE Done
	   INC SI
	   JMP Next
Done:       ‎…
‎

The next example demonstrates a program that counts the number of non-blank characters in a Table defined in the data segment. Assume that the table size is 100 bytes.

Example: Use of the LOOPE and JCXZ instructions.
		‎
	   XOR SI, SI	; character count 
	   MOV  BX, -1 	; table index 
	   MOV CX, 100‎
Next:     JCXZ Done
‎  ‎	   INC BX
	   CMP Table[BX], ' '
	   LOOPE Next
	   JE Done
	   INC SI
	   JMP Next
Done:       ‎…
‎