<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="generator" content="pdf2htmlEX">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://static.pudn.com/base/css/base.min.css">
<link rel="stylesheet" href="https://static.pudn.com/base/css/fancy.min.css">
<link rel="stylesheet" href="https://static.pudn.com/prod/directory_preview_static/626451364c65f41259cae270/raw.css">
<script src="https://static.pudn.com/base/js/compatibility.min.js"></script>
<script src="https://static.pudn.com/base/js/pdf2htmlEX.min.js"></script>
<script>
try{
pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});
}catch(e){}
</script>
<title></title>
</head>
<body>
<div id="sidebar" style="display: none">
<div id="outline">
</div>
</div>
<div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/626451364c65f41259cae270/bg1.jpg"><div class="t m0 x1 h2 y1 ff1 fs0 fc0 sc0 ls0 ws0">© Copyright IBM Corporation 2001<span class="_ _0"> </span>T<span class="_ _1"></span>rademarks</div><div class="t m0 x1 h2 y2 ff1 fs0 fc1 sc0 ls0 ws0">Inline assembly for x86 in Linux<span class="_ _2"> </span>Page 1 of 9</div><div class="t m0 x1 h3 y3 ff2 fs1 fc2 sc0 ls0 ws0">Inline assembly for x86 in Linux</div><div class="t m0 x1 h4 y4 ff2 fs2 fc2 sc0 ls0 ws0">Putting the pieces together</div><div class="t m0 x1 h5 y5 ff1 fs3 fc0 sc0 ls0 ws0">Bharata Rao<span class="fc1"> (</span>rbharata@in.ibm.com<span class="fc1">)</span></div><div class="t m0 x1 h5 y6 ff1 fs3 fc1 sc0 ls0 ws0">IBM Linux T<span class="_ _3"></span>echnology Center<span class="_ _4"></span>, IBM Software Labs, India</div><div class="t m0 x2 h5 y5 ff1 fs3 fc1 sc0 ls0 ws0">01 March 2001</div><div class="t m0 x3 h5 y7 ff1 fs3 fc1 sc0 ls0 ws0">Bharata B. Rao offers a guide to the overall use and structure of inline assembly for x86 on the</div><div class="t m0 x3 h5 y8 ff1 fs3 fc1 sc0 ls0 ws0">Linux platform. He covers the basics of inline assembly and its various usages, gives some</div><div class="t m0 x3 h5 y9 ff1 fs3 fc1 sc0 ls0 ws0">basic inline assembly coding guidelines, and explains the instances of inline assembly code in</div><div class="t m0 x3 h5 ya ff1 fs3 fc1 sc0 ls0 ws0">the Linux kernel.</div><div class="t m0 x1 h5 yb ff1 fs3 fc1 sc0 ls0 ws0">If you're a Linux kernel developer<span class="_ _4"></span>, you probably find yourself coding highly architecture-dependent</div><div class="t m0 x1 h5 yc ff1 fs3 fc1 sc0 ls0 ws0">functions or optimizing a code path pretty often. And you probably do this by inserting assembly</div><div class="t m0 x1 h5 yd ff1 fs3 fc1 sc0 ls0 ws0">language instructions into the middle of C statements (a method otherwise known as inline</div><div class="t m0 x1 h5 ye ff1 fs3 fc1 sc0 ls0 ws0">assembly). Let's take a look at the specific usage of inline assembly in Linux. (We'll limit our</div><div class="t m0 x1 h5 yf ff1 fs3 fc1 sc0 ls0 ws0">discussion to the IA32 assembly<span class="_ _4"></span>.)</div><div class="t m0 x1 h4 y10 ff2 fs2 fc2 sc0 ls0 ws0">GNU assembler syntax in brief</div><div class="t m0 x1 h5 y11 ff1 fs3 fc1 sc0 ls0 ws0">Let's first look at the basic assembler syntax used in Linux. GCC, the GNU C Compiler for Linux,</div><div class="t m0 x1 h5 y12 ff1 fs3 fc1 sc0 ls0 ws0">uses A<span class="_ _4"></span>T&T assembly syntax. Some of the basic rules of this syntax are listed below<span class="_ _4"></span>. (The list is by</div><div class="t m0 x1 h5 y13 ff1 fs3 fc1 sc0 ls0 ws0">no means complete; I've included only those rules pertinent to inline assembly<span class="_ _4"></span>.)</div><div class="t m0 x1 h6 y14 ff2 fs3 fc2 sc0 ls0 ws0">Register naming</div><div class="t m0 x1 h5 y15 ff1 fs3 fc1 sc0 ls0 ws0">Register names are prefixed by %. That is, if eax has to be used, it should be used as %eax.</div><div class="t m0 x1 h6 y16 ff2 fs3 fc2 sc0 ls0 ws0">Source and destination ordering</div><div class="t m0 x1 h5 y17 ff1 fs3 fc1 sc0 ls0 ws0">In any instruction, source comes first and destination follows. This differs from Intel syntax, where</div><div class="t m0 x1 h5 y18 ff1 fs3 fc1 sc0 ls0 ws0">source comes after destination.</div><div class="t m0 x1 h7 y19 ff3 fs4 fc3 sc0 ls0 ws0"> mov %eax, %ebx, transfers the contents of eax to ebx.</div><div class="t m0 x1 h6 y1a ff2 fs3 fc2 sc0 ls0 ws0">Size of operand</div><div class="t m0 x1 h5 y1b ff1 fs3 fc1 sc0 ls0 ws0">The instructions are suffixed by b, w<span class="_ _4"></span>, or l, depending on whether the operand is a byte, word, or</div><div class="t m0 x1 h5 y1c ff1 fs3 fc1 sc0 ls0 ws0">long. This is not mandatory; GCC tries provide the appropriate suffix by reading the operands. But</div><div class="t m0 x1 h5 y1d ff1 fs3 fc1 sc0 ls0 ws0">specifying the suffixes manually improves the code readability and eliminates the possibility of the</div><div class="t m0 x1 h5 y1e ff1 fs3 fc1 sc0 ls0 ws0">compilers guessing incorrectly<span class="_ _4"></span>.</div><a class="l" rel='nofollow' onclick='return false;'><div class="d m1"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m1"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m1"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m1"></div></a></div><div class="pi" data-data='{"ctm":[1.612700,0.000000,0.000000,1.612700,0.000000,0.000000]}'></div></div>
</body>
</html>