⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn技术中心 在visual c++中使用内联汇编.htm

📁 vc和汇编的混合编程
💻 HTM
📖 第 1 页 / 共 4 页
字号:
              <TR>
                <TD style="COLOR: #0033ff; BORDER-BOTTOM: #b0c4de 1px dashed" 
                align=right><FONT style="FONT-SIZE: 8pt">数据库</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/articlelist.aspx?c=8">数据库开发</A>&nbsp;&nbsp;(<A 
                  href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&amp;bigclassid=8">rss</A>)&nbsp;&nbsp;&nbsp;&nbsp;</TD></TR></TBODY></TABLE>
            <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center 
border=0>
              <TBODY>
              <TR>
                <TD style="COLOR: #0033ff; BORDER-BOTTOM: #b0c4de 1px dashed" 
                align=right><FONT style="FONT-SIZE: 8pt">软件工程</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/articlelist.aspx?c=13">其他</A>&nbsp;&nbsp;(<A 
                  href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&amp;bigclassid=13">rss</A>)&nbsp;&nbsp;&nbsp;&nbsp;</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
      <TABLE cellSpacing=1 cellPadding=0 width="100%" bgColor=#000000 
        border=0><TBODY>
        <TR>
          <TD vAlign=top bgColor=#ededed>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD align=right 
                background="CSDN技术中心 在Visual C++中使用内联汇编.files/top.gif" 
                  height=27><FONT 
              class=fonttitle>积极原创作者&nbsp;</FONT></TD></TR></TBODY></TABLE>
            <TABLE cellSpacing=1 cellPadding=2 width="100%" border=0>
              <TBODY>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/lynnboy">lynnboy</A>&nbsp;<FONT 
                  color=#ff0000>(6)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/alcoholwang">alcoholwang</A>&nbsp;<FONT 
                  color=#ff0000>(2)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/babyrockxray">babyrockxray</A>&nbsp;<FONT 
                  color=#ff0000>(2)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/blueis">blueis</A>&nbsp;<FONT 
                  color=#ff0000>(1)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/xiaopeng_e">xiaopeng_e</A>&nbsp;<FONT 
                  color=#ff0000>(1)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/usersmj">usersmj</A>&nbsp;<FONT 
                  color=#ff0000>(4)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/bpfish">bpfish</A>&nbsp;<FONT 
                  color=#ff0000>(10)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/hongweijin">hongweijin</A>&nbsp;<FONT 
                  color=#ff0000>(6)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/ljcao">ljcao</A>&nbsp;<FONT 
                  color=#ff0000>(10)</FONT></TD></TR>
              <TR>
                <TD align=right><A 
                  href="http://dev.csdn.net/user/scud">scud</A>&nbsp;<FONT 
                  color=#ff0000>(6)</FONT></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD>
    <TD vAlign=top width=786>
      <TABLE cellPadding=4 width="100%" border=0>
        <TBODY>
        <TR>
          <TD width=10 height=19></TD>
          <TD height=19>
            <TABLE cellSpacing=3 cellPadding=3 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><A href="http://www.csdn.net/">CSDN</A> - <A 
                  href="http://dev.csdn.net/">文档中心</A> - <FONT color=#003399><A 
                  id=ArticleTitle1_ArticleTitle1_hlClass 
                  href="http://dev.csdn.net/articlelist.aspx?c=1">Visual 
                  C++</A></FONT> </TD>
                <TD align=right>阅读:<SPAN 
                  id=ArticleTitle1_ArticleTitle1_lblReadCount>4211</SPAN> 
                  &nbsp;&nbsp;评论: <SPAN 
                  id=ArticleTitle1_ArticleTitle1_lblCommentCount>13</SPAN> 
                  &nbsp;&nbsp; <A 
                  href="http://dev.csdn.net/article/15/15231.shtm#Comment">参与评论</A> 
                </TD></TR></TBODY></TABLE>
            <TABLE cellSpacing=3 cellPadding=3 width="100%" bgColor=#eeeeee 
            border=0>
              <TBODY>
              <TR>
                <TD noWrap width=60 height=0></TD>
                <TD></TD></TR>
              <TR>
                <TD noWrap align=middle bgColor=#003399 height=16><FONT 
                  color=#ffffff>标题</FONT></TD>
                <TD><B>&nbsp; <SPAN 
                  id=ArticleTitle1_ArticleTitle1_lblTitle>在Visual 
                  C++中使用内联汇编</SPAN></B>&nbsp;&nbsp;&nbsp;&nbsp; 选择自 <A 
                  id=ArticleTitle1_ArticleTitle1_AuthorLink 
                  href="http://dev.csdn.net/user/doskey">doskey</A> 的 Blog 
</TD></TR>
              <TR>
                <TD align=middle bgColor=#003399 height=16><FONT 
                  color=#ffffff>关键字</FONT></TD>
                <TD width=500>&nbsp; <SPAN 
                  id=ArticleTitle1_ArticleTitle1_lblKeywords>VC,内联,汇编,asm,inline</SPAN></TD></TR>
              <TR>
                <TD align=middle bgColor=#003399 height=16><FONT 
                  color=#ffffff>出处</FONT></TD>
                <TD>&nbsp; <SPAN 
                  id=ArticleTitle1_ArticleTitle1_lblSource></SPAN></TD></TR></TBODY></TABLE></TD></TR>
        <TR>
          <TD width=10></TD>
          <TD><SPAN id=ArticleContent1_ArticleContent1_lblContent>
            <P><FONT 
            color=#ff0000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            在Visual C++中使用内联汇编</FONT> </P>
            <P>&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; 
            作者:Yonsm<BR><FONT color=#ff8c00>一、内联汇编的优缺点</FONT> <BR><BR>&nbsp; 
            &nbsp;因为在Visual C++中使用内联汇编不需要额外的编译器和联接器,且可以处理Visual 
            C++中不能处理的一些事情,而且可以使用在C/C++中的变量,所以非常方便。内联汇编主要用于如下场合: <BR><BR>&nbsp; 
            &nbsp;1.使用汇编语言写函数; <BR>&nbsp; &nbsp;2.对速度要求非常高的代码; <BR>&nbsp; 
            &nbsp;3.设备驱动程序中直接访问硬件; <BR>&nbsp; &nbsp;4."Naked" Call的初始化和结束代码。 
            <BR>&nbsp; &nbsp; 
            &nbsp;//(."Naked",理解了意思,但是不知道怎么翻译^_^,大概就是不需要C/C++的编译器(自作聪明)生成的函数初始化和收尾代码,请参看MSDN的"Naked 
            Functions"的说明) <BR><BR>&nbsp; 
            &nbsp;内联汇编代码不易于移植,如果你的程序打算在不同类型的机器(比如x86和Alpha)上运行,应当尽量避免使用内联汇编。这时候你可以使用MASM,因为MASM支持更方便的的宏指令和数据指示符。 
            <BR><BR><BR><FONT color=#ff8c00>二、内联汇编关键字</FONT> <BR><BR>&nbsp; 
            &nbsp;在Visual C++使用内联汇编用到的是__asm关键字,这个关键字有两种使用方法: <BR><BR>&nbsp; 
            &nbsp;1.简单__asm块 <BR>&nbsp; &nbsp; &nbsp; &nbsp;__asm <BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;{ <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;MOV &nbsp; &nbsp; AL, 2 <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;MOV &nbsp; &nbsp; DX, 0XD007 <BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;OUT &nbsp; &nbsp; AL, DX <BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;} <BR><BR>&nbsp; &nbsp;2.在每条汇编指令之前加__asm关键字 <BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;__asm MOV &nbsp; AL, 2 <BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;__asm MOV &nbsp; DX, 0xD007 <BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;__asm OUT &nbsp; AL, DX <BR><BR>&nbsp; 
            &nbsp;因为__asm关键字是语句分隔符,因此你可以把汇编指令放在同一行: <BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;__asm MOV AL, 2 &nbsp; &nbsp; __asm MOV DX, 0XD007 &nbsp; 
            &nbsp;__asm OUT AL, DX <BR><BR>&nbsp; 
            &nbsp;显然,第一种方法和C/C++的风格很一致,并且有很多其它优点,因此推荐使用第一种方法。 <BR><BR>&nbsp; 
            &nbsp;不象在C/C++中的"{}",__asm块的"{}"不会影响C/C++变量的作用范围。同时,__asm块可以嵌套,嵌套也不会影响变量的作用范围。 
            <BR><BR><BR><BR><FONT color=#ff8c00>三、在__asm块中使用汇编语言</FONT> 
            <BR><BR>&nbsp; &nbsp;1.内联汇编指令集 <BR>&nbsp; &nbsp;内联汇编完全支持的Intel 
            486指令集,允许使用MMX指令。不支持的指令可以使用_EMIT伪指令定义(_EMIT伪指令说明见下文)。 <BR><BR>&nbsp; 
            &nbsp;2.MASM表达式 <BR>&nbsp; &nbsp;内联汇编可以使用MASM中的表达式。比如: MOV EAX, 1。 
            <BR><BR>&nbsp; &nbsp;3.数据指示符和操作符 <BR>&nbsp; 
            &nbsp;虽然__asm块中允许使用C/C++的数据类型和对象,但它不能用MASM指示符和操作符定义数据对象。这里特别指出,__asm块中不允许MASM中的定义指示符: 
            DB、DW、DD、DQ、DT和DF,也不允许DUP和THIS操作符。MASM结构和记录也不再有效,内联汇编不接受STRUC、RECORD、WIDTH或者MASK。 
            <BR><BR>&nbsp; &nbsp;4.EVEN和ALIGN指示符 <BR>&nbsp; 
            &nbsp;尽管内联汇编不支持大多数MASM指示符,但它支持EVEN和ALIGN,当需要的时候,这些指示符在汇编代码里面加入NOP(空操作)指令使标号对齐到特定边界。这样可以使某些处理器取指令时具有更高的效率。 
            <BR><BR>&nbsp; &nbsp;5.MASM宏指示符 <BR>&nbsp; 
            &nbsp;内联汇编不是宏汇编,不能使用MASM宏指示符(MACRO、REPT、IRC、IRP和ENDM)和宏操作符(&lt;&gt;、!、&amp;、%和.TYPE)。 
            <BR><BR>&nbsp; &nbsp;6.段说明 <BR>&nbsp; 
            &nbsp;必须使用寄存器来说明段,跨越段必须显式地说明,如ES:[BX]。 <BR><BR>&nbsp; 
            &nbsp;7.类型和变量大小 <BR>&nbsp; 
            &nbsp;我们可以使用LENGTH来取得C/C++中的数组中的元素个数,如果不是一个数组,则结果为一。使用SIZE来取得C/C++中变量的大小,一个变量的大小是LENGTH和TYPE的乘积。TYPE用来取得一个变量的大小,如果是一个数组,它得到的一个数组中的单个元素的大小。 
            <BR><BR>&nbsp; &nbsp;8.注释 <BR>&nbsp; 
            &nbsp;可以使用C/C++的注释,但推荐用ASM的注释,即";"号。 <BR><BR>&nbsp; &nbsp;9._EMIT伪指令 
            <BR>&nbsp; &nbsp;_EMIT伪指令相当于MASM中的DB,但一次只能定义一个字节,比如: <BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;__asm <BR>&nbsp; &nbsp; &nbsp; &nbsp;{ 
            <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JMP &nbsp; &nbsp; 
            _CodeOfAsm <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_EMIT 
            &nbsp; 0x00 &nbsp; &nbsp;; 定义混合在代码段的数据 <BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;_EMIT &nbsp; 0x01 <BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;_CodeOfAsm: <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; 
            这里是代码 <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_EMIT &nbsp; 0x90 
            &nbsp; &nbsp;; NOP指令 <BR>&nbsp; &nbsp; &nbsp; &nbsp;} 
            <BR><BR><BR><BR><FONT color=#ff8c00>四、在__asm块中使用C/C++语言元素</FONT> 
            <BR><BR>&nbsp; 
            &nbsp;C/C++与汇编可以混合使用,在内联汇编可以使用C/C++的变量和很多其它C/C++的元素。在__asm块中可以使用以下C/C++元素: 
            <BR><BR>&nbsp; &nbsp;1.符号,包括标号、变量和函数名; <BR><BR>&nbsp; 
            &nbsp;2.常量,包括符号常量和枚举型(enum)成员; <BR><BR>&nbsp; &nbsp;3.宏定义和预处理指示符; 
            <BR><BR>&nbsp; &nbsp;4.注释,包括"/**/"和"//"; <BR><BR>&nbsp; 
            &nbsp;5.类型名,包括所有MASM中合法的类型 <BR><BR>&nbsp; &nbsp;6.typedef名称, 
            像PTR、TYPE、特定的结构成员或枚举成员这样的通用操作符。 <BR><BR>&nbsp; 
            &nbsp;在__asm块中,可以使用C/C++或ASM的基数计数法(比如: 0x100和100H是相等的)。 
            <BR><BR>&nbsp; 
            &nbsp;__asm块中不能使用像&lt;&lt;一类的C/C++操作符。C/C++和MASM通用的操作符,比如"*"和"[]"操作符,都被认为是汇编语言的操作符。举个例子: 
            <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;int array[10]; <BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;__asm MOV array[6], BX ; &nbsp;Store BX at 
            array+6 (not scaled) <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;array[6] = 
            0; &nbsp; &nbsp; &nbsp; &nbsp; /* Store 0 at array+12 (scaled) */ 
            <BR><BR>&nbsp; &nbsp;* 小技巧: 内联汇编中,你可以使用TYPE操作符使作其与C一致。比如,下面两条语句是一样的: 
            <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;__asm MOV array[6 * TYPE int], 0 
            ; Store 0 at array + 12 <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;array[6] 
            = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            /* Store 0 at array + 12 */ <BR><BR>&nbsp; 
            &nbsp;内联汇编能通过变两名直接引用C/C++的变量。__asm块中可以引用任何符号,包括变量名。 <BR><BR>&nbsp; 
            &nbsp;如果C/C++中的类、结构或者枚举成员具有唯一的名称,如果在"."操作符之前不指定变量或者typedef名称,则__asm块中只能引用成员名称。然而,如果成员不是唯一的,你必须在"."操作符之前加上变量名或typedef名称。例如,下面的两个结构都具有same_name这个成员变量: 
            <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;struct first_type <BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;{ <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;char *weasel; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int 
            same_name; <BR>&nbsp; &nbsp; &nbsp; &nbsp;}; <BR><BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;struct second_type <BR>&nbsp; &nbsp; &nbsp; &nbsp;{ 
            <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int wonton; <BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;long same_name; <BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;}; <BR><BR>&nbsp; &nbsp;如果按下面声明变量: <BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;struct first_type hal; <BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;struct second_type oat; <BR><BR>&nbsp; 
            &nbsp;那么,所有引用same_name成员的地方都必须使用变量名,因为same_name不是唯一的。另外,上面的weasel变量具有唯一的名称,你可以仅仅使用它的成员名称来引用它: 
            <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;__asm <BR>&nbsp; &nbsp; &nbsp; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -