<<< LEA Load Effective Address | Index | LOOPNZ/LOOPNE Loop While CX/ECX > 0 and ZF = 0 >>> |
O D I T S Z A P C OF: Overflow flag TF: Trap flag AF: Aux carry F F F F F F F F F DF: Direction flag SF: Sign flag PF: Parity flag <none> IF: Interrupt flag ZF: Zero flag CF: Carry flag
LOOP is a combination decrement counter, test, and jump instruction. It uses the CX register in 16-bit modes, and ECX in 32-bit modes. The operation of LOOP is logistically identical in both modes, and I use 16-bit coding as an example here.
LOOP simplifies code by acting as a DEC CX instruction, a CMP CX,0 instruction, and JZ instruction, all at once. A repeat count must be initially loaded into CX. When the LOOP instruction is executed, it first decrements CX. Then it tests to see if CX = 0. If CX is not 0, LOOP transfers control to the displacement specified as its operand:
MOV CX,17 DoIt: CALL CrunchIt CALL StuffIt LOOP DoIt
Here, the two procedure CALLs will be made 17 times. The first 16 times through, CX will still be nonzero and LOOP will transfer control to DoIt. On the 17th pass, however, LOOP will decrement CX to 0, and then fall through to the next instruction in sequence when it tests CX.
LOOP does not alter any flags, even when CX is decremented to 0. Warning: Watch your initial conditions! If CX is initially 0, LOOP will decrement it to 65,535 (0FFFFH) and then perform the loop 65,535 times. Worse, if you're working in 32-bit protected mode and enter a loop with ECX = 0, the loop will be performed over 2 billion times, which might be long enough to look like a system lockup.
r8 = AL AH BL BH CL CH DL DH r16 = AX BX CX DX BP SP SI DI sr = CS DS SS ES FS GS r32 = EAX EBX ECX EDX EBP ESP ESI EDI m8 = 8-bit memory data m16 = 16-bit memory data m32 = 32-bit memory data i8 = 8-bit immediate data i16 = 16-bit immediate data i32 = 32-bit immediate data d8 = 8-bit signed displacement d16 = 16-bit signed displacement d32 = 32-bit unsigned displacement
<<< LEA Load Effective Address | Index | LOOPNZ/LOOPNE Loop While CX/ECX > 0 and ZF = 0 >>> |