The LOOP instruction

Organizing loops with a counter in CX.

Format of the LOOP instruction and its variants

InstructionDescription
LOOP rel8Decrements CX; short jump if CX≠0.
LOOPE / LOOPZ rel8Decrements CX; short jump if CX≠0 and ZF=1.
LOOPNE / LOOPNZ rel8Decrements CX; short jump if CX≠0 and ZF=0.

Performs a loop operation using the cx register as a counter. Each time the loop instruction is executed, the cx counter register is decremented and then tested for equality with 0. If the counter is not zero, a jump to the label (the start of the loop) is performed.

Example 1: A simple loop

This example performs 10 iterations, incrementing AX by 1 each time.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public        ; Start of the code segment
    assume cs:code

    main proc              ; Start of the main procedure
        mov     cx, 10     ; Set the counter equal to 10
        xor     ax, ax     ; Zero AX

    MainLoop:
        inc     ax         ; Increment the value of the ax register by 1
        loop    MainLoop   ; CX--; if CX≠0, jump to MainLoop

        mov     ax, 4C00h  ; Copy the process termination function number into ah and the value into al
        int     21h        ; Call the interrupt
    main endp              ; End of the main procedure

code ends                  ; End of the code segment
end main                   ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Example 2: Circular shift left (ROL) via SHL + CF check

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public        ; Start of the code segment
    assume cs:code

    main proc              ; Start of the main procedure
        mov     ax, 8001h  ; Copy the value 1000 0000 0000 0001 into the ax register
        mov     cx, 16     ; Set the counter equal to 16

    ROL_Loop:
        shl     ax, 1      ; Shift the value in the ax register left by 1

        jnc     Skip       ; If CF=0, jump to the label Skip
        or      ax, 1      ; If CF=1, insert 1 into the least significant bit

    Skip:
        loop    ROL_Loop   ; CX--; if CX≠0, repeat

        mov     ax, 4C00h  ; Copy the process termination function number into ah and the value into al
        int     21h        ; Call the interrupt
    main endp              ; End of the main procedure

code ends                  ; End of the code segment
end main                   ; End of file. The main procedure is the entry point

Result: after 16 iterations the number 8001h returns to its original state.

Example 3: Circular shift right (ROR) via SHR + CF check

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public        ; Start of the code segment
    assume cs:code

    main proc              ; Start of the main procedure
        mov     ax, 8001h  ; Copy the value 1000 0000 0000 0001 into the ax register; AX = 8001
        mov     cx, 16     ; Set the counter equal to 16; CX = 0010

    ROR_Loop:
        shr     ax, 1      ; Shift the value in the ax register right by 1

        jnc     Skip       ; If CF=0, jump to the label Skip
        or      ax, 8000h  ; If CF=1, insert 1 into the most significant bit

    Skip:
        loop    ROR_Loop   ; CX--; if CX≠0, repeat

        mov     ax, 4C00h  ; Copy the process termination function number into ah and the value into al
        int     21h        ; Call the interrupt
    main endp              ; End of the main procedure

code ends                  ; End of the code segment
end main                   ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Example 4: Counting the number of set bits

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    count dw 0
data ends

code segment public           ; Start of the code segment
    assume ds:data, cs:code

    main proc                 ; Start of the main procedure
        mov     ax, seg data  ; Get the data segment address and store it in ax
        mov     ds, ax        ; Pass the data segment address from ax to ds

        mov     ax, 0AAABh    ; Copy the value 1010 1010 1010 1011 into the ax register
        mov     cx, 16        ; Set the counter equal to 16

    CountLoop:
        shr     ax, 1         ; Shift the value in the ax register right by 1 (the least significant bit goes into CF)

        jnc     Skip          ; If CF=0, jump to the label Skip
        inc     count         ; Increment the value of the variable count by 1

    Skip:
        loop    CountLoop     ; Loop while CX≠0

        mov     ax, 4C00h     ; Copy the process termination value into ah and the value into al
        int     21h           ; Call the interrupt
    main endp                 ; End of the main procedure

code ends                     ; End of the code segment
end main                      ; End of file. The main procedure is the entry point

Result: the variable count will contain 10 (the number of one bits in the number AAABh).

Processor status flags

CF
No
PF
No
AF
No
ZF
No
SF
No
TF
No
IF
No
DF
No
OF
No