| ||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||
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
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.
The next example illustrates the execution of the loop instruction.
LOOPZ/LOOPE and LOOPNZ/LOOPNE InstructionsIn 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.
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.
The JCXZ instructionIt 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.
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.
|