Figure 3-39 Loop instructions.

NameMnemonic and FormatAlternate MnemonicTest Condition*
Loop
LOOP   OPR
(CX) != 0
Loop while zero, or equal
LOOPZ  OPR
LOOPE
ZF = 1 and (CX) != 0
Loop while nonzero, or not equal
LOOPNZ  OPR
LOOPNE
ZF = 0 and (CX) != 0
Branch on CX
JCXZ   OPR
(CX) = 0

*Except for JCXZ which leaves (CX) unchanged, (CX)(CX)-1. Then if the test condition is met, (IP)(IP) + sign extended D8; otherwise (IP) are unchanged and the program continues in sequence.

Flags: No flags are affected.

Addressing modes: Mode is relative to IP. OPR must represent a label that is within -128 to 127 bytes of the instruction following the loop instruction.

Figure 3-40 Program for adding an array of binary numbers.

Data related directives, input, and other code
M, ARRAY, and TOTAL are defined as word variables
            MOV    CX,M         ;PUT COUNT IN CX
            MOV    AX,0         ;ZERO AX
            MOV    SI,AX        ;AND SI
START_LOOP: ADD    AX,ARRAY[SI] ;ADD NEXT ELEMENT TO AX
            ADD    SI,2         ;INCREMENT INDEX BY TWO
            LOOP   START_LOOP   ;REPEAT LOOP IF CX NONZERO
            MOV    TOTAL,AX     ;STORE RESULT IN TOTAL
Output and other code

Figure 3-42 Search example using LOOPNE.

Data related directives, input, and other code
L is defined as word variable and ASCII_STR is defined as byte variable
      MOV    CX,L               ;PUT ARRAY SIZE IN CX
      MOV    SI,-1              ;INITIALIZE INDEX, AND
      MOV    AL,20H             ;PUT CODE FOR SPACE IN AL
NEXT: INC    SI                 ;INCREMENT INDEX
      CMP    AL,ASCII_STR[SI]   ;TEST FOR SPACE
      LOOPNE NEXT               ;LOOP IF NOT SPACE AND COUNT IS NONZERO
      JNZ    NOT_FOUND          ;IF SPACE NOT FOUND, BRANCH TO NOT_FOUND
Output and other code

Figure 3-35 Branch address computation using 16-bit displacement.


Table of Contents | Next page | Previous page

This is page 36.