Tue Jun 9 12:20:05 1998 Alan Modra <alan@spri.levels.unisa.edu.au>
[binutils-gdb.git] / gas / doc / c-i386.texi
1 @c Copyright (C) 1991, 92, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
2 @c This is part of the GAS manual.
3 @c For copying conditions, see the file as.texinfo.
4 @ifset GENERIC
5 @page
6 @node i386-Dependent
7 @chapter 80386 Dependent Features
8 @end ifset
9 @ifclear GENERIC
10 @node Machine Dependencies
11 @chapter 80386 Dependent Features
12 @end ifclear
13
14 @cindex i386 support
15 @cindex i80306 support
16 @menu
17 * i386-Options:: Options
18 * i386-Syntax:: AT&T Syntax versus Intel Syntax
19 * i386-Opcodes:: Opcode Naming
20 * i386-Regs:: Register Naming
21 * i386-prefixes:: Opcode Prefixes
22 * i386-Memory:: Memory References
23 * i386-jumps:: Handling of Jump Instructions
24 * i386-Float:: Floating Point
25 * i386-16bit:: Writing 16-bit Code
26 * i386-Bugs:: AT&T Syntax bugs
27 * i386-Notes:: Notes
28 @end menu
29
30 @node i386-Options
31 @section Options
32
33 @cindex options for i386 (none)
34 @cindex i386 options (none)
35 The 80386 has no machine dependent options.
36
37 @node i386-Syntax
38 @section AT&T Syntax versus Intel Syntax
39
40 @cindex i386 syntax compatibility
41 @cindex syntax compatibility, i386
42 In order to maintain compatibility with the output of @code{@value{GCC}},
43 @code{@value{AS}} supports AT&T System V/386 assembler syntax. This is quite
44 different from Intel syntax. We mention these differences because
45 almost all 80386 documents use Intel syntax. Notable differences
46 between the two syntaxes are:
47
48 @cindex immediate operands, i386
49 @cindex i386 immediate operands
50 @cindex register operands, i386
51 @cindex i386 register operands
52 @cindex jump/call operands, i386
53 @cindex i386 jump/call operands
54 @cindex operand delimiters, i386
55 @itemize @bullet
56 @item
57 AT&T immediate operands are preceded by @samp{$}; Intel immediate
58 operands are undelimited (Intel @samp{push 4} is AT&T @samp{pushl $4}).
59 AT&T register operands are preceded by @samp{%}; Intel register operands
60 are undelimited. AT&T absolute (as opposed to PC relative) jump/call
61 operands are prefixed by @samp{*}; they are undelimited in Intel syntax.
62
63 @cindex i386 source, destination operands
64 @cindex source, destination operands; i386
65 @item
66 AT&T and Intel syntax use the opposite order for source and destination
67 operands. Intel @samp{add eax, 4} is @samp{addl $4, %eax}. The
68 @samp{source, dest} convention is maintained for compatibility with
69 previous Unix assemblers. Note that instructions with more than one
70 source operand, such as the @samp{enter} instruction, do @emph{not} have
71 reversed order. @ref{i386-Bugs}.
72
73 @cindex opcode suffixes, i386
74 @cindex sizes operands, i386
75 @cindex i386 size suffixes
76 @item
77 In AT&T syntax the size of memory operands is determined from the last
78 character of the opcode name. Opcode suffixes of @samp{b}, @samp{w},
79 and @samp{l} specify byte (8-bit), word (16-bit), and long (32-bit)
80 memory references. Intel syntax accomplishes this by prefixes memory
81 operands (@emph{not} the opcodes themselves) with @samp{byte ptr},
82 @samp{word ptr}, and @samp{dword ptr}. Thus, Intel @samp{mov al, byte
83 ptr @var{foo}} is @samp{movb @var{foo}, %al} in AT&T syntax.
84
85 @cindex return instructions, i386
86 @cindex i386 jump, call, return
87 @item
88 Immediate form long jumps and calls are
89 @samp{lcall/ljmp $@var{section}, $@var{offset}} in AT&T syntax; the
90 Intel syntax is
91 @samp{call/jmp far @var{section}:@var{offset}}. Also, the far return
92 instruction
93 is @samp{lret $@var{stack-adjust}} in AT&T syntax; Intel syntax is
94 @samp{ret far @var{stack-adjust}}.
95
96 @cindex sections, i386
97 @cindex i386 sections
98 @item
99 The AT&T assembler does not provide support for multiple section
100 programs. Unix style systems expect all programs to be single sections.
101 @end itemize
102
103 @node i386-Opcodes
104 @section Opcode Naming
105
106 @cindex i386 opcode naming
107 @cindex opcode naming, i386
108 Opcode names are suffixed with one character modifiers which specify the
109 size of operands. The letters @samp{b}, @samp{w}, and @samp{l} specify
110 byte, word, and long operands. If no suffix is specified by an
111 instruction then @code{@value{AS}} tries to
112 fill in the missing suffix based on the destination register operand
113 (the last one by convention). Thus, @samp{mov %ax, %bx} is equivalent
114 to @samp{movw %ax, %bx}; also, @samp{mov $1, %bx} is equivalent to
115 @samp{movw $1, %bx}. Note that this is incompatible with the AT&T Unix
116 assembler which assumes that a missing opcode suffix implies long
117 operand size. (This incompatibility does not affect compiler output
118 since compilers always explicitly specify the opcode suffix.)
119
120 Almost all opcodes have the same names in AT&T and Intel format. There
121 are a few exceptions. The sign extend and zero extend instructions need
122 two sizes to specify them. They need a size to sign/zero extend
123 @emph{from} and a size to zero extend @emph{to}. This is accomplished
124 by using two opcode suffixes in AT&T syntax. Base names for sign extend
125 and zero extend are @samp{movs@dots{}} and @samp{movz@dots{}} in AT&T
126 syntax (@samp{movsx} and @samp{movzx} in Intel syntax). The opcode
127 suffixes are tacked on to this base name, the @emph{from} suffix before
128 the @emph{to} suffix. Thus, @samp{movsbl %al, %edx} is AT&T syntax for
129 ``move sign extend @emph{from} %al @emph{to} %edx.'' Possible suffixes,
130 thus, are @samp{bl} (from byte to long), @samp{bw} (from byte to word),
131 and @samp{wl} (from word to long).
132
133 @cindex conversion instructions, i386
134 @cindex i386 conversion instructions
135 The Intel-syntax conversion instructions
136
137 @itemize @bullet
138 @item
139 @samp{cbw} --- sign-extend byte in @samp{%al} to word in @samp{%ax},
140
141 @item
142 @samp{cwde} --- sign-extend word in @samp{%ax} to long in @samp{%eax},
143
144 @item
145 @samp{cwd} --- sign-extend word in @samp{%ax} to long in @samp{%dx:%ax},
146
147 @item
148 @samp{cdq} --- sign-extend dword in @samp{%eax} to quad in @samp{%edx:%eax},
149 @end itemize
150
151 @noindent
152 are called @samp{cbtw}, @samp{cwtl}, @samp{cwtd}, and @samp{cltd} in
153 AT&T naming. @code{@value{AS}} accepts either naming for these instructions.
154
155 @cindex jump instructions, i386
156 @cindex call instructions, i386
157 Far call/jump instructions are @samp{lcall} and @samp{ljmp} in
158 AT&T syntax, but are @samp{call far} and @samp{jump far} in Intel
159 convention.
160
161 @node i386-Regs
162 @section Register Naming
163
164 @cindex i386 registers
165 @cindex registers, i386
166 Register operands are always prefixed with @samp{%}. The 80386 registers
167 consist of
168
169 @itemize @bullet
170 @item
171 the 8 32-bit registers @samp{%eax} (the accumulator), @samp{%ebx},
172 @samp{%ecx}, @samp{%edx}, @samp{%edi}, @samp{%esi}, @samp{%ebp} (the
173 frame pointer), and @samp{%esp} (the stack pointer).
174
175 @item
176 the 8 16-bit low-ends of these: @samp{%ax}, @samp{%bx}, @samp{%cx},
177 @samp{%dx}, @samp{%di}, @samp{%si}, @samp{%bp}, and @samp{%sp}.
178
179 @item
180 the 8 8-bit registers: @samp{%ah}, @samp{%al}, @samp{%bh},
181 @samp{%bl}, @samp{%ch}, @samp{%cl}, @samp{%dh}, and @samp{%dl} (These
182 are the high-bytes and low-bytes of @samp{%ax}, @samp{%bx},
183 @samp{%cx}, and @samp{%dx})
184
185 @item
186 the 6 section registers @samp{%cs} (code section), @samp{%ds}
187 (data section), @samp{%ss} (stack section), @samp{%es}, @samp{%fs},
188 and @samp{%gs}.
189
190 @item
191 the 3 processor control registers @samp{%cr0}, @samp{%cr2}, and
192 @samp{%cr3}.
193
194 @item
195 the 6 debug registers @samp{%db0}, @samp{%db1}, @samp{%db2},
196 @samp{%db3}, @samp{%db6}, and @samp{%db7}.
197
198 @item
199 the 2 test registers @samp{%tr6} and @samp{%tr7}.
200
201 @item
202 the 8 floating point register stack @samp{%st} or equivalently
203 @samp{%st(0)}, @samp{%st(1)}, @samp{%st(2)}, @samp{%st(3)},
204 @samp{%st(4)}, @samp{%st(5)}, @samp{%st(6)}, and @samp{%st(7)}.
205 @end itemize
206
207 @node i386-prefixes
208 @section Opcode Prefixes
209
210 @cindex i386 opcode prefixes
211 @cindex opcode prefixes, i386
212 @cindex prefixes, i386
213 Opcode prefixes are used to modify the following opcode. They are used
214 to repeat string instructions, to provide section overrides, to perform
215 bus lock operations, and to give operand and address size (16-bit
216 operands are specified in an instruction by prefixing what would
217 normally be 32-bit operands with a ``operand size'' opcode prefix).
218 Opcode prefixes are usually given as single-line instructions with no
219 operands, and must directly precede the instruction they act upon. For
220 example, the @samp{scas} (scan string) instruction is repeated with:
221 @smallexample
222 repne
223 scas
224 @end smallexample
225 @noindent
226 or
227 @smallexample
228 repne/scas
229 @end smallexample
230
231 Here is a list of opcode prefixes:
232
233 @cindex section override prefixes, i386
234 @itemize @bullet
235 @item
236 Section override prefixes @samp{cs}, @samp{ds}, @samp{ss}, @samp{es},
237 @samp{fs}, @samp{gs}. These are automatically added by specifying
238 using the @var{section}:@var{memory-operand} form for memory references.
239
240 @cindex size prefixes, i386
241 @item
242 Operand/Address size prefixes @samp{data16} and @samp{addr16}
243 change 32-bit operands/addresses into 16-bit operands/addresses,
244 while @samp{data32} and @samp{addr32} change 16-bit ones (in a
245 @code{.code16} section) into 32-bit operands/addresses.
246
247 @cindex bus lock prefixes, i386
248 @cindex inhibiting interrupts, i386
249 @item
250 The bus lock prefix @samp{lock} inhibits interrupts during
251 execution of the instruction it precedes. (This is only valid with
252 certain instructions; see a 80386 manual for details).
253
254 @cindex coprocessor wait, i386
255 @item
256 The wait for coprocessor prefix @samp{wait} waits for the
257 coprocessor to complete the current instruction. This should never be
258 needed for the 80386/80387 combination.
259
260 @cindex repeat prefixes, i386
261 @item
262 The @samp{rep}, @samp{repe}, and @samp{repne} prefixes are added
263 to string instructions to make them repeat @samp{%ecx} times.
264 @end itemize
265
266 @node i386-Memory
267 @section Memory References
268
269 @cindex i386 memory references
270 @cindex memory references, i386
271 An Intel syntax indirect memory reference of the form
272
273 @smallexample
274 @var{section}:[@var{base} + @var{index}*@var{scale} + @var{disp}]
275 @end smallexample
276
277 @noindent
278 is translated into the AT&T syntax
279
280 @smallexample
281 @var{section}:@var{disp}(@var{base}, @var{index}, @var{scale})
282 @end smallexample
283
284 @noindent
285 where @var{base} and @var{index} are the optional 32-bit base and
286 index registers, @var{disp} is the optional displacement, and
287 @var{scale}, taking the values 1, 2, 4, and 8, multiplies @var{index}
288 to calculate the address of the operand. If no @var{scale} is
289 specified, @var{scale} is taken to be 1. @var{section} specifies the
290 optional section register for the memory operand, and may override the
291 default section register (see a 80386 manual for section register
292 defaults). Note that section overrides in AT&T syntax @emph{must} have
293 be preceded by a @samp{%}. If you specify a section override which
294 coincides with the default section register, @code{@value{AS}} does @emph{not}
295 output any section register override prefixes to assemble the given
296 instruction. Thus, section overrides can be specified to emphasize which
297 section register is used for a given memory operand.
298
299 Here are some examples of Intel and AT&T style memory references:
300
301 @table @asis
302 @item AT&T: @samp{-4(%ebp)}, Intel: @samp{[ebp - 4]}
303 @var{base} is @samp{%ebp}; @var{disp} is @samp{-4}. @var{section} is
304 missing, and the default section is used (@samp{%ss} for addressing with
305 @samp{%ebp} as the base register). @var{index}, @var{scale} are both missing.
306
307 @item AT&T: @samp{foo(,%eax,4)}, Intel: @samp{[foo + eax*4]}
308 @var{index} is @samp{%eax} (scaled by a @var{scale} 4); @var{disp} is
309 @samp{foo}. All other fields are missing. The section register here
310 defaults to @samp{%ds}.
311
312 @item AT&T: @samp{foo(,1)}; Intel @samp{[foo]}
313 This uses the value pointed to by @samp{foo} as a memory operand.
314 Note that @var{base} and @var{index} are both missing, but there is only
315 @emph{one} @samp{,}. This is a syntactic exception.
316
317 @item AT&T: @samp{%gs:foo}; Intel @samp{gs:foo}
318 This selects the contents of the variable @samp{foo} with section
319 register @var{section} being @samp{%gs}.
320 @end table
321
322 Absolute (as opposed to PC relative) call and jump operands must be
323 prefixed with @samp{*}. If no @samp{*} is specified, @code{@value{AS}}
324 always chooses PC relative addressing for jump/call labels.
325
326 Any instruction that has a memory operand, but no register operand,
327 @emph{must} specify its size (byte, word, or long) with an opcode suffix
328 (@samp{b}, @samp{w}, or @samp{l}, respectively).
329
330 @node i386-jumps
331 @section Handling of Jump Instructions
332
333 @cindex jump optimization, i386
334 @cindex i386 jump optimization
335 Jump instructions are always optimized to use the smallest possible
336 displacements. This is accomplished by using byte (8-bit) displacement
337 jumps whenever the target is sufficiently close. If a byte displacement
338 is insufficient a long (32-bit) displacement is used. We do not support
339 word (16-bit) displacement jumps in 32-bit mode (i.e. prefixing the jump instruction
340 with the @samp{data16} opcode prefix), since the 80386 insists upon masking
341 @samp{%eip} to 16 bits after the word displacement is added.
342
343 Note that the @samp{jcxz}, @samp{jecxz}, @samp{loop}, @samp{loopz},
344 @samp{loope}, @samp{loopnz} and @samp{loopne} instructions only come in byte
345 displacements, so that if you use these instructions (@code{@value{GCC}} does
346 not use them) you may get an error message (and incorrect code). The AT&T
347 80386 assembler tries to get around this problem by expanding @samp{jcxz foo}
348 to
349
350 @smallexample
351 jcxz cx_zero
352 jmp cx_nonzero
353 cx_zero: jmp foo
354 cx_nonzero:
355 @end smallexample
356
357 @node i386-Float
358 @section Floating Point
359
360 @cindex i386 floating point
361 @cindex floating point, i386
362 All 80387 floating point types except packed BCD are supported.
363 (BCD support may be added without much difficulty). These data
364 types are 16-, 32-, and 64- bit integers, and single (32-bit),
365 double (64-bit), and extended (80-bit) precision floating point.
366 Each supported type has an opcode suffix and a constructor
367 associated with it. Opcode suffixes specify operand's data
368 types. Constructors build these data types into memory.
369
370 @cindex @code{float} directive, i386
371 @cindex @code{single} directive, i386
372 @cindex @code{double} directive, i386
373 @cindex @code{tfloat} directive, i386
374 @itemize @bullet
375 @item
376 Floating point constructors are @samp{.float} or @samp{.single},
377 @samp{.double}, and @samp{.tfloat} for 32-, 64-, and 80-bit formats.
378 These correspond to opcode suffixes @samp{s}, @samp{l}, and @samp{t}.
379 @samp{t} stands for temporary real, and that the 80387 only supports
380 this format via the @samp{fldt} (load temporary real to stack top) and
381 @samp{fstpt} (store temporary real and pop stack) instructions.
382
383 @cindex @code{word} directive, i386
384 @cindex @code{long} directive, i386
385 @cindex @code{int} directive, i386
386 @cindex @code{quad} directive, i386
387 @item
388 Integer constructors are @samp{.word}, @samp{.long} or @samp{.int}, and
389 @samp{.quad} for the 16-, 32-, and 64-bit integer formats. The corresponding
390 opcode suffixes are @samp{s} (single), @samp{l} (long), and @samp{q}
391 (quad). As with the temporary real format the 64-bit @samp{q} format is
392 only present in the @samp{fildq} (load quad integer to stack top) and
393 @samp{fistpq} (store quad integer and pop stack) instructions.
394 @end itemize
395
396 Register to register operations should not use opcode suffixes.
397 @samp{fstl %st, %st(1)} will give a warning, and be assembled as if you
398 wrote @samp{fst %st, %st(1)}, since all register to register operations
399 use 80-bit floating point operands. (Contrast this with @samp{fstl %st, mem},
400 which converts @samp{%st} from 80-bit to 64-bit floating point format,
401 then stores the result in the 4 byte location @samp{mem})
402
403 @node i386-16bit
404 @section Writing 16-bit Code
405
406 @cindex i386 16-bit code
407 @cindex 16-bit code, i386
408 @cindex real-mode code, i386
409 @cindex @code{code16} directive, i386
410 @cindex @code{code32} directive, i386
411 While @code{@value{AS}} normally writes only ``pure'' 32-bit i386 code,
412 it also supports writing code to run in real mode or in 16-bit protected
413 mode code segments. To do this, put a @samp{.code16} directive before
414 the assembly language instructions to be run in 16-bit mode. You can
415 switch @code{@value{AS}} back to writing normal 32-bit code with the
416 @samp{.code32} directive.
417
418 The code which @code{@value{AS}} generates in 16-bit mode will not
419 necessarily run on a 16-bit pre-80386 processor. To write code that
420 runs on such a processor, you must refrain from using @emph{any} 32-bit
421 constructs which require @code{@value{AS}} to output address or operand
422 size prefixes.
423
424 Note that writing 16-bit code instructions by explicitly specifying a
425 prefix or a suffix within a 32-bit code section generates different
426 machine instructions than those generated for a 16-bit code segment. In a
427 32-bit code section, the following code generates the machine
428 instruction sequence @samp{66 6a 04}, which pushes the value @samp{4} onto
429 the stack, decrementing @samp{%esp} by 2.
430
431 @smallexample
432 pushw $4
433 @end smallexample
434
435 The same code in a 16-bit code section would generate the machine
436 instruction sequence @samp{6a 04} (ie. without the operand size prefix),
437 which is correct since the processor default operand size is assumed to
438 be 16 bits in a 16-bit code section.
439
440 @node i386-Bugs
441 @section AT&T Syntax bugs
442
443 The UnixWare assembler, and probably other AT&T derived ix86 Unix
444 assemblers, generate floating point instructions with reversed source
445 and destination registers in certain cases. Unfortunately, gcc and
446 possibly many other programs use this reversed syntax, so we're stuck
447 with it.
448
449 For example
450
451 @smallexample
452 fsub %st,%st(3)
453 @end smallexample
454 @noindent
455 results in @samp{%st(3)} being updated to @samp{%st - %st(3)} rather
456 than the expected @samp{%st(3) - %st}. This happens with all the
457 non-commutative arithmetic floating point operations with two register
458 operands where the source register is @samp{%st} and the destination
459 register is @samp{%st(i)}.
460
461 @node i386-Notes
462 @section Notes
463
464 @cindex i386 @code{mul}, @code{imul} instructions
465 @cindex @code{mul} instruction, i386
466 @cindex @code{imul} instruction, i386
467 There is some trickery concerning the @samp{mul} and @samp{imul}
468 instructions that deserves mention. The 16-, 32-, and 64-bit expanding
469 multiplies (base opcode @samp{0xf6}; extension 4 for @samp{mul} and 5
470 for @samp{imul}) can be output only in the one operand form. Thus,
471 @samp{imul %ebx, %eax} does @emph{not} select the expanding multiply;
472 the expanding multiply would clobber the @samp{%edx} register, and this
473 would confuse @code{@value{GCC}} output. Use @samp{imul %ebx} to get the
474 64-bit product in @samp{%edx:%eax}.
475
476 We have added a two operand form of @samp{imul} when the first operand
477 is an immediate mode expression and the second operand is a register.
478 This is just a shorthand, so that, multiplying @samp{%eax} by 69, for
479 example, can be done with @samp{imul $69, %eax} rather than @samp{imul
480 $69, %eax, %eax}.
481