- 1、本文档共23页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
GCC嵌入汇编代码课件
呆木舟
Posts - 8? Articles - 11? Comments - 0?
【转】GCC 嵌入汇编代码
?4.?Basic Inline.
The format of basic inline assembly is very much straight forward. Its basic form is
基本汇编嵌入格式如下:
asm(assembly code);
Example.
asm(movl?%ecx?%eax);?/*?moves?the?contents?of?ecx?to?eax?*/??
__asm__(movb?%bh?(%eax));?/*moves?the?byte?from?bh?to?the?memory?pointed?by?eax?*/??
You might have noticed that here I’ve used?asm?and?__asm__. Both are valid. We can use__asm__?if the keywordasm?conflicts with something in our program. If we have more than one instructions, we write one per line in double quotes, and also suffix a ’\n’ and ’\t’ to the instruction. This is because gcc sends each instruction as a string toas(GAS) and by using the newline/tab we send correctly formatted lines to the assembler.
可能注意到了这里使用了 asm 和 __asm__. 都是有效的.如果关键字asm在程序中有冲突, 则可以使用__asm__. 如果我们需要使用一条以上的汇编指令, 我们应该每条占用一行, 用双引号括起,并加上\n和\t后缀. 这是因为gcc把用字符串的格式把汇编指令传给as(GAS), 然后利用换行符, 把它们转换成正确的汇编格式.
?Example.
__asm__?(movl?%eax,?%ebx\n\t??
?????????movl?$56,?%esi\n\t??
?????????movl?%ecx,?$label(%edx,%ebx,$4)\n\t??
?????????movb?%ah,?(%ebx));??
If in our code we touch (ie, change the contents) some registers and return from asm without fixing those changes, something bad is going to happen. This is because GCC have no idea about the changes in the register contents and this leads us to trouble, especially when compiler makes some optimizations. It will suppose that some register contains the value of some variable that we might have changed without informing GCC, and it continues like nothing happened. What we can do is either use those instructions having no side effects or fix things when we quit or wait for something to crash. This is where we want some extended functionality. Extended asm provides us with that functionality.
如果我们的代码里使用了寄存器, 并且在返回的时候没有还原它, 这将有坏的情况发生. 因为GCC并不知道寄存器的值改变了, 特别是编译器对代码进行优化的时候. 编译器会认为,那些存放变量的寄存器,我们并没有改变它,然后继续自己的优化. 为了避免这种情况, 要么, 我们不改变寄存器的值, 要么, 汇编函数返回之前, 还原寄存器使用前的值, 或者 等着代码崩溃(wait for something to crash
文档评论(0)