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