* remove "\r" nonsense
[mascara-docs.git] / i386 / i386.reference / LOOP.htm
blobd9ddafdae0efc57b9631b5109340aca0b92c5513
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
2 <HTML>
3 <HEAD>
4 <TITLE>80386 Programmer's Reference Manual -- Opcode LOOP</TITLE>
5 </HEAD>
6 <BODY>
7 <B>up:</B> <A HREF="c17.htm">
8 Chapter 17 -- 80386 Instruction Set</A><BR>
9 <B>prev:</B><A HREF="LODS.htm"> LODS/LODSB/LODSW/LODSD Load String Operand</A><BR>
10 <B>next:</B><A HREF="LSL.htm"> LSL Load Segment Limit</A>
11 <P>
12 <HR>
13 <P>
14 <H1>LOOP/LOOPcond -- Loop Control with CX Counter</H1>
16 <PRE>
17 Opcode Instruction Clocks Description
19 E2 cb LOOP rel8 11+m DEC count; jump short if count <> 0
20 E1 cb LOOPE rel8 11+m DEC count; jump short if count <> 0 and ZF=1
21 E1 cb LOOPZ rel8 11+m DEC count; jump short if count <> 0 and ZF=1
22 E0 cb LOOPNE rel8 11+m DEC count; jump short if count <> 0 and ZF=0
23 E0 cb LOOPNZ rel8 11+m DEC count; jump short if count <> 0 and ZF=0
24 </PRE>
26 <H2>Operation</H2>
28 <PRE>
29 IF AddressSize = 16 THEN CountReg is CX ELSE CountReg is ECX; FI;
30 CountReg := CountReg - 1;
31 IF instruction <> LOOP
32 THEN
33 IF (instruction = LOOPE) OR (instruction = LOOPZ)
34 THEN BranchCond := (ZF = 1) AND (CountReg <> 0);
35 FI;
36 IF (instruction = LOOPNE) OR (instruction = LOOPNZ)
37 THEN BranchCond := (ZF = 0) AND (CountReg <> 0);
38 FI;
39 FI;
41 IF BranchCond
42 THEN
43 IF OperandSize = 16
44 THEN
45 IP := IP + SignExtend(rel8);
46 ELSE (* OperandSize = 32 *)
47 EIP := EIP + SignExtend(rel8);
48 FI;
49 FI;
50 </PRE>
52 <H2>Description</H2>
54 LOOP decrements the count register without changing any of the flags.
55 Conditions are then checked for the form of LOOP being used. If the
56 conditions are met, a short jump is made to the label given by the operand
57 to LOOP. If the address-size attribute is 16 bits, the CX register is used
58 as the count register; otherwise the ECX register is used. The operand
59 of LOOP must be in the range from 128 (decimal) bytes before the
60 instruction to 127 bytes ahead of the instruction.
61 <P>
62 The LOOP instructions provide iteration control and combine loop index
63 management with conditional branching. Use the LOOP instruction by
64 loading an unsigned iteration count into the count register, then code the
65 LOOP at the end of a series of instructions to be iterated. The
66 destination of LOOP is a label that points to the beginning of the
67 iteration.
69 <H2>Flags Affected</H2>
71 None
73 <H2>Protected Mode Exceptions</H2>
75 #GP(0) if the offset jumped to is beyond the limits of the current code
76 segment
78 <H2>Real Address Mode Exceptions</H2>
80 None
82 <H2>Virtual 8086 Mode Exceptions</H2>
84 None
87 <P>
88 <HR>
89 <P>
90 <B>up:</B> <A HREF="c17.htm">
91 Chapter 17 -- 80386 Instruction Set</A><BR>
92 <B>prev:</B><A HREF="LODS.htm"> LODS/LODSB/LODSW/LODSD Load String Operand</A><BR>
93 <B>next:</B><A HREF="LSL.htm"> LSL Load Segment Limit</A>
94 </BODY>