80386 Programmer's Reference Manual -- Opcode LOOP

LOOP/LOOPcond -- Loop Control with CX Counter

OpcodeInstructionClocks DescriptionExample
E2 cbloop rel811+m DEC countloop rel8_34
E1 cbloope rel811+m DEC countloope rel8_35
E1 cbloopz rel811+m DEC countloopz rel8_36
E0 cbloopne rel811+m DEC countloopne rel8_37
E0 cbloopnz rel811+m DEC countloopnz rel8_38


IF AddressSize = 16 THEN CountReg is CX ELSE CountReg is ECX; FI;
CountReg := CountReg - 1;
IF instruction <> LOOP
   IF (instruction = LOOPE) OR (instruction = LOOPZ)
   THEN BranchCond := (ZF = 1) AND (CountReg <> 0);
   IF (instruction = LOOPNE) OR (instruction = LOOPNZ)
   THEN BranchCond := (ZF = 0) AND (CountReg <> 0);

IF BranchCond
   IF OperandSize = 16
      IP := IP + SignExtend(rel8);
   ELSE (* OperandSize = 32 *)
      EIP := EIP + SignExtend(rel8);


LOOP decrements the count register without changing any of the flags. Conditions are then checked for the form of LOOP being used. If the conditions are met, a short jump is made to the label given by the operand to LOOP. If the address-size attribute is 16 bits, the CX register is used as the count register; otherwise the ECX register is used. The operand of LOOP must be in the range from 128 (decimal) bytes before the instruction to 127 bytes ahead of the instruction.

The LOOP instructions provide iteration control and combine loop index management with conditional branching. Use the LOOP instruction by loading an unsigned iteration count into the count register, then code the LOOP at the end of a series of instructions to be iterated. The destination of LOOP is a label that points to the beginning of the iteration.

Flags Affected


Protected Mode Exceptions

#GP(0) if the offset jumped to is beyond the limits of the current code segment

Real Address Mode Exceptions


Virtual 8086 Mode Exceptions


[Home Page dell'ITIS "Fermi"] [80386 Programmer's Reference Manual Index] [Previous] [Next]