﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-模拟器开发</title><link>http://www.cnitblog.com/ken0917/</link><description>8位16位32位游戏机模拟器
Emulator porting,developing.....</description><language>zh-cn</language><lastBuildDate>Mon, 04 May 2026 22:21:36 GMT</lastBuildDate><pubDate>Mon, 04 May 2026 22:21:36 GMT</pubDate><ttl>60</ttl><item><title>arm LDR在非4字节对齐的地址上会如何表现</title><link>http://www.cnitblog.com/ken0917/archive/2010/06/14/66731.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Sun, 13 Jun 2010 23:56:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2010/06/14/66731.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/66731.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2010/06/14/66731.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/66731.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/66731.html</trackback:ping><description><![CDATA[先看手册怎么说：<br>will normally use a word aligned address. However, an<br>address offset from a word boundary will cause the data to be<br>rotated into the register so that the addressed byte occupies<br>bits 0 to 7. This means that half-words accessed at offsets 0<br>and 2 from the word boundary will be correctly loaded into<br>bits 0 through 15 of the register. Two shift operations are then<br>required to clear or to sign extend the upper 16 bits. This is<br>illustrated in Figure 5-15: Little Endian offset addressing on<br>page 5-21.<br>A word store (STR) should generate a word aligned address.<br>The word presented to the data bus is not affected if the<br>address is not word aligned. That is, bit 31 of the register<br>being stored always appears on data bus output 31.<br><br>就是说会rotate移位，一个0x12345678的值，在地址0x2，读取出来，会变成0x56781234<br>如果在地址0x1，读取出来，会变成0x34567812或者类似的效果，呵呵，本来以为会crash，然后说data misalignment，唉，没os就是麻烦。。。。。。<br><br><img src ="http://www.cnitblog.com/ken0917/aggbug/66731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2010-06-14 07:56 <a href="http://www.cnitblog.com/ken0917/archive/2010/06/14/66731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>rvds 要求8字节栈对齐操作</title><link>http://www.cnitblog.com/ken0917/archive/2010/06/12/66667.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Sat, 12 Jun 2010 09:11:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2010/06/12/66667.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/66667.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2010/06/12/66667.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/66667.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/66667.html</trackback:ping><description><![CDATA[This RVDS/RVCT&nbsp;linker error is given where a stack alignment conflict is
detected in object code. The "ABI for the ARM Architecture" demands
that code maintains 8-byte stack alignment at its interfaces. This
allows efficient use of&nbsp;<code class="aainlinemono">LDRD</code>&nbsp;and&nbsp;<code class="aainlinemono">STRD</code>&nbsp;instructions (in ARM Architecture 5TE
and later) to access 8-byte-aligned "<code class="aainlinemono">double</code>"
and "<code class="aainlinemono">long long</code>" data types.<img src ="http://www.cnitblog.com/ken0917/aggbug/66667.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2010-06-12 17:11 <a href="http://www.cnitblog.com/ken0917/archive/2010/06/12/66667.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>arm movs指令如何影响carry （C）标志位</title><link>http://www.cnitblog.com/ken0917/archive/2010/05/06/65849.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Thu, 06 May 2010 06:56:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2010/05/06/65849.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/65849.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2010/05/06/65849.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/65849.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/65849.html</trackback:ping><description><![CDATA[arm movs指令如何影响carry （C）标志位<br>MOVS R1,R1,LSR#1<br>如果上面r1右移之前最低位为1，则C为1，否则为0<br><img src ="http://www.cnitblog.com/ken0917/aggbug/65849.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2010-05-06 14:56 <a href="http://www.cnitblog.com/ken0917/archive/2010/05/06/65849.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Predeclared ARM Register Names  </title><link>http://www.cnitblog.com/ken0917/archive/2010/04/17/65382.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Sat, 17 Apr 2010 15:10:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2010/04/17/65382.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/65382.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2010/04/17/65382.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/65382.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/65382.html</trackback:ping><description><![CDATA[<p>By default, the assembler predeclares the following register names:</p>
<ul>
    <li><strong>R0&#8211;R15</strong> and <strong>r0&#8211;r15</strong><br></li>
    <li><strong>c0&#8211;c15</strong> coprocessor registers<br></li>
    <li><strong>p0&#8211;p15</strong> coprocessor registers<br></li>
    <li><strong>a1-a4</strong> scratch registers, synonymous with <strong>r0-r3</strong><br></li>
    <li><strong>v1-v8 </strong>variable registers, synonymous with <strong>r4-r11</strong><br></li>
    <li><strong>sb and SB</strong>stack base, synonymous with <strong>r9</strong><br></li>
    <li><strong>sl</strong> and <strong>SL</strong> stack base, synonymous with <strong>r10</strong><br></li>
    <li><strong>fp</strong> and <strong>FP</strong> frame pointer, synonymous with <strong>r11</strong><br></li>
    <li><strong>ip</strong> and <strong>IP</strong> intra-procedure call scratch register, synonymous with <strong>r12</strong><br></li>
    <li><strong>sp</strong> and <strong>SP</strong> stack pointer, synonymous with <strong>r13</strong><br></li>
    <li><strong>lr</strong> and <strong>LR</strong> link register, synonymous with <strong>r14</strong><br></li>
    <li><strong>pc</strong> and <strong>PC</strong> program counter, synonymous with <strong>r15</strong><br></li>
    <li><strong>s0-s32</strong> VFP single precision registers<br></li>
    <li><strong>d0-d16 </strong>VFP double precision registers<br></li>
    <li><strong>fpsid </strong>VFP system ID register<br></li>
    <li><strong>fpscr</strong> VFP status and control register<br></li>
    <li><strong>fpexc</strong> VFP exception register<br></li>
    <li><strong>wr0-wr16</strong> WMMX SIMD data registers<br></li>
    <li><strong>wc0-wc16</strong> WMMX status and control registers<br></li>
    <li><strong>wcid</strong> WMMX coprocessor ID register, synonymous with <strong>wc0</strong><br></li>
    <li><strong>wcon </strong>WMMX control register, synonymous with <strong>wc1</strong><br></li>
    <li><strong>wcssf</strong> WMMX saturation SIMD flags, synonymous with <strong>wc2</strong><br></li>
    <li><strong>wcasf</strong> WMMX arithmetic SIMD flags, synonymous with <strong>wc3</strong><br></li>
    <li><strong>wcgr0-wcgr3</strong> WMMX control general purpose registers, synonymous with <strong>wc8-wc11</strong></li>
</ul><img src ="http://www.cnitblog.com/ken0917/aggbug/65382.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2010-04-17 23:10 <a href="http://www.cnitblog.com/ken0917/archive/2010/04/17/65382.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ARM指令的条件后缀</title><link>http://www.cnitblog.com/ken0917/archive/2010/04/09/65156.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Fri, 09 Apr 2010 03:09:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2010/04/09/65156.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/65156.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2010/04/09/65156.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/65156.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/65156.html</trackback:ping><description><![CDATA[<span style="font-weight: bold;">ARM指令的条件后缀</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  条件后缀的用途：在指令执行前先判断条件是否满足。满足执行本条指令，不满足跳过执行下一条指令<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  例如：<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  ADDEQ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  R4,R3,#0x01&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
在Z零标志位等于零的时候（Z=0），R4&lt;---R3+1<br>
&nbsp;&nbsp;&nbsp;&nbsp;  被测试的条件标志位有 Z，C，N和<img src="file:///C:/DOCUME%7E1/paul7898/LOCALS%7E1/Temp/moz-screenshot.jpg">V<br>
&nbsp;&nbsp;&nbsp;&nbsp;  N负数或小于<br>
&nbsp;&nbsp;&nbsp;&nbsp;  C进位或借位或扩张<br>
&nbsp;&nbsp;&nbsp;&nbsp;  Z零<br>
&nbsp;&nbsp;&nbsp;&nbsp;  V溢出<br>
&nbsp;&nbsp;&nbsp;&nbsp;  以上4位在CPSR或SPSR中。<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <span style="font-weight: bold;">ARM的条件码表</span><br>
<br>
<table style="width: 507px; height: 499px;" border="1" cellpadding="1" cellspacing="1">
    <tbody>
        <tr>
            <td> 编码</td>
            <td> 后缀助记符</td>
            <td> 标志位</td>
            <td> 定义</td>
        </tr>
        <tr>
            <td> 0000</td>
            <td> EQ</td>
            <td> Z=1</td>
            <td> 相等</td>
        </tr>
        <tr>
            <td> 0001</td>
            <td> NE</td>
            <td> Z=0</td>
            <td> 不相等</td>
        </tr>
        <tr>
            <td> 0010</td>
            <td> CS</td>
            <td> C=1</td>
            <td> 无符号大于或者等于</td>
        </tr>
        <tr>
            <td> 0011</td>
            <td> CC</td>
            <td> C=0</td>
            <td> 无符号小于</td>
        </tr>
        <tr>
            <td> 0100</td>
            <td> MI</td>
            <td> N=1</td>
            <td> 负值</td>
        </tr>
        <tr>
            <td> 0101</td>
            <td> PL</td>
            <td> N=0</td>
            <td> 正值或0</td>
        </tr>
        <tr>
            <td> 0110</td>
            <td> VS</td>
            <td> V=1</td>
            <td> 溢出</td>
        </tr>
        <tr>
            <td> 0111</td>
            <td> VC</td>
            <td> V=0</td>
            <td> 无溢出</td>
        </tr>
        <tr>
            <td> 1000</td>
            <td> HI</td>
            <td> C=1且Z=0</td>
            <td> 无符号大于</td>
        </tr>
        <tr>
            <td> 1001</td>
            <td> LS</td>
            <td> C=0或Z=1</td>
            <td> 无符号小于或等于</td>
        </tr>
        <tr>
            <td> 1010</td>
            <td> GE</td>
            <td> N和V相同</td>
            <td> 有符号大于或等于</td>
        </tr>
        <tr>
            <td> 1011</td>
            <td> LT</td>
            <td> N和V不同</td>
            <td> 有符号小于</td>
        </tr>
        <tr>
            <td> 1100</td>
            <td> GT</td>
            <td>Z=0且N等于V</td>
            <td> 有符号大于</td>
        </tr>
        <tr>
            <td> 1101</td>
            <td> LE</td>
            <td>Z=1或N不等于V</td>
            <td> 有符号小于或等于</td>
        </tr>
        <tr>
            <td> 1110</td>
            <td> AL</td>
            <td> 无条件</td>
            <td> 无条件</td>
        </tr>
    </tbody>
</table>
<br style="font-weight: bold;">
其中AL应该省略不写。默认AL无条件<br>
当条件后缀和S后缀同时使用时，S后缀在条件后缀之后。<br>
如  ADDEQS <img src ="http://www.cnitblog.com/ken0917/aggbug/65156.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2010-04-09 11:09 <a href="http://www.cnitblog.com/ken0917/archive/2010/04/09/65156.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux命令，备忘</title><link>http://www.cnitblog.com/ken0917/archive/2010/03/29/64888.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Mon, 29 Mar 2010 01:23:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2010/03/29/64888.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/64888.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2010/03/29/64888.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/64888.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/64888.html</trackback:ping><description><![CDATA[<p>tar.bz2&nbsp;命令: tar -jxvf&nbsp; *.tar.bz2</p>
<p>tar.z&nbsp;&nbsp; 命令: tar -zxvf&nbsp; *.tar.bz2</p>
<p>tar.gz&nbsp;&nbsp; 命令: tar -Zxvf&nbsp; *.tar.bz2</p>
<p>说明,关于这些参数你可以用man 帮助了.&nbsp; 你要看清楚大小写了,因为linxu区分大小写的.&nbsp; 第三个&nbsp; tar.gz&nbsp; </p>
<p>的参数是:-Zxvf&nbsp;&nbsp; Z是大写的．</p>
<p>打包：</p>
<p>&nbsp;tar -zcvf ./xx.tar.gz ./xx_dir<br><br></p>
<p><br></p>
<p>把交叉编译器的路径加入到PATH，以方便使用：sudovi/etc/bash_bashrc在
后面加入如下代码：if[-d/usr/local/arm];thenPATH=/usr/local/arm/2.95.3/bin:"${PATH}"fi代
码</p>
<p><br></p>
<p><br></p>
<p>今天装了源代码阅读工具，slickedit，但是候来给删除到了回收站里面，清空回收站的时候那个文件夹总是还<br>在，sudo rm -fr $HOME/.Trash/，这样也不行。这让我感到很是郁闷，每当想着有几百兆的空间浪费着，而<br>且看着回收站心里面怪难受的。于是上网查看相关文章，终于找到答案，原来回收站的路径变了。<br>变成了$HOME/.local/share/Trash/。<br>于是sudo rm -fr $HOME/.local/share/Trash/files/这样就可以了。<br><br>我想这可能是8.04跟7.10之前版本的一个改变吧。<br>
</p>
<p> <!--<a href="#" href_cetemp="#">http://msnpiki.msnfanatic.com/index.php/Main_Page</a>--></p>
<script src="http://www.diybl.com/js/2009/ad/ad_f4.js"></script>
<script type="text/javascript">
cpro_client='diybl_cpr';
cpro_at='text_image';
cpro_161=4;
cpro_flush=4;
cpro_w=336;
cpro_h=280;
cpro_template='text_default_336_280';
cpro_cbd='#F5FAFE';
cpro_cbg='#F5FAFE';
cpro_ctitle='#B50707';
cpro_cdesc='#333333';
cpro_curl='#333333';
cpro_cflush='#e10900';
cpro_uap=1;
cpro_cad=1;
</script>
<script language="JavaScript" type="text/javascript" src="http://cpro.baidu.com/cpro/ui/cp.js"></script>
<div style="display: none;">-</div><img src ="http://www.cnitblog.com/ken0917/aggbug/64888.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2010-03-29 09:23 <a href="http://www.cnitblog.com/ken0917/archive/2010/03/29/64888.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GCC的内嵌汇编语法 AT&amp;T汇编语言语法</title><link>http://www.cnitblog.com/ken0917/archive/2009/11/29/62898.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Sun, 29 Nov 2009 13:00:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2009/11/29/62898.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/62898.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2009/11/29/62898.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/62898.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/62898.html</trackback:ping><description><![CDATA[__asm__ __volatile__ GCC的内嵌汇编语法 AT&amp;T汇编语言语法（一）<br>2007年05月09日 12:36<br>开发一个OS，尽管绝大部分代码只需要用C/C++等高级语言就可以了，但至少和硬件相关部分的代码需要使用汇编语言，另外，由于启动部分的代码有大小限制，使用精练的汇编可以缩小目标代码的Size。另外，对于某些需要被经常调用的代码，使用汇编来写可以提高性能。所以我们必须了解汇编语言，即使你有可能并不喜欢它。<br><br><br><br>如果你是计算机专业的话，在大学里你应该学习过Intel格式的8086/80386汇编，这里就不再讨论。如果我们选择的OS开发工具是GCC以及GAS的话，就必须了解AT&amp;T汇编语言语法，因为GCC/GAS只支持这种汇编语法。<br><br><br><br>本书不会去讨论8086/80386的汇编编程，这类的书籍很多，你可以参考它们。这里只会讨论AT&amp;T的汇编语法，以及GCC的内嵌汇编语法。<br><br><br><br>--------------------------------------------------------------------------------<br><br><br><br>0.3.2 Syntax<br><br>1.寄存器引用<br><br>引用寄存器要在寄存器号前加百分号%,如&#8220;movl %eax, %ebx&#8221;。<br><br>80386有如下寄存器：<br><br>8个32-bit寄存器 %eax，%ebx，%ecx，%edx，%edi，%esi，%ebp，%esp；<br>8个16-bit寄存器，它们事实上是上面8个32-bit寄存器的低16位：%ax，%bx，%cx，%dx，%di，%si，%bp，%sp；<br>8个8-bit寄存器：%ah，%al，%bh，%bl，%ch，%cl，%dh，%dl。它们事实上是寄存器%ax，%bx，%cx，%dx的高8位和低8位；<br>6个段寄存器：%cs(code)，%ds(data)，%ss(stack), %es，%fs，%gs；<br>3个控制寄存器：%cr0，%cr2，%cr3；<br>6个debug寄存器：%db0，%db1，%db2，%db3，%db6，%db7；<br>2个测试寄存器：%tr6，%tr7；<br>8个浮点寄存器栈：%st(0)，%st(1)，%st(2)，%st(3)，%st(4)，%st(5)，%st(6)，%st(7)。<br>2. 操作数顺序<br><br>操作数排列是从源（左）到目的（右），如&#8220;movl %eax(源）, %ebx(目的）&#8221;<br><br>3. 立即数<br><br>使用立即数，要在数前面加符号$, 如&#8220;movl $0x04, %ebx&#8221;<br><br>或者：<br><br>para = 0x04<br><br>movl $para, %ebx<br><br>指令执行的结果是将立即数04h装入寄存器ebx。<br><br>4. 符号常数<br><br>符号常数直接引用 如<br><br>value: .long 0x12a3f2de<br><br>movl value , %ebx<br><br>指令执行的结果是将常数0x12a3f2de装入寄存器ebx。<br><br>引用符号地址在符号前加符号$, 如&#8220;movl $value, % ebx&#8221;则是将符号value的地址装入寄存器ebx。<br><br>5. 操作数的长度<br><br>操作数的长度用加在指令后的符号表示b(byte, 8-bit), w(word, 16-bits), l(long, 32-bits)，如&#8220;movb %al, %bl&#8221;，&#8220;movw %ax, %bx&#8221;，&#8220;movl %eax, %ebx &#8221;。<br><br>如 果没有指定操作数长度的话，编译器将按照目标操作数的长度来设置。比如指令&#8220;mov %ax, %bx&#8221;，由于目标操作数bx的长度为word，那么编译器将把此指令等同于&#8220;movw %ax, %bx&#8221;。同样道理，指令&#8220;mov $4, %ebx&#8221;等同于指令&#8220;movl $4, %ebx&#8221;，&#8220;push %al&#8221;等同于&#8220;pushb %al&#8221;。对于没有指定操作数长度，但编译器又无法猜测的指令，编译器将会报错，比如指令&#8220;push $4&#8221;。<br><br>6. 符号扩展和零扩展指令<br><br>绝大多数面向80386的AT&amp;T汇编指令与Intel格式的汇编指令都是相同的，符号扩展指令和零扩展指令则是仅有的不同格式指令。<br><br>符号扩展指令和零扩展指令需要指定源操作数长度和目的操作数长度，即使在某些指令中这些操作数是隐含的。<br><br>在AT&amp; T语法中，符号扩展和零扩展指令的格式为，基本部分"movs"和"movz"（对应Intel语法的movsx和movzx），后面跟上源操作数长度和目的操作数长度。movsbl意味着movs （from）byte （to）long；movbw意味着movs （from）byte （to）word；movswl意味着movs （from）word （to）long。对于movz指令也一样。比如指令&#8220;movsbl %al, %edx&#8221;意味着将al寄存器的内容进行符号扩展后放置到edx寄存器中。<br><br>其它的Intel格式的符号扩展指令还有：<br><br>cbw -- sign-extend byte in %al to word in %ax；<br>cwde -- sign-extend word in %ax to long in %eax；<br>cwd -- sign-extend word in %ax to long in %dx:%ax；<br>cdq -- sign-extend dword in %eax to quad in %edx:%eax；<br>对应的AT&amp;T语法的指令为cbtw，cwtl，cwtd，cltd。<br><br>7. 调用和跳转指令<br><br>段内调用和跳转指令为"call"，"ret"和"jmp"，段间调用和跳转指令为"lcall"，"lret"和"ljmp"。<br><br>段间调用和跳转指令的格式为&#8220;lcall/ljmp $SECTION, $OFFSET&#8221;，而段间返回指令则为&#8220;lret $STACK-ADJUST&#8221;。<br><br>8. 前缀<br><br>操作码前缀被用在下列的情况：<br><br>字符串重复操作指令(rep,repne)；<br>指定被操作的段(cs,ds,ss,es,fs,gs)；<br>进行总线加锁(lock)；<br>指定地址和操作的大小(data16,addr16)；<br>在AT&amp;T汇编语法中，操作码前缀通常被单独放在一行，后面不跟任何操作数。例如，对于重复scas指令，其写法为：<br><br>repne<br>scas<br><br>上述操作码前缀的意义和用法如下：<br><br>指定被操作的段前缀为cs,ds,ss,es,fs,和gs。在AT&amp;T语法中，只需要按照section:memory-operand的格式就指定了相应的段前缀。比如：lcall %cs:realmode_swtch<br>操作数／地址大小前缀是&#8220;data16&#8221;和"addr16"，它们被用来在32-bit操作数／地址代码中指定16-bit的操作数／地址。<br>总 线加锁前缀&#8220;lock&#8221;，它是为了在多处理器环境中，保证在当前指令执行期间禁止一切中断。这个前缀仅仅对ADD, ADC, AND, BTC, BTR, BTS, CMPXCHG,DEC, INC, NEG, NOT, OR, SBB, SUB, XOR, XADD,XCHG指令有效，如果将Lock前缀用在其它指令之前，将会引起异常。<br>字符串重复操作前缀"rep","repe","repne"用来让字符串操作重复&#8220;%ecx&#8221;次。<br><br>9. 内存引用<br><br>Intel语法的间接内存引用的格式为：<br><br>section:[base+index*scale+displacement]<br><br>而在AT&amp;T语法中对应的形式为：<br><br>section:displacement(base,index,scale)<br><br>其 中，base和index是任意的32-bit base和index寄存器。scale可以取值1，2，4，8。如果不指定scale值，则默认值为1。section可以指定任意的段寄存器作为段前缀，默认的段寄存器在不同的情况下不一样。如果你在指令中指定了默认的段前缀，则编译器在目标代码中不会产生此段前缀代码。<br><br>下面是一些例子：<br><br>-4(%ebp)：base=%ebp，displacement=-4，section没有指定，由于base＝%ebp，所以默认的section=%ss，index,scale没有指定，则index为0。<br><br>foo(,%eax,4)：index=%eax，scale=4，displacement=foo。其它域没有指定。这里默认的section=%ds。<br><br>foo(,1)：这个表达式引用的是指针foo指向的地址所存放的值。注意这个表达式中没有base和index，并且只有一个逗号，这是一种异常语法，但却合法。<br><br>%gs:foo：这个表达式引用的是放置于%gs段里变量foo的值。<br><br>如果call和jump操作在操作数前指定前缀&#8220;*&#8221;，则表示是一个绝对地址调用/跳转，也就是说jmp/call指令指定的是一个绝对地址。如果没有指定"*"，则操作数是一个相对地址。<br><br>任何指令如果其操作数是一个内存操作，则指令必须指定它的操作尺寸(byte,word,long），也就是说必须带有指令后缀(b,w,l)。<br><br>.3 GCC Inline ASM<br><br>GCC 支持在C/C++代码中嵌入汇编代码，这些汇编代码被称作GCC Inline ASM——GCC内联汇编。这是一个非常有用的功能，有利于我们将一些C/C++语法无法表达的指令直接潜入C/C++代码中，另外也允许我们直接写 C/C++代码中使用汇编编写简洁高效的代码。<br><br>1.基本内联汇编<br><br>GCC中基本的内联汇编非常易懂，我们先来看两个简单的例子：<br><br>__asm__("movl %esp,%eax"); // 看起来很熟悉吧！<br><br>或者是<br><br>__asm__("<br>movl $1,%eax // SYS_exit<br>xor %ebx,%ebx<br>int $0x80<br>");<br><br>或<br><br>__asm__(<br>"movl $1,%eax\r\t" \<br>"xor %ebx,%ebx\r\t" \<br>"int $0x80" \<br>);<br><br>基本内联汇编的格式是<br><br>__asm__ __volatile__("Instruction List");<br><br><br>1、__asm__<br><br>__asm__是GCC关键字asm的宏定义：<br><br>#define __asm__ asm<br><br>__asm__或asm用来声明一个内联汇编表达式，所以任何一个内联汇编表达式都是以它开头的，是必不可少的。<br><br>2、Instruction List<br><br>Instruction List是汇编指令序列。它可以是空的，比如：__asm__ __volatile__(""); 或__asm__ ("");都是完全合法的内联汇编表达式，只不过这两条语句没有什么意义。但并非所有Instruction List为空的内联汇编表达式都是没有意义的，比如：__asm__ ("":::"memory"); 就非常有意义，它向GCC声明：&#8220;我对内存作了改动&#8221;，GCC在编译的时候，会将此因素考虑进去。<br><br>我们看一看下面这个例子：<br><br>$ cat example1.c<br><br>int main(int __argc, char* __argv[])<br>{<br>int* __p = (int*)__argc;<br><br>(*__p) = 9999;<br><br>//__asm__("":::"memory");<br><br>if((*__p) == 9999)<br>return 5;<br><br>return (*__p);<br>}<br><br>在这段代码中，那条内联汇编是被注释掉的。在这条内联汇编之前，内存指针__p所指向的内存被赋值为9999，随即在内联汇编之后，一条if语句判断__p 所指向的内存与9999是否相等。很明显，它们是相等的。GCC在优化编译的时候能够很聪明的发现这一点。我们使用下面的命令行对其进行编译：<br><br>$ gcc -O -S example1.c<br><br>选项-O表示优化编译，我们还可以指定优化等级，比如-O2表示优化等级为2；选项-S表示将C/C++源文件编译为汇编文件，文件名和C/C++文件一样，只不过扩展名由.c变为.s。<br><br>我们来查看一下被放在example1.s中的编译结果，我们这里仅仅列出了使用gcc 2.96在redhat 7.3上编译后的相关函数部分汇编代码。为了保持清晰性，无关的其它代码未被列出。<br><br>$ cat example1.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>movl 8(%ebp), %eax # int* __p = (int*)__argc<br>movl $9999, (%eax) # (*__p) = 9999<br>movl $5, %eax # return 5<br>popl %ebp<br>ret<br><br>参 照一下C源码和编译出的汇编代码，我们会发现汇编代码中，没有if语句相关的代码，而是在赋值语句(*__p)=9999后直接return 5；这是因为GCC认为在(*__p)被赋值之后，在if语句之前没有任何改变(*__p)内容的操作，所以那条if语句的判断条件(*__p) == 9999肯定是为true的，所以GCC就不再生成相关代码，而是直接根据为true的条件生成return 5的汇编代码（GCC使用eax作为保存返回值的寄存器）。<br><br>我们现在将example1.c中内联汇编的注释去掉，重新编译，然后看一下相关的编译结果。<br><br>$ gcc -O -S example1.c<br><br>$ cat example1.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>movl 8(%ebp), %eax # int* __p = (int*)__argc<br>movl $9999, (%eax) # (*__p) = 9999<br>#APP<br><br># __asm__("":::"memory")<br>#NO_APP<br>cmpl $9999, (%eax) # (*__p) == 9999 ?<br>jne .L3 # false<br>movl $5, %eax # true, return 5<br>jmp .L2<br>.p2align 2<br>.L3:<br>movl (%eax), %eax<br>.L2:<br>popl %ebp<br>ret<br><br>由于内联汇编语句__asm__("":::"memory")向GCC声明，在此内联汇编语句出现的位置内存内容可能了改变，所以GCC在编译时就不能像刚才那样处理。这次，GCC老老实实的将if语句生成了汇编代码。<br><br>可能有人会质疑：为什么要使用__asm__("":::"memory")向GCC声明内存发生了变化？明明&#8220;Instruction List&#8221;是空的，没有任何对内存的操作，这样做只会增加GCC生成汇编代码的数量。<br><br>确实，那条内联汇编语句没有对内存作任何操作，事实上它确实什么都没有做。但影响内存内容的不仅仅是你当前正在运行的程序。比如，如果你现在正在操作的内存是一块内存映射，映射的内容是外围I/O设备寄存器。那么操作这块内存的就不仅仅是当前的程序，I/O设备也会去操作这块内存。既然两者都会去操作同一块内存，那么任何一方在任何时候都不能对这块内存的内容想当然。所以当你使用高级语言C/C++写这类程序的时候，你必须让编译器也能够明白这一点，毕竟高级语言最终要被编译为汇编代码。<br><br>你可能已经注意到了，这次输出的汇编结果中，有两个符号：#APP和#NO_APP，GCC将内联汇编语 句中"Instruction List"所列出的指令放在#APP和#NO_APP之间，由于__asm__("":::"memory")中&#8220;Instruction List&#8221;为空，所以#APP和#NO_APP中间也没有任何内容。但我们以后的例子会更加清楚的表现这一点。<br><br>关于为什么内联汇编__asm__("":::"memory")是一条声明内存改变的语句，我们后面会详细讨论。<br><br>刚才我们花了大量的内容来讨论"Instruction List"为空是的情况，但在实际的编程中，"Instruction List"绝大多数情况下都不是空的。它可以有1条或任意多条汇编指令。<br><br>当 在"Instruction List"中有多条指令的时候，你可以在一对引号中列出全部指令，也可以将一条或几条指令放在一对引号中，所有指令放在多对引号中。如果是前者，你可以将每一条指令放在一行，如果要将多条指令放在一行，则必须用分号（；）或换行符（\n，大多数情况下\n后还要跟一个\t，其中\n是为了换行，\t是为了空出一个tab宽度的空格）将它们分开。比如：<br><br>__asm__("movl %eax, %ebx<br>sti<br>popl %edi<br>subl %ecx, %ebx");<br><br>__asm__("movl %eax, %ebx; sti<br>popl %edi; subl %ecx, %ebx");<br><br>__asm__("movl %eax, %ebx; sti\n\t popl %edi<br>subl %ecx, %ebx");<br><br>都是合法的写法。如果你将指令放在多对引号中，则除了最后一对引号之外，前面的所有引号里的最后一条指令之后都要有一个分号(；)或(\n)或(\n\t)。比如：<br><br>__asm__("movl %eax, %ebx<br>sti\n"<br>"popl %edi;"<br>"subl %ecx, %ebx");<br><br>__asm__("movl %eax, %ebx; sti\n\t"<br>"popl %edi; subl %ecx, %ebx");<br><br>__asm__("movl %eax, %ebx; sti\n\t popl %edi\n"<br>"subl %ecx, %ebx");<br><br>__asm__("movl %eax, %ebx; sti\n\t popl %edi;" "subl %ecx, %ebx");<br><br>都是合法的。<br><br>上述原则可以归结为：<br><br>任意两个指令间要么被分号(；)分开，要么被放在两行；<br>放在两行的方法既可以从通过\n的方法来实现，也可以真正的放在两行；<br>可以使用1对或多对引号，每1对引号里可以放任一多条指令，所有的指令都要被放到引号中。<br>在基本内联汇编中，&#8220;Instruction List&#8221;的书写的格式和你直接在汇编文件中写非内联汇编没有什么不同，你可以在其中定义Label，定义对齐(.align n )，定义段(.section name )。例如：<br><br>__asm__(".align 2\n\t"<br>"movl %eax, %ebx\n\t"<br>"test %ebx, %ecx\n\t"<br>"jne error\n\t"<br>"sti\n\t"<br>"error: popl %edi\n\t"<br>"subl %ecx, %ebx");<br><br>上面例子的格式是Linux内联代码常用的格式，非常整齐。也建议大家都使用这种格式来写内联汇编代码。<br><br><br>3、__volatile__<br><br>__volatile__是GCC关键字volatile的宏定义：<br><br>#define __volatile__ volatile<br><br>__volatile__ 或volatile是可选的，你可以用它也可以不用它。如果你用了它，则是向GCC声明&#8220;不要动我所写的Instruction List，我需要原封不动的保留每一条指令&#8221;，否则当你使用了优化选项(-O)进行编译时，GCC将会根据自己的判断决定是否将这个内联汇编表达式中的指令优化掉。<br><br>那么GCC判断的原则是什么？我不知道（如果有哪位朋友清楚的话，请告诉我）。我试验了一下，发现一条内联汇编语句如果是基本内联汇编的话（即只有&#8220;Instruction List&#8221;，没有Input/Output/Clobber的内联汇编，我们后面将会讨论这一点），无论你是否使用__volatile__来修饰， GCC 2.96在优化编译时，都会原封不动的保留内联汇编中的&#8220;Instruction List&#8221;。但或许我的试验的例子并不充分，所以这一点并不能够得到保证。<br><br>为了保险起见，如果你不想让GCC的优化影响你的内联汇编代码，你最好在前面都加上__volatile__，而不要依赖于编译器的原则，因为即使你非常了解当前编译器的优化原则，你也无法保证这种原则将来不会发生变化。而__volatile__的含义却是恒定的。<br><br>2、带有C/C++表达式的内联汇编<br><br>GCC允许你通过C/C++表达式指定内联汇编中"Instrcuction List"中指令的输入和输出，你甚至可以不关心到底使用哪个寄存器被使用，完全靠GCC来安排和指定。这一点可以让程序员避免去考虑有限的寄存器的使用，也可以提高目标代码的效率。<br><br>我们先来看几个例子：<br><br>__asm__ (" " : : : "memory" ); // 前面提到的<br><br>__asm__ ("mov %%eax, %%ebx" : "=b"(rv) : "a"(foo) : "eax", "ebx");<br><br>__asm__ __volatile__("lidt %0": "=m" (idt_descr));<br><br>__asm__("subl %2,%0\n\t"<br>"sbbl %3,%1"<br>: "=a" (endlow), "=d" (endhigh)<br>: "g" (startlow), "g" (starthigh), "0" (endlow), "1" (endhigh));<br><br>怎么样，有点印象了吧，是不是也有点晕？没关系，下面讨论完之后你就不会再晕了。（当然，也有可能更晕^_^）。讨论开始——<br><br>带有C/C++表达式的内联汇编格式为：<br><br>__asm__　__volatile__("Instruction List" : Output : Input : Clobber/Modify);<br><br>从中我们可以看出它和基本内联汇编的不同之处在于：它多了3个部分(Input，Output，Clobber/Modify)。在括号中的4个部分通过冒号(:)分开。<br><br>这4个部分都不是必须的，任何一个部分都可以为空，其规则为：<br><br>如 果Clobber/Modify为空，则其前面的冒号(:)必须省略。比如__asm__("mov %%eax, %%ebx" : "=b"(foo) : "a"(inp) : )就是非法的写法；而__asm__("mov %%eax, %%ebx" : "=b"(foo) : "a"(inp) )则是正确的。<br>如果Instruction List为空，则Input，Output，Clobber/Modify可以不为空，也可以为空。比如__asm__ ( " " : : : "memory" );和__asm__(" " : : );都是合法的写法。<br>如果Output，Input，Clobber/Modify都为空，Output，Input之前的冒号(:)既可以省略，也可以不省略。如果都省略，则此汇编退化为一个基本内联汇编，否则，仍然是一个带有C/C++表达式的内联汇编，此时"Instruction List"中的寄存器写法要遵守相关规定，比如寄存器前必须使用两个百分号(%%)，而不是像基本汇编格式一样在寄存器前只使用一个百分号(%)。比如 __asm__( " mov %%eax, %%ebx" : : )；__asm__( " mov %%eax, %%ebx" : )和__asm__( " mov %eax, %ebx" )都是正确的写法，而__asm__( " mov %eax, %ebx" : : )；__asm__( " mov %eax, %ebx" : )和__asm__( " mov %%eax, %%ebx" )都是错误的写法。<br>如果Input，Clobber/Modify为空，但Output不为空，Input前的冒号(:)既可以省略，也可以不省略。比如 __asm__( " mov %%eax, %%ebx" : "=b"(foo) : )；__asm__( " mov %%eax, %%ebx" : "=b"(foo) )都是正确的。<br>如果后面的部分不为空，而前面的部分为空，则前面的冒号(:)都必须保留，否则无法说 明不为空的部分究竟是第几部分。比如， Clobber/Modify，Output为空，而Input不为空，则Clobber/Modify前的冒号必须省略（前面的规则），而Output 前的冒号必须为保留。如果Clobber/Modify不为空，而Input和Output都为空，则Input和Output前的冒号都必须保留。比如 __asm__( " mov %%eax, %%ebx" : : "a"(foo) )和__asm__( " mov %%eax, %%ebx" : : : "ebx" )。<br>从上面的规则可以看到另外一个事实，区分一个内联汇编是基本格式的还是带有C/C++表达式格式的，其规则在于在"Instruction List"后是否有冒号(:)的存在，如果没有则是基本格式的，否则，则是带有C/C++表达式格式的。<br><br>两种格式对寄存器语法的要求不同：基本格式要求寄存器前只能使用一个百分号(%)，这一点和非内联汇编相同；而带有C/C++表达式格式则要求寄存器前必须使用两个百分号(%%)，其原因我们会在后面讨论。<br><br>1. Output<br><br>Output用来指定当前内联汇编语句的输出。我们看一看这个例子：<br><br>__asm__("movl %%cr0, %0": "=a" (cr0));<br><br>这个内联汇编语句的输出部分为"=r"(cr0)，它是一个&#8220;操作表达式&#8221;，指定了一个输出操作。我们可以很清楚得看到这个输出操作由两部分组成：括号括住的部分(cr0)和引号引住的部分"=a"。这两部分都是每一个输出操作必不可少的。括号括住的部分是一个C/C++表达式，用来保存内联汇编的一个输出值，其操作就等于C/C++的相等赋值cr0 = output_value，因此，括号中的输出表达式只能是C/C++的左值表达式，也就是说它只能是一个可以合法的放在C/C++赋值操作中等号(=) 左边的表达式。那么右值output_value从何而来呢？<br><br>答案是引号中的内容，被称作&#8220;操作约束&#8221;（Operation Constraint），在这个例子中操作约束为"=a"，它包含两个约束：等号(=)和字母a，其中等号(=)说明括号中左值表达式cr0是一个 Write-Only的，只能够被作为当前内联汇编的输入，而不能作为输入。而字母a是寄存器EAX / AX / AL的简写，说明cr0的值要从eax寄存器中获取，也就是说cr0 = eax，最终这一点被转化成汇编指令就是movl %eax, address_of_cr0。现在你应该清楚了吧，操作约束中会给出：到底从哪个寄存器传递值给cr0。<br><br>另外，需要特别说明的是，很多文档都声明，所有输出操作的操作约束必须包含一个等号(=)，但GCC的文档中却很清楚的声明，并非如此。因为等号(=)约束说明当前的表达式是一个 Write-Only的，但另外还有一个符号——加号(+)用来说明当前表达式是一个Read-Write的，如果一个操作约束中没有给出这两个符号中的任何一个，则说明当前表达式是Read-Only的。因为对于输出操作来说，肯定是必须是可写的，而等号(=)和加号(+)都表示可写，只不过加号(+) 同时也表示是可读的。所以对于一个输出操作来说，其操作约束只需要有等号(=)或加号(+)中的任意一个就可以了。<br><br>二者的区别是：等号(=)表示当前操作表达式指定了一个纯粹的输出操作，而加号(+)则表示当前操作表达式不仅仅只是一个输出操作还是一个输入操作。但无论是等号(=)约束还是加号(+)约束所约束的操作表达式都只能放在Output域中，而不能被用在Input域中。<br><br>另外，有些文档声明：尽管GCC文档中提供了加号(+)约束，但在实际的编译中通不过；我不知道老版本会怎么样，我在GCC 2.96中对加号(+)约束的使用非常正常。<br><br>我们通过一个例子看一下，在一个输出操作中使用等号(=)约束和加号(+)约束的不同。<br><br>$ cat example2.c<br><br>int main(int __argc, char* __argv[])<br>{<br>int cr0 = 5;<br><br>__asm__ __volatile__("movl %%cr0, %0":"=a" (cr0));<br><br>return 0;<br>}<br><br>$ gcc -S example2.c<br><br>$ cat example2.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>subl $4, %esp<br>movl $5, -4(%ebp) # cr0 = 5<br>#APP<br>movl %cr0, %eax<br>#NO_APP<br>movl %eax, %eax<br>movl %eax, -4(%ebp) # cr0 = %eax<br>movl $0, %eax<br>leave<br>ret<br><br><br>这个例子是使用等号(=)约束的情况，变量cr0被放在内存-4(%ebp)的位置，所以指令mov %eax, -4(%ebp)即表示将%eax的内容输出到变量cr0中。<br><br>下面是使用加号(+)约束的情况：<br><br>$ cat example3.c<br><br>int main(int __argc, char* __argv[])<br>{<br>int cr0 = 5;<br><br>__asm__ __volatile__("movl %%cr0, %0" : "+a" (cr0));<br><br>return 0;<br>}<br><br>$ gcc -S example3.c<br><br>$ cat example3.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>subl $4, %esp<br>movl $5, -4(%ebp) # cr0 = 5<br>movl -4(%ebp), %eax # input ( %eax = cr0 )<br>#APP<br>movl %cr0, %eax<br>#NO_APP<br>movl %eax, -4(%ebp) # output (cr0 = %eax )<br>movl $0, %eax<br>leave<br>ret<br><br><br>从编译的结果可以看出，当使用加号(+)约束的时候，cr0不仅作为输出，还作为输入，所使用寄存器都是寄存器约束(字母a，表示使用eax寄存器)指定的。关于寄存器约束我们后面讨论。<br><br>在Output域中可以有多个输出操作表达式，多个操作表达式中间必须用逗号(,)分开。例如：<br><br>__asm__(<br>"movl %%eax, %0 \n\t"<br>"pushl %%ebx \n\t"<br>"popl %1 \n\t"<br>"movl %1, %2"<br>: "+a"(cr0), "=b"(cr1), "=c"(cr2));<br><br><br><br>2、Input<br><br>Input域的内容用来指定当前内联汇编语句的输入。我们看一看这个例子：<br><br>__asm__("movl %0, %%db7" : : "a" (cpu-&gt;db7));<br><br>例中Input域的内容为一个表达式"a"[cpu-&gt;db7)，被称作&#8220;输入表达式&#8221;，用来表示一个对当前内联汇编的输入。<br><br>像输出表达式一样，一个输入表达式也分为两部分：带括号的部分(cpu-&gt;db7)和带引号的部分"a"。这两部分对于一个内联汇编输入表达式来说也是必不可少的。<br><br>括号中的表达式cpu-&gt;db7是一个C/C++语言的表达式，它不必是一个左值表达式，也就是说它不仅可以是放在C/C++赋值操作左边的表达式，还可以是放在C/C++赋值操作右边的表达式。所以它可以是一个变量，一个数字，还可以是一个复杂的表达式（比如a+b/c*d）。比如上例可以改为： __asm__("movl %0, %%db7" : : "a" (foo))，__asm__("movl %0, %%db7" : : "a" (0x1000))或__asm__("movl %0, %%db7" : : "a" (va*vb/vc))。<br><br>引号号中的部分是约束部分，和输出表达式约束不同的是，它不允许指定加号(+)约束和等号(=)约束，也就是说它只能是默认的Read-Only的。约束中必须指定一个寄存器约束，例中的字母a表示当前输入变量cpu-&gt;db7要通过寄存器eax输入到当前内联汇编中。<br><br>我们看一个例子：<br><br>$ cat example4.c<br><br>int main(int __argc, char* __argv[])<br>{<br>int cr0 = 5;<br><br>__asm__ __volatile__("movl %0, %%cr0"::"a" (cr0));<br><br>return 0;<br>}<br><br>$ gcc -S example4.c<br><br>$ cat example4.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>subl $4, %esp<br>movl $5, -4(%ebp) # cr0 = 5<br>movl -4(%ebp), %eax # %eax = cr0<br>#APP<br>movl %eax, %cr0<br>#NO_APP<br>movl $0, %eax<br>leave<br>ret<br><br><br>我们从编译出的汇编代码可以看到，在"Instruction List"之前，GCC按照我们的输入约束"a"，将变量cr0的内容装入了eax寄存器。<br><br>3. Operation Constraint<br><br>每一个Input和Output表达式都必须指定自己的操作约束Operation Constraint，我们这里来讨论在80386平台上所可能使用的操作约束。<br><br>1、寄存器约束<br><br>当你当前的输入或输入需要借助一个寄存器时，你需要为其指定一个寄存器约束。你可以直接指定一个寄存器的名字，比如：<br><br>__asm__ __volatile__("movl %0, %%cr0"::"eax" (cr0));<br><br>也可以指定一个缩写，比如：<br><br>__asm__ __volatile__("movl %0, %%cr0"::"a" (cr0));<br><br>如果你指定一个缩写，比如字母a，则GCC将会根据当前操作表达式中C/C++表达式的宽度决定使用%eax，还是%ax或%al。比如：<br><br>unsigned short __shrt;<br><br>__asm__ ("mov %0，%%bx" : : "a"(__shrt));<br><br>由于变量__shrt是16-bit short类型，则编译出来的汇编代码中，则会让此变量使用%ex寄存器。编译结果为：<br><br>movw -2(%ebp), %ax # %ax = __shrt<br>#APP<br>movl %ax, %bx<br>#NO_APP<br><br>无论是Input，还是Output操作表达式约束，都可以使用寄存器约束。<br><br>下表中列出了常用的寄存器约束的缩写。<br><br>约束 Input/Output 意义<br>r I,O 表示使用一个通用寄存器，由GCC在%eax/%ax/%al, %ebx/%bx/%bl, %ecx/%cx/%cl, %edx/%dx/%dl中选取一个GCC认为合适的。<br>q I,O 表示使用一个通用寄存器，和r的意义相同。<br>a I,O 表示使用%eax / %ax / %al<br>b I,O 表示使用%ebx / %bx / %bl<br>c I,O 表示使用%ecx / %cx / %cl<br>d I,O 表示使用%edx / %dx / %dl<br>D I,O 表示使用%edi / %di<br>S I,O 表示使用%esi / %si<br>f I,O 表示使用浮点寄存器<br>t I,O 表示使用第一个浮点寄存器<br>u I,O 表示使用第二个浮点寄存器<br><br><br>2、内存约束<br>如果一个Input/Output操作表达式的C/C++表达式表现为一个内存地址，不想借助于任何寄存器，则可以使用内存约束。比如：<br><br>__asm__ ("lidt %0" : "=m"(__idt_addr)); 或 __asm__ ("lidt %0" : :"m"(__idt_addr));<br><br>我们看一下它们分别被放在一个C源文件中，然后被GCC编译后的结果：<br><br>$ cat example5.c<br><br>// 本例中，变量sh被作为一个内存输入<br><br>int main(int __argc, char* __argv[])<br>{<br>char* sh = (char*)&amp;__argc;<br><br>__asm__ __volatile__("lidt %0" : : "m" (sh));<br><br>return 0;<br>}<br><br>$ gcc -S example5.c<br><br>$ cat example5.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>subl $4, %esp<br>leal 8(%ebp), %eax<br>movl %eax, -4(%ebp) # sh = (char*) &amp;__argc<br>#APP<br>lidt -4(%ebp)<br>#NO_APP<br>movl $0, %eax<br>leave<br>ret<br><br><br>$ cat example6.c<br><br>// 本例中，变量sh被作为一个内存输出<br><br>int main(int __argc, char* __argv[])<br>{<br>char* sh = (char*)&amp;__argc;<br><br>__asm__ __volatile__("lidt %0" : "=m" (sh));<br><br>return 0;<br>}<br><br>$ gcc -S example6.c<br><br>$ cat example6.s<br><br>main:<br>pushl %ebp<br>movl %esp, %ebp<br>subl $4, %esp<br>leal 8(%ebp), %eax<br>movl %eax, -4(%ebp) # sh = (char*) &amp;__argc<br>#APP<br>lidt -4(%ebp)<br>#NO_APP<br>movl $0, %eax<br>leave<br>ret<br>首先，你会注意到，在这两个例子中，变量sh没有借助任何寄存器，而是直接参与了指令lidt的操作。<br><br>其次，通过仔细观察，你会发现一个惊人的事实，两个例子编译出来的汇编代码是一样的！虽然，一个例子中变量sh作为输入，而另一个例子中变量sh作为输出。这是怎么回事？<br><br>原来，使用内存方式进行输入输出时，由于不借助寄存器，所以GCC不会按照你的声明对其作任何的输入输出处理。GCC只会直接拿来用，究竟对这个C/C++表达式而言是输入还是输出，完全依赖与你写在"Instruction List"中的指令对其操作的指令。<br><br>由 于上例中，对其操作的指令为lidt，lidt指令的操作数是一个输入型的操作数，所以事实上对变量sh的操作是一个输入操作，即使你把它放在 Output域也不会改变这一点。所以，对此例而言，完全符合语意的写法应该是将sh放在Input域，尽管放在Output域也会有正确的执行结果。<br><br>所 以，对于内存约束类型的操作表达式而言，放在Input域还是放在Output域，对编译结果是没有任何影响的，因为本来我们将一个操作表达式放在 Input域或放在Output域是希望GCC能为我们自动通过寄存器将表达式的值输入或输出。既然对于内存约束类型的操作表达式来说，GCC不会自动为它做任何事情，那么放在哪儿也就无所谓了。但从程序员的角度而言，为了增强代码的可读性，最好能够把它放在符合实际情况的地方。<br><br>约束 Input/Output 意义<br>m I,O 表示使用系统所支持的任何一种内存方式，不需要借助寄存器<br><br><img src ="http://www.cnitblog.com/ken0917/aggbug/62898.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2009-11-29 21:00 <a href="http://www.cnitblog.com/ken0917/archive/2009/11/29/62898.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ARM寄存器</title><link>http://www.cnitblog.com/ken0917/archive/2009/11/05/62428.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Thu, 05 Nov 2009 12:21:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2009/11/05/62428.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/62428.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2009/11/05/62428.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/62428.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/62428.html</trackback:ping><description><![CDATA[<p>ARM处理器包含多少寄存器?每种模式下又有那些寄存器?这些寄存器的作用又是什么?带着这些问题我们来学习ARM寄存器吧!相信看完这篇文章后你会有所收获。<br>ARM处理器共有37个寄存器。<br>它包含31个通用寄存器和6个状态寄存器。</p>
<p>=======================================================================================<br>Usr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Supervisor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Abort&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Undefined&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IRQ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FIQ<br>----------------------------------------------------------------------------------------------------------------<br>R0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; R0&nbsp;&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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PC<br>CPSR&nbsp;&nbsp;&nbsp; CPSR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CPSR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CPSR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CPSR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CPSR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CPSR<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SPSR_svc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPSR_abt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPSR_und&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPSR_irq&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPSR_fiq<br>=======================================================================================<br>1.通用寄存器的分类：<br>a.未备份寄存器,包括R0-R7<br>&nbsp;&nbsp;&nbsp;
对每个未备份寄存器来说,在所有的模式下都是指同一个物理寄存器(例如:Usr下的R0与FIQ下的R0是同一个寄存器)。在异常程序中断造成模式切换
时，由于不同模式使用的是相同的物理寄存器。这可能导致数据遭到破坏。未备份寄存器没有被系统作为别的用途,任何场合均可采用未备份寄存器。<br>b.备份寄存器,包括R8-R14<br>&nbsp;&nbsp;&nbsp; 对于备份寄存器R8-R12来说,除FIQ模式下其它模式均使用相同的物理寄存器。在FIQ模式下R8_fiq,R9_fiq,<br>R10_fiq,R11_fiq,R12_fiq。它有自己的物理寄存器。<br>&nbsp;&nbsp;&nbsp; 对于R13和R14寄存器每种模式都有自己的物理寄存器(System与Usr的寄存器相同)当异常中断发生时,系统使用相应模式下的物理寄存器,从而可以避免数据遭到破坏。<br>&nbsp;&nbsp; R13也称为SP堆栈指针。<br>&nbsp;&nbsp; R14也称为LR寄存器<br>c.程序计数器,PC<br>&nbsp;&nbsp; PC寄存器存储指令地址,由于ARM采用流水机制执行指令,故PC寄存器总是存储下一条指令的地址。<br>&nbsp;&nbsp; 由于ARM是按照字对齐故PC被读取后的值的bit[1:0]总是0b00(thumb的bit[0]是0b0)。</p>
<p>2.程序状态寄存器<br>程序状态寄存器包含当前程序状态寄存器和备份状态寄存器。<br>a.CPSR(程序状态寄存器)<br>CPSR在任何处理器模式下都可以被访问。其结构如下：</p>
<p>&nbsp; 31 30 29 28&nbsp; ---&nbsp;&nbsp; 7&nbsp;&nbsp; 6&nbsp;&nbsp; -&nbsp;&nbsp; 4&nbsp;&nbsp; 3&nbsp;&nbsp; 2&nbsp;&nbsp; 1&nbsp;&nbsp; 0<br>&nbsp; N&nbsp; Z&nbsp; C&nbsp; V&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I&nbsp;&nbsp; F&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; M4&nbsp; M3&nbsp; M2&nbsp; M1&nbsp; M0</p>
<p>N(Negative)、Z(Zero)、C(Carry)以及V(oVerflow)称为条件标志位。ARM指令根据CPSR的条件标志位来选择地执行。</p>
<p>CPSR条件标志位<br>=======================================================================================<br>条件标志位&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 含义<br>----------------------------------------------------------------------------------------------------------------<br>N&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N=1 表示运算结果为负数,N=0 表示运算结果为正数。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>Z&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Z=1 表示运算结果为0， Z=0 表示运算结果为非零。<br>C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C=1 表示运算结果产生了进位。<br>V&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; V=1 运算结果的符号位发生了溢出。<br>Q&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在ARMv5 E系列版本中Q=1 表示DSP指令溢出。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在ARMv5以前的版本中没有Q标志位。<br>=======================================================================================<br>以下指令会影响CPSR的条件标志位<br>(1)比较指令,如: CMP、CMN、TEQ、TST等。<br>(2)当一些算术逻辑运算的目标寄存器不是PC时，这些指令会影响CPSR的条件标志位。<br>(3)MSR与MRS指令可以对CPSR/SPSR进行操作。<br>(4)LDM指令可以将SPSR复制到CPSR中。</p>
<p>CPSR的控制位<br>=======================================================================================<br>控制位&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 含义<br>----------------------------------------------------------------------------------------------------------------<br>I&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I=1 禁用IRO中断<br>F&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; F=1 禁用FIQ中断<br>T&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ARMv4以上T版本T=0 执行ARM指令,T=1执行Thumb指令。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ARMv5以上非T版本T=0 执行ARM指令,T=1表示下一条指令产生未定义指令中断。M[4:0]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 控制处理器模式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b10000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; User<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b10001&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FIQ<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b10010&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IRQ<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b10011&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Supervisor<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b10111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Abort<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b11011&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Undefined<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0b11111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>=======================================================================================<br>b.SPSR(备份状态寄存器)<br>SPSR的结构与CPSR的结构相同，SPSR是用来备份CPSR的。<br>现在你对ARM处理器的寄存器有一些了解了吧? <br></p><img src ="http://www.cnitblog.com/ken0917/aggbug/62428.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2009-11-05 20:21 <a href="http://www.cnitblog.com/ken0917/archive/2009/11/05/62428.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>self modifying code 自修改程序</title><link>http://www.cnitblog.com/ken0917/archive/2009/10/22/62052.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Thu, 22 Oct 2009 02:35:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2009/10/22/62052.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/62052.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2009/10/22/62052.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/62052.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/62052.html</trackback:ping><description><![CDATA[程序在运行过程序中，修改自己的代码段内存，改变了原先的代码指令。<br>这种编程思路在某些方面相当实用，能很好的提高程序的效率。<br>比如一段代码在第一次运行后就不再使用了，但每次都要对其进行判断，像某些模拟器程序中，这种判断就会占去很多的效率，如果使用这种自修改思路，即把某些只要执行一次的代码在执行过后除去，那将很好的提高了效率。<br><br>下面是一个在网上DOWNLOAD的一个示例程序，我在 Linux 2.6.28 , gcc 4.3.3 环境测试成功。<br><br>$ cat -n smc.c <br>&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp; &nbsp;/*<br>&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp; &nbsp; * Self Modifying Code&nbsp; smc.c<br>&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp; &nbsp; */<br>&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp; &nbsp;#include &lt;stdio.h&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp; &nbsp;#include &lt;stdlib.h&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp; &nbsp;#include &lt;unistd.h&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp; 8&nbsp;&nbsp; &nbsp;#include &lt;sys/mman.h&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp; 9&nbsp;&nbsp; &nbsp;#include &lt;errno.h&gt;<br>&nbsp;&nbsp;&nbsp; 10&nbsp;&nbsp; &nbsp;#include &lt;string.h&gt;<br>&nbsp;&nbsp;&nbsp; 11&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 12&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;gcc is oblivious to asm level so we have to spell out<br>&nbsp;&nbsp;&nbsp; 13&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;our intentions.. it's an ugly hack. if you know of<br>&nbsp;&nbsp;&nbsp; 14&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;a proper way to do it in pure C please tell me. */<br>&nbsp;&nbsp;&nbsp; 15&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 16&nbsp;&nbsp; &nbsp;extern char injectHere;<br>&nbsp;&nbsp;&nbsp; 17&nbsp;&nbsp; &nbsp;extern char injectionStart;<br>&nbsp;&nbsp;&nbsp; 18&nbsp;&nbsp; &nbsp;extern char injectionEnd;<br>&nbsp;&nbsp;&nbsp; 19&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 20&nbsp;&nbsp; &nbsp;void dummyTextContext( void )<br>&nbsp;&nbsp;&nbsp; 21&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp;&nbsp; 22&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;sometimes gcc is just too smart.. in an ideal world<br>&nbsp;&nbsp;&nbsp; 23&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;we would write smthng like (*(&amp;exit))( 42 ); and get<br>&nbsp;&nbsp;&nbsp; 24&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;an indirect call (via absolute address). anyway,<br>&nbsp;&nbsp;&nbsp; 25&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;we don't get that so we have to make one pointer for<br>&nbsp;&nbsp;&nbsp; 26&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;every type of function we want to call */<br>&nbsp;&nbsp;&nbsp; 27&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 28&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int (*callPrintf)( const char *format, ... ); <br>&nbsp;&nbsp;&nbsp; 29&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;void (*callExit)( int ); /* man 3 printf exit */<br>&nbsp;&nbsp;&nbsp; 30&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 31&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;asm volatile( "injectionStart:" );<br>&nbsp;&nbsp;&nbsp; 32&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 33&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;do what you like here but keep it relocatable<br>&nbsp;&nbsp;&nbsp; 34&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fire up objdump, gdb or whatever to check what you get */<br>&nbsp;&nbsp;&nbsp; 35&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 36&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;(*(callPrintf=&amp;printf))( "Hello :-)\n" );<br>&nbsp;&nbsp;&nbsp; 37&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;(*(callPrintf=&amp;printf))( "No endless loop here!\n" );<br>&nbsp;&nbsp;&nbsp; 38&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 39&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;(*(callExit=&amp;exit))( 42 ); /* the meaning of life (for this proggie anyway..) */<br>&nbsp;&nbsp;&nbsp; 40&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 41&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;asm volatile( "injectionEnd:" );<br>&nbsp;&nbsp;&nbsp; 42&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp;&nbsp; 43&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 44&nbsp;&nbsp; &nbsp;int main( void )<br>&nbsp;&nbsp;&nbsp; 45&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp;&nbsp; 46&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;it's fairly safe to assume page is 2^N,<br>&nbsp;&nbsp;&nbsp; 47&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;now.. let's find us a page to bash &gt;:*/<br>&nbsp;&nbsp;&nbsp; 48&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 49&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;unsigned page = (unsigned)&amp;injectHere &amp; ~( getpagesize() - 1 );<br>&nbsp;&nbsp;&nbsp; 50&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 51&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;chmod u=rwx page ;-) */<br>&nbsp;&nbsp;&nbsp; 52&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 53&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if( mprotect( (char*)page, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC ) )<br>&nbsp;&nbsp;&nbsp; 54&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp;&nbsp; 55&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;perror( "mprotect failed" );<br>&nbsp;&nbsp;&nbsp; 56&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;exit( errno );<br>&nbsp;&nbsp;&nbsp; 57&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp;&nbsp; 58&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 59&nbsp;&nbsp; &nbsp;endlessLoop:<br>&nbsp;&nbsp;&nbsp; 60&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 61&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;asm volatile( "injectHere:" );&nbsp;&nbsp; &nbsp;/* ground zero */<br>&nbsp;&nbsp;&nbsp; 62&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 63&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;a few stats to ease debugging (and some code to fill our loop too) */<br>&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 65&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf( "PAGE(0x%x): cp &lt;0x%x&gt;[0..%u] &lt;0x%x&gt;\n",<br>&nbsp;&nbsp;&nbsp; 66&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;page, (int)&amp;injectionStart, &amp;injectionEnd - &amp;injectionStart, (int)&amp;injectHere );<br>&nbsp;&nbsp;&nbsp; 67&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 68&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp;inject some */<br>&nbsp;&nbsp;&nbsp; 69&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 70&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;memcpy( &amp;injectHere, &amp;injectionStart, &amp;injectionEnd - &amp;injectionStart );<br>&nbsp;&nbsp;&nbsp; 71&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 72&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;goto endlessLoop;<br>&nbsp;&nbsp;&nbsp; 73&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; 74&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 0;<br>&nbsp;&nbsp;&nbsp; 75&nbsp;&nbsp; &nbsp;}<img src ="http://www.cnitblog.com/ken0917/aggbug/62052.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2009-10-22 10:35 <a href="http://www.cnitblog.com/ken0917/archive/2009/10/22/62052.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mips linux calling convention</title><link>http://www.cnitblog.com/ken0917/archive/2009/10/21/62043.html</link><dc:creator>王小明</dc:creator><author>王小明</author><pubDate>Wed, 21 Oct 2009 08:29:00 GMT</pubDate><guid>http://www.cnitblog.com/ken0917/archive/2009/10/21/62043.html</guid><wfw:comment>http://www.cnitblog.com/ken0917/comments/62043.html</wfw:comment><comments>http://www.cnitblog.com/ken0917/archive/2009/10/21/62043.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ken0917/comments/commentRss/62043.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ken0917/services/trackbacks/62043.html</trackback:ping><description><![CDATA[有个老外问的：<br>
<div class="post-text">
<p>There appears to be no
definitive standardized stack frame and C language calling conventions
(register usage and such) for the MIPS-32 Processor Architecture. That
is, it appears to be completely up to the assembler/compiler tool chain
to define their own stack frame and calling conventions. I've struggled
to find a definitive reference of what conventions the GCC compiler
uses for MIPS-32 instruction set. I'm specially using GCC
cross-compiler on Cygwin that targets a MIPS-32 core being used in an
embedded environment on the eCos open source kernel.</p>
<p>Any references to definitive documentation about GCC for MIPS-32 in this area would be appreciated.</p>
<p><br></p>
<p>另外一个回复的：</p>
<div class="post-text">
<p>You are right, this is a bit of a mess and is poorly documented. I would recommend:</p>
<ul>
    <li><a  href="http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf" rel="nofollow">http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf</a></li>
    <li>"See MIPS Run" by Dominic Sweetman</li>
    <li>Examining disassembled code generated by the compiler to check for discrepancies with the above references.</li>
</ul>
</div>
</div>
<br><br><img src ="http://www.cnitblog.com/ken0917/aggbug/62043.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ken0917/" target="_blank">王小明</a> 2009-10-21 16:29 <a href="http://www.cnitblog.com/ken0917/archive/2009/10/21/62043.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>