i386_002dMemory.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the GNU Assembler "as".
  4. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  5. Permission is granted to copy, distribute and/or modify this document
  6. under the terms of the GNU Free Documentation License, Version 1.3
  7. or any later version published by the Free Software Foundation;
  8. with no Invariant Sections, with no Front-Cover Texts, and with no
  9. Back-Cover Texts. A copy of the license is included in the
  10. section entitled "GNU Free Documentation License".
  11. -->
  12. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <title>Using as: i386-Memory</title>
  15. <meta name="description" content="Using as: i386-Memory">
  16. <meta name="keywords" content="Using as: i386-Memory">
  17. <meta name="resource-type" content="document">
  18. <meta name="distribution" content="global">
  19. <meta name="Generator" content="makeinfo">
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  21. <link href="index.html#Top" rel="start" title="Top">
  22. <link href="AS-Index.html#AS-Index" rel="index" title="AS Index">
  23. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  24. <link href="i386_002dDependent.html#i386_002dDependent" rel="up" title="i386-Dependent">
  25. <link href="i386_002dJumps.html#i386_002dJumps" rel="next" title="i386-Jumps">
  26. <link href="i386_002dPrefixes.html#i386_002dPrefixes" rel="prev" title="i386-Prefixes">
  27. <style type="text/css">
  28. <!--
  29. a.summary-letter {text-decoration: none}
  30. blockquote.smallquotation {font-size: smaller}
  31. div.display {margin-left: 3.2em}
  32. div.example {margin-left: 3.2em}
  33. div.indentedblock {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  37. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  38. div.smalllisp {margin-left: 3.2em}
  39. kbd {font-style:oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. pre.smalldisplay {font-family: inherit; font-size: smaller}
  45. pre.smallexample {font-size: smaller}
  46. pre.smallformat {font-family: inherit; font-size: smaller}
  47. pre.smalllisp {font-size: smaller}
  48. span.nocodebreak {white-space:nowrap}
  49. span.nolinebreak {white-space:nowrap}
  50. span.roman {font-family:serif; font-weight:normal}
  51. span.sansserif {font-family:sans-serif; font-weight:normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  57. <a name="i386_002dMemory"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="i386_002dJumps.html#i386_002dJumps" accesskey="n" rel="next">i386-Jumps</a>, Previous: <a href="i386_002dPrefixes.html#i386_002dPrefixes" accesskey="p" rel="prev">i386-Prefixes</a>, Up: <a href="i386_002dDependent.html#i386_002dDependent" accesskey="u" rel="up">i386-Dependent</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="Memory-References"></a>
  64. <h4 class="subsection">9.15.7 Memory References</h4>
  65. <a name="index-i386-memory-references"></a>
  66. <a name="index-memory-references_002c-i386"></a>
  67. <a name="index-x86_002d64-memory-references"></a>
  68. <a name="index-memory-references_002c-x86_002d64"></a>
  69. <p>An Intel syntax indirect memory reference of the form
  70. </p>
  71. <div class="smallexample">
  72. <pre class="smallexample"><var>section</var>:[<var>base</var> + <var>index</var>*<var>scale</var> + <var>disp</var>]
  73. </pre></div>
  74. <p>is translated into the AT&amp;T syntax
  75. </p>
  76. <div class="smallexample">
  77. <pre class="smallexample"><var>section</var>:<var>disp</var>(<var>base</var>, <var>index</var>, <var>scale</var>)
  78. </pre></div>
  79. <p>where <var>base</var> and <var>index</var> are the optional 32-bit base and
  80. index registers, <var>disp</var> is the optional displacement, and
  81. <var>scale</var>, taking the values 1, 2, 4, and 8, multiplies <var>index</var>
  82. to calculate the address of the operand. If no <var>scale</var> is
  83. specified, <var>scale</var> is taken to be 1. <var>section</var> specifies the
  84. optional section register for the memory operand, and may override the
  85. default section register (see a 80386 manual for section register
  86. defaults). Note that section overrides in AT&amp;T syntax <em>must</em>
  87. be preceded by a &lsquo;<samp>%</samp>&rsquo;. If you specify a section override which
  88. coincides with the default section register, <code>as</code> does <em>not</em>
  89. output any section register override prefixes to assemble the given
  90. instruction. Thus, section overrides can be specified to emphasize which
  91. section register is used for a given memory operand.
  92. </p>
  93. <p>Here are some examples of Intel and AT&amp;T style memory references:
  94. </p>
  95. <dl compact="compact">
  96. <dt>AT&amp;T: &lsquo;<samp>-4(%ebp)</samp>&rsquo;, Intel: &lsquo;<samp>[ebp - 4]</samp>&rsquo;</dt>
  97. <dd><p><var>base</var> is &lsquo;<samp>%ebp</samp>&rsquo;; <var>disp</var> is &lsquo;<samp>-4</samp>&rsquo;. <var>section</var> is
  98. missing, and the default section is used (&lsquo;<samp>%ss</samp>&rsquo; for addressing with
  99. &lsquo;<samp>%ebp</samp>&rsquo; as the base register). <var>index</var>, <var>scale</var> are both missing.
  100. </p>
  101. </dd>
  102. <dt>AT&amp;T: &lsquo;<samp>foo(,%eax,4)</samp>&rsquo;, Intel: &lsquo;<samp>[foo + eax*4]</samp>&rsquo;</dt>
  103. <dd><p><var>index</var> is &lsquo;<samp>%eax</samp>&rsquo; (scaled by a <var>scale</var> 4); <var>disp</var> is
  104. &lsquo;<samp>foo</samp>&rsquo;. All other fields are missing. The section register here
  105. defaults to &lsquo;<samp>%ds</samp>&rsquo;.
  106. </p>
  107. </dd>
  108. <dt>AT&amp;T: &lsquo;<samp>foo(,1)</samp>&rsquo;; Intel &lsquo;<samp>[foo]</samp>&rsquo;</dt>
  109. <dd><p>This uses the value pointed to by &lsquo;<samp>foo</samp>&rsquo; as a memory operand.
  110. Note that <var>base</var> and <var>index</var> are both missing, but there is only
  111. <em>one</em> &lsquo;<samp>,</samp>&rsquo;. This is a syntactic exception.
  112. </p>
  113. </dd>
  114. <dt>AT&amp;T: &lsquo;<samp>%gs:foo</samp>&rsquo;; Intel &lsquo;<samp>gs:foo</samp>&rsquo;</dt>
  115. <dd><p>This selects the contents of the variable &lsquo;<samp>foo</samp>&rsquo; with section
  116. register <var>section</var> being &lsquo;<samp>%gs</samp>&rsquo;.
  117. </p></dd>
  118. </dl>
  119. <p>Absolute (as opposed to PC relative) call and jump operands must be
  120. prefixed with &lsquo;<samp>*</samp>&rsquo;. If no &lsquo;<samp>*</samp>&rsquo; is specified, <code>as</code>
  121. always chooses PC relative addressing for jump/call labels.
  122. </p>
  123. <p>Any instruction that has a memory operand, but no register operand,
  124. <em>must</em> specify its size (byte, word, long, or quadruple) with an
  125. instruction mnemonic suffix (&lsquo;<samp>b</samp>&rsquo;, &lsquo;<samp>w</samp>&rsquo;, &lsquo;<samp>l</samp>&rsquo; or &lsquo;<samp>q</samp>&rsquo;,
  126. respectively).
  127. </p>
  128. <p>The x86-64 architecture adds an RIP (instruction pointer relative)
  129. addressing. This addressing mode is specified by using &lsquo;<samp>rip</samp>&rsquo; as a
  130. base register. Only constant offsets are valid. For example:
  131. </p>
  132. <dl compact="compact">
  133. <dt>AT&amp;T: &lsquo;<samp>1234(%rip)</samp>&rsquo;, Intel: &lsquo;<samp>[rip + 1234]</samp>&rsquo;</dt>
  134. <dd><p>Points to the address 1234 bytes past the end of the current
  135. instruction.
  136. </p>
  137. </dd>
  138. <dt>AT&amp;T: &lsquo;<samp>symbol(%rip)</samp>&rsquo;, Intel: &lsquo;<samp>[rip + symbol]</samp>&rsquo;</dt>
  139. <dd><p>Points to the <code>symbol</code> in RIP relative way, this is shorter than
  140. the default absolute addressing.
  141. </p></dd>
  142. </dl>
  143. <p>Other addressing modes remain unchanged in x86-64 architecture, except
  144. registers used are 64-bit instead of 32-bit.
  145. </p>
  146. <hr>
  147. <div class="header">
  148. <p>
  149. Next: <a href="i386_002dJumps.html#i386_002dJumps" accesskey="n" rel="next">i386-Jumps</a>, Previous: <a href="i386_002dPrefixes.html#i386_002dPrefixes" accesskey="p" rel="prev">i386-Prefixes</a>, Up: <a href="i386_002dDependent.html#i386_002dDependent" accesskey="u" rel="up">i386-Dependent</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
  150. </div>
  151. </body>
  152. </html>