LOOPNZ and LOOPNE Instructions


The LOOPNZ (loop if not zero) and LOOPNE (loop if not equal) instructions are the counterpart of LOOPZ or LOOPE. The loop destination must be within –128 to +127 bytes of the current location counter.

LOOPNZ/LOOPNE — LOOP If Not Equal (Zero)
Usage: LOOPNZ label or LOOPNE label

Flag O D I S Z A P C
Result                

Decrements ECX by 1 (without modifying the flags) and transfers control to “label” if ECX != 0 and the Zero Flag is clear.
Clocks
Operands 286 386 486 Size Bytes
label: jump 8+m 11+m 9 2
no jump 4 ? 6 2

The loop continues while the unsigned value of ECX is greater than zero and zero flag is clear. The following code is the execution logic of them:
  ECX = ECX - 1
  if (ECX > 0 and ZF = 0)
    jump to destination
The code on the right shows an array whose integer elements are sorted ascendingly. It scans each number in an array until a nonnegative number is found. It then applies addition to the remaining nonnegative numbers.
 Adding up the Nonnegative Integers 
 INCLUDE Irvine32.inc
 .data
 array     SWORD  -10, -6, -3, -1, 
                    4, 10, 30, 40
 .code
 main PROC
       mov    esi, OFFSET array
       mov    ecx, LENGTHOF array
       ; Skip negative values.
 next: test   WORD PTR [esi], 8000h
       pushfd
       add    esi, TYPE array
       popfd
       loopnz next
       jnz    quit
       
       ; Add up the rest values.
       sub    esi, TYPE array
       mov    eax, 0
       
calc: add ax, [esi] add esi, TYPE array loop calc call WriteInt quit: exit main ENDP END main