LOOP            Loop on Count                        Flags: Not altered

LOOP location Loop
LOOPE/LOOPZ location Loop While Equal/Zero
LOOPNE/LOOPNZ location Loop While Not Equal/Zero

Logic
; loop ; loope ; loopne
eCX ← eCX - 1 eCX ← eCX - 1 eCX ← eCX - 1
if eCX <> 0 if eCX <> 0 AND ZF=1 if eCX <> 0 AND ZF=0
JMP location JMP location JMP location
endif endif endif

The LOOP instructions are conditional jumps that use the count
value in eCX (LOOPE/LOOPNE also use the zero flag) to determine
whether a code loop should be continued or terminated.

All LOOPs automatically decrement eCX and terminate when eCX = 0.
The operand, a short code label, must be in the range -128 to +127
bytes from the next instruction.

If the address-size attribute is 16 bits, the CX register is used
as the count register; otherwise, ECX is used (80386+).


Example: sub ax,ax
jcxz skip ; Don't execute 65,535 times
again:
add ax,[bx]
stosw
inc bx
inc bx
loop again
mov [looped],1
skip:

Example: mov cx,127
mov si,0081h
blanks:
lodsb
cmp al,20h
loopz blanks


Note
Some assemblers support the following LOOP extensions that force
the use of CX (loopw forms) or ECX (loopd forms) regardless of the
code segment's size attribute (80386+):

loopw,loopwe,loopwne,loopwnz,loopwz
loopd,loopde,loopdne,loopdnz,loopdz


Opcode Format
E0 cb LOOPNE rel8
E1 cb LOOPE rel8
E2 cb LOOP rel8


Length and timing
Operands Bytes 8088 186 286 386 486 Pentium
LOOP short 2 5/17 5/15 4/8+m 11+m 6/7 5/6 NP
LOOPE short 2 6/18 5/16 4/8 11+m 6/9 7/8 NP
LOOPNE short 2 5/19 5/16 4/8 11+m 6/9 7/8 NP