1 <!DOCTYPE HTML PUBLIC
"-//IETF//DTD HTML 2.0//EN">
4 <TITLE>80386 Programmer's Reference Manual -- Opcode RET
</TITLE>
7 <B>up:
</B> <A HREF=
"c17.htm">
8 Chapter
17 --
80386 Instruction Set
</A><BR>
9 <B>prev:
</B><A HREF=
"REP.htm"> REP/REPE/REPZ/REPNE/REPNZ Repeat Following String Operation
</A><BR>
10 <B>next:
</B><A HREF=
"SAHF.htm"> SAHF Store AH into Flags
</A>
14 <H1>RET -- Return from Procedure
</H1>
17 Opcode Instruction Clocks Description
19 C3 RET
10+m Return (near) to caller
20 CB RET
18+m,pm=
32+m Return (far) to caller, same
22 CB RET pm=
68 Return (far), lesser privilege,
24 C2 iw RET imm16
10+m Return (near), pop imm16 bytes of
26 CA iw RET imm16
18+m,pm=
32+m Return (far), same privilege, pop
28 CA iw RET imm16 pm=
68 Return (far), lesser privilege, pop
36 IF instruction = near RET
41 EIP := EIP AND
0000FFFFH;
42 ELSE (* OperandSize =
32 *)
45 IF instruction has immediate operand THEN eSP := eSP + imm16; FI;
48 IF (PE =
0 OR (PE =
1 AND VM =
1))
49 (* real mode or virtual
8086 mode *)
50 AND instruction = far RET
55 EIP := EIP AND
0000FFFFH;
56 CS := Pop(); (*
16-bit pop *)
57 ELSE (* OperandSize =
32 *)
59 CS := Pop(); (*
32-bit pop, high-order
16-bits discarded *)
61 IF instruction has immediate operand THEN eSP := eSP + imm16; FI;
64 IF (PE =
1 AND VM =
0) (* Protected mode, not V86 mode *)
65 AND instruction = far RET
68 THEN Third word on stack must be within stack limits else #SS(
0);
69 ELSE Second word on stack must be within stack limits else #SS(
0);
71 Return selector RPL must be
>= CPL ELSE #GP(return selector)
72 IF return selector RPL = CPL
74 ELSE GOTO OUTER-PRIVILEGE-LEVEL;
79 Return selector must be non-null ELSE #GP(
0)
80 Selector index must be within its descriptor table limits ELSE
82 Descriptor AR byte must indicate code segment ELSE #GP(selector)
84 THEN code segment DPL must equal CPL;
88 THEN code segment DPL must be <= CPL;
91 Code segment must be present ELSE #NP(selector);
92 Top word on stack must be within stack limits ELSE #SS(
0);
93 IP must be in code segment limit ELSE #GP(
0);
96 Load CS:EIP from stack
97 Load CS register with descriptor
98 Increment eSP by
8 plus the immediate offset if it exists
99 ELSE (* OperandSize=
16 *)
100 Load CS:IP from stack
101 Load CS register with descriptor
102 Increment eSP by
4 plus the immediate offset if it exists
105 OUTER-PRIVILEGE-LEVEL:
107 THEN Top (
16+immediate) bytes on stack must be within stack limits
109 ELSE Top (
8+immediate) bytes on stack must be within stack limits ELSE
112 Examine return CS selector and associated descriptor:
113 Selector must be non-null ELSE #GP(
0);
114 Selector index must be within its descriptor table limits ELSE
116 Descriptor AR byte must indicate code segment ELSE #GP(selector);
118 THEN code segment DPL must equal return selector RPL
122 THEN code segment DPL must be <= return selector RPL;
125 Segment must be present ELSE #NP(selector)
126 Examine return SS selector and associated descriptor:
127 Selector must be non-null ELSE #GP(
0);
128 Selector index must be within its descriptor table limits
130 Selector RPL must equal the RPL of the return CS selector ELSE
132 Descriptor AR byte must indicate a writable data segment ELSE
134 Descriptor DPL must equal the RPL of the return CS selector ELSE
136 Segment must be present ELSE #NP(selector);
137 IP must be in code segment limit ELSE #GP(
0);
138 Set CPL to the RPL of the return CS selector;
141 Load CS:EIP from stack;
143 Increment eSP by
8 plus the immediate offset if it exists;
144 Load SS:eSP from stack;
145 ELSE (* OperandMode=
16 *)
146 Load CS:IP from stack;
148 Increment eSP by
4 plus the immediate offset if it exists;
149 Load SS:eSP from stack;
151 Load the CS register with the return CS descriptor;
152 Load the SS register with the return SS descriptor;
153 For each of ES, FS, GS, and DS
155 IF the current register setting is not valid for the outer level,
156 set the register to null (selector := AR :=
0);
157 To be valid, the register setting must satisfy the following
159 Selector index must be within descriptor table limits;
160 Descriptor AR byte must indicate data or readable code segment;
161 IF segment is data or non-conforming code, THEN
162 DPL must be
>= CPL, or DPL must be
>= RPL;
169 RET transfers control to a return address located on the stack. The
170 address is usually placed on the stack by a
171 <A HREF=
"CALL.htm">CALL
</A> instruction, and the
172 return is made to the instruction that follows the
173 <A HREF=
"CALL.htm">CALL
</A>.
175 The optional numeric parameter to RET gives the number of stack bytes
176 (OperandMode=
16) or words (OperandMode=
32) to be released after the return
177 address is popped. These items are typically used as input parameters to the
180 For the intrasegment (near) return, the address on the stack is a segment
181 offset, which is popped into the instruction pointer. The CS register is
182 unchanged. For the intersegment (far) return, the address on the stack
183 is a long pointer. The offset is popped first, followed by the selector.
185 In real mode, CS and IP are loaded directly. In Protected Mode, an
186 intersegment return causes the processor to check the descriptor
187 addressed by the return selector. The AR byte of the descriptor must
188 indicate a code segment of equal or lesser privilege (or greater or equal
189 numeric value) than the current privilege level. Returns to a lesser
190 privilege level cause the stack to be reloaded from the value saved beyond
193 The DS, ES, FS, and GS segment registers can be set to
0 by the RET
194 instruction during an interlevel transfer. If these registers refer to
195 segments that cannot be used by the new privilege level, they are set to
196 0 to prevent unauthorized access from the new privilege level.
198 <H2>Flags Affected
</H2>
202 <H2>Protected Mode Exceptions
</H2>
204 #GP, #NP, or #SS, as described under
"<H2>Operation</H2>" above; #PF(fault-code) for
207 <H2>Real Address Mode Exceptions
</H2>
209 Interrupt
13 if any part of the operand would be outside the effective
210 address space from
0 to
0FFFFH
212 <H2>Virtual
8086 Mode Exceptions
</H2>
214 Same exceptions as in Real Address Mode; #PF(fault-code) for a page
221 <B>up:
</B> <A HREF=
"c17.htm">
222 Chapter
17 --
80386 Instruction Set
</A><BR>
223 <B>prev:
</B><A HREF=
"REP.htm"> REP/REPE/REPZ/REPNE/REPNZ Repeat Following String Operation
</A><BR>
224 <B>next:
</B><A HREF=
"SAHF.htm"> SAHF Store AH into Flags
</A>