Assembly Notes on the LOOP Instruction

[BX] and LOOP

[BX] refers to a memory operand. The default segment is DS, and context can determine the operand size.

push ax subtracts 2 from SP and stores AX at SS × 16 + SP. pop ax reads AX from that address and adds 2 to SP.

LOOP execution

LOOP decrements CX. If the result is nonzero, it branches to the label. Otherwise execution falls through.

For a typical counted loop, initialize CX with the iteration count, put the label before the body, and put LOOP after it.

assume cs:code
code segment
mov bx,0
mov cx,12
mov ax,0ffffh
mov ds,ax
mov ax,0020h
mov es,ax
s: mov al,[bx]
mov es:[bx],al
inc bx
loop s

mov ax,4c00h
int 21h
code ends
end

Assembler details

Some assemblers require a leading zero in a hexadecimal literal that starts with a letter, so use 0FFFFH. Some assemblers also reject a bare numeric memory operand such as [number], making a register-based operand such as [BX] useful.

Combining LOOP with [BX] makes it possible to advance through memory while bounding the iteration count.

The original DOS notes describe 0:0200–0:02FF as a 256-byte practice area. Availability depends on the DOS environment and loaded software.