博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C与asm链接和内嵌
阅读量:7102 次
发布时间:2019-06-28

本文共 1205 字,大约阅读时间需要 4 分钟。

1 内嵌汇编

1)__asm__用于指示编译器在此插入汇编语句
2)__volatile__用于告诉编译器,严禁将此处的汇编语句与其它的语句重组合优化。 即:原原本本按原来的样子处理这这里的汇编。

 

 

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 keyword asm 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 to as(GAS) and by using the newline/tab we send correctly formatted lines to the assembler.

2 c文件和asm文件链接(使用gnu as汇编器)

先假设c文件中有函数c_func,调用asm文件的函数asm_func,那么c中的声明为:

void c_func(); void asm_func();

而在asm中要有相应的函数和声明为,现在假设c函数使用c calling conversion。那么name mangle的时候会在名字前加上下划线变为_c_func。

所以在汇编asm和c一起链接的时候,一定要确定好c中使用的那种方式的name mangle。对应的asm中的代码为:

.text .extern _HelloWorld _newSleep:     .global _newSleep     call _HelloWorld     ret .end

加了下划线是因为c编译时候有name mangle的原因。

命令为:
gcc -o sth.exe file.c file.s.

转载地址:http://plkhl.baihongyu.com/

你可能感兴趣的文章
Synchronized之一:基本使用
查看>>
Oracle与Sql Server复制表结构和数据
查看>>
iOS 辛格尔顿
查看>>
C#动态调用WCF接口,两种方式任你选。
查看>>
【AngularJS】—— 13 服务Service
查看>>
锐捷上网认证常见问题及解决办法
查看>>
Eclipse+超快速的模拟器Genymotion开展Android申请书(第一步:安装和配置Genymotion)...
查看>>
MySQL查看一个表的创建文本以及删除表某列的索引
查看>>
BZOJ3009 : 集合
查看>>
android图片压缩的3种方法实例
查看>>
SVN(TortoiseSVN)提交时忽略bin跟obj目录
查看>>
tophat
查看>>
Codeforces Round #326 (Div. 2) A. Duff and Meat 水题
查看>>
最优二叉查找树
查看>>
Android屏幕适配全攻略(最权威的官方适配指导) (转)
查看>>
AutoMapper(七)
查看>>
内存数据库:memcached与redis技术的对比试验
查看>>
仿58同城UITableViewCell动画
查看>>
android service 的各种用法(IPC、AIDL)
查看>>
MongoDB聚合运算之mapReduce函数的使用(11)
查看>>