You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
7.3 KiB
HTML
153 lines
7.3 KiB
HTML
4 years ago
|
<html lang="en">
|
||
|
<head>
|
||
|
<title>Basic Asm - Using the GNU Compiler Collection (GCC)</title>
|
||
|
<meta http-equiv="Content-Type" content="text/html">
|
||
|
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
|
||
|
<meta name="generator" content="makeinfo 4.13">
|
||
|
<link title="Top" rel="start" href="index.html#Top">
|
||
|
<link rel="up" href="Using-Assembly-Language-with-C.html#Using-Assembly-Language-with-C" title="Using Assembly Language with C">
|
||
|
<link rel="next" href="Extended-Asm.html#Extended-Asm" title="Extended Asm">
|
||
|
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
|
||
|
<!--
|
||
|
Copyright (C) 1988-2015 Free Software Foundation, Inc.
|
||
|
|
||
|
Permission is granted to copy, distribute and/or modify this document
|
||
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
||
|
any later version published by the Free Software Foundation; with the
|
||
|
Invariant Sections being ``Funding Free Software'', the Front-Cover
|
||
|
Texts being (a) (see below), and with the Back-Cover Texts being (b)
|
||
|
(see below). A copy of the license is included in the section entitled
|
||
|
``GNU Free Documentation License''.
|
||
|
|
||
|
(a) The FSF's Front-Cover Text is:
|
||
|
|
||
|
A GNU Manual
|
||
|
|
||
|
(b) The FSF's Back-Cover Text is:
|
||
|
|
||
|
You have freedom to copy and modify this GNU Manual, like GNU
|
||
|
software. Copies published by the Free Software Foundation raise
|
||
|
funds for GNU development.-->
|
||
|
<meta http-equiv="Content-Style-Type" content="text/css">
|
||
|
<style type="text/css"><!--
|
||
|
pre.display { font-family:inherit }
|
||
|
pre.format { font-family:inherit }
|
||
|
pre.smalldisplay { font-family:inherit; font-size:smaller }
|
||
|
pre.smallformat { font-family:inherit; font-size:smaller }
|
||
|
pre.smallexample { font-size:smaller }
|
||
|
pre.smalllisp { font-size:smaller }
|
||
|
span.sc { font-variant:small-caps }
|
||
|
span.roman { font-family:serif; font-weight:normal; }
|
||
|
span.sansserif { font-family:sans-serif; font-weight:normal; }
|
||
|
--></style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<div class="node">
|
||
|
<a name="Basic-Asm"></a>
|
||
|
<p>
|
||
|
Next: <a rel="next" accesskey="n" href="Extended-Asm.html#Extended-Asm">Extended Asm</a>,
|
||
|
Up: <a rel="up" accesskey="u" href="Using-Assembly-Language-with-C.html#Using-Assembly-Language-with-C">Using Assembly Language with C</a>
|
||
|
<hr>
|
||
|
</div>
|
||
|
|
||
|
<h4 class="subsection">6.43.1 Basic Asm — Assembler Instructions Without Operands</h4>
|
||
|
|
||
|
<p><a name="index-basic-_0040code_007basm_007d-3421"></a><a name="index-assembly-language-in-C_002c-basic-3422"></a>
|
||
|
A basic <code>asm</code> statement has the following syntax:
|
||
|
|
||
|
<pre class="example"> asm <span class="roman">[</span> volatile <span class="roman">]</span> ( <var>AssemblerInstructions</var> )
|
||
|
</pre>
|
||
|
<p>The <code>asm</code> keyword is a GNU extension.
|
||
|
When writing code that can be compiled with <samp><span class="option">-ansi</span></samp> and the
|
||
|
various <samp><span class="option">-std</span></samp> options, use <code>__asm__</code> instead of
|
||
|
<code>asm</code> (see <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>).
|
||
|
|
||
|
<h5 class="subsubheading">Qualifiers</h5>
|
||
|
|
||
|
<dl>
|
||
|
<dt><code>volatile</code><dd>The optional <code>volatile</code> qualifier has no effect.
|
||
|
All basic <code>asm</code> blocks are implicitly volatile.
|
||
|
</dl>
|
||
|
|
||
|
<h5 class="subsubheading">Parameters</h5>
|
||
|
|
||
|
<dl>
|
||
|
<dt><var>AssemblerInstructions</var><dd>This is a literal string that specifies the assembler code. The string can
|
||
|
contain any instructions recognized by the assembler, including directives.
|
||
|
GCC does not parse the assembler instructions themselves and
|
||
|
does not know what they mean or even whether they are valid assembler input.
|
||
|
|
||
|
<p>You may place multiple assembler instructions together in a single <code>asm</code>
|
||
|
string, separated by the characters normally used in assembly code for the
|
||
|
system. A combination that works in most places is a newline to break the
|
||
|
line, plus a tab character (written as ‘<samp><span class="samp">\n\t</span></samp>’).
|
||
|
Some assemblers allow semicolons as a line separator. However,
|
||
|
note that some assembler dialects use semicolons to start a comment.
|
||
|
</dl>
|
||
|
|
||
|
<h5 class="subsubheading">Remarks</h5>
|
||
|
|
||
|
<p>Using extended <code>asm</code> typically produces smaller, safer, and more
|
||
|
efficient code, and in most cases it is a better solution than basic
|
||
|
<code>asm</code>. However, there are two situations where only basic <code>asm</code>
|
||
|
can be used:
|
||
|
|
||
|
<ul>
|
||
|
<li>Extended <code>asm</code> statements have to be inside a C
|
||
|
function, so to write inline assembly language at file scope (“top-level”),
|
||
|
outside of C functions, you must use basic <code>asm</code>.
|
||
|
You can use this technique to emit assembler directives,
|
||
|
define assembly language macros that can be invoked elsewhere in the file,
|
||
|
or write entire functions in assembly language.
|
||
|
|
||
|
<li>Functions declared
|
||
|
with the <code>naked</code> attribute also require basic <code>asm</code>
|
||
|
(see <a href="Function-Attributes.html#Function-Attributes">Function Attributes</a>).
|
||
|
</ul>
|
||
|
|
||
|
<p>Safely accessing C data and calling functions from basic <code>asm</code> is more
|
||
|
complex than it may appear. To access C data, it is better to use extended
|
||
|
<code>asm</code>.
|
||
|
|
||
|
<p>Do not expect a sequence of <code>asm</code> statements to remain perfectly
|
||
|
consecutive after compilation. If certain instructions need to remain
|
||
|
consecutive in the output, put them in a single multi-instruction <code>asm</code>
|
||
|
statement. Note that GCC's optimizers can move <code>asm</code> statements
|
||
|
relative to other code, including across jumps.
|
||
|
|
||
|
<p><code>asm</code> statements may not perform jumps into other <code>asm</code> statements.
|
||
|
GCC does not know about these jumps, and therefore cannot take
|
||
|
account of them when deciding how to optimize. Jumps from <code>asm</code> to C
|
||
|
labels are only supported in extended <code>asm</code>.
|
||
|
|
||
|
<p>Under certain circumstances, GCC may duplicate (or remove duplicates of) your
|
||
|
assembly code when optimizing. This can lead to unexpected duplicate
|
||
|
symbol errors during compilation if your assembly code defines symbols or
|
||
|
labels.
|
||
|
|
||
|
<p>Since GCC does not parse the <var>AssemblerInstructions</var>, it has no
|
||
|
visibility of any symbols it references. This may result in GCC discarding
|
||
|
those symbols as unreferenced.
|
||
|
|
||
|
<p>The compiler copies the assembler instructions in a basic <code>asm</code>
|
||
|
verbatim to the assembly language output file, without
|
||
|
processing dialects or any of the ‘<samp><span class="samp">%</span></samp>’ operators that are available with
|
||
|
extended <code>asm</code>. This results in minor differences between basic
|
||
|
<code>asm</code> strings and extended <code>asm</code> templates. For example, to refer to
|
||
|
registers you might use ‘<samp><span class="samp">%eax</span></samp>’ in basic <code>asm</code> and
|
||
|
‘<samp><span class="samp">%%eax</span></samp>’ in extended <code>asm</code>.
|
||
|
|
||
|
<p>On targets such as x86 that support multiple assembler dialects,
|
||
|
all basic <code>asm</code> blocks use the assembler dialect specified by the
|
||
|
<samp><span class="option">-masm</span></samp> command-line option (see <a href="x86-Options.html#x86-Options">x86 Options</a>).
|
||
|
Basic <code>asm</code> provides no
|
||
|
mechanism to provide different assembler strings for different dialects.
|
||
|
|
||
|
<p>Here is an example of basic <code>asm</code> for i386:
|
||
|
|
||
|
<pre class="example"> /* Note that this code will not compile with -masm=intel */
|
||
|
#define DebugBreak() asm("int $3")
|
||
|
</pre>
|
||
|
</body></html>
|
||
|
|