GCC跟NASM联合编译
2016-08-22 16:58
316 查看
GCC跟NASM联合编译
GCC和NASM联合编译GCC和NASM
GCC不用说,C语言的编译器。NASM是一个x86的汇编器,用来编译汇编的。最近在研究GCC和NASM联合编译,为什么要研究二者的联合编译呢?GCC中能嵌套汇编代码啊?这是因为GCC中嵌套的汇编代码是AT&T的汇编代码不是Interl的。其语法格式实在是太复杂了,不是适合初学者(其实是我不会其语法)。下面我们结合一个例子来将二者如何联合使用。1.建立示例工程
示例工程很简单,就下面三个文件:main.c ---C代码 hello.asm --- 汇编代码 Makefile ---编译命令
其中Makefile的内容如下:
all: nasm -f elf hello.asm -o hello.o gcc -o main main.c hello.o
其中nasm的参数 -f elf表示输出文件的格式为ELF格式。
2.NASM和C互相调用对方的函数
先看例子的代码:extern print_helloworld [section .text] global print_two_hello_world print_two_hello_world: call print_helloworld call print_helloworld
#include "stdio.h" extern void print_two_hello_world(); char *strhello = "Hello,world!\n"; void print_helloworld () { printf ("%s",strhello); } int main () { print_two_hello_world(); return 0; }
首先看NASM代码,先导入一个外部函数print_helloworld(),此函数是C语言定义的一个函数。接下来是定义了一个函数print_two_hello_world,用global关键字导出使其可以在C中调用,函数的内容是调用了两次print_helloword()。
在来看C代码,代码很简单就具体讲了,这里主要数一下我们将主函数放在了C代码中写,这时因为我们要使用C代码的函数库中的printf()函数,如果C代码中没有什么库的依赖可以将主函数放在汇编代码中,具体的实现请参考《自己动手写操作系统》一书。
3.NASM和C互相调用对方的变量
同样先看代码:global string extern strhello [section .data] string: db 'I am Chinese.',0x0A,0x0 [section .text] global print_hello global cpy_mem print_hello: mov edx, 13 mov ecx,[strhello] mov ebx,1 mov eax,4 int 0x80
#include "stdio.h" #include "string.h" extern char *string; extern void print_hello(); extern cpy_mem (void *dest, int len); char *strhello = "Hello,world!\n"; char *str = NULL; int main () { printf ("%x\n",&string); str = &string; printf ("%s", str); print_hello (); return 0; }
Make后的执行结果如下:
具体的代码内容就部分析了,这里主要将两个关键的地方。
1.在C语言中定义了一个strhello的字符串变量,在C语言中strhello表示的是字符串的首地址,比如字符串的地址是0xa00001,而strhello是个指针即4字节其地址为0xb00001,在C语言中strhello表示的值是0xa00001字符串的首地址,但到了NASM中则表示的strhello变量的首地址了0xb00001,所以
mov ecx,[strhello]
代码中加了中括号表示是内容,这一点一定要注意,否则会出错!!
2.第二点要注意的是,在NASM中定义了一个字符串string,在C语言中导入的话,就是表示字符串的首地址,因此要引用该字符串一定要取其地址,不要直接转为(char*)类型直接用,否则出错,这是因为直接转的话,就将字符串的前4个字节转成了字符串指针,但该指针是不确定会出现段错误!
相关文章推荐
- 使用GCC和NASM在Linux下联合编译
- 使用GCC和NASM在Linux下联合编译
- Ubuntu下用gcc与nasm联合编译
- Ubuntu下用gcc与nasm联合编译
- GCC 和 NASM 联合编译,汇编函数前要有引到下划线 _
- 【转】GCC和NASM联合编译
- NASM GCC混合编译16位二进制文件。。。。
- Freepascal for CE和gcc for arm的联合编译
- 如何编译文件(gcc + nasm)
- 如何编译文件(gcc + nasm)
- GCC编译“Value too large for defined data type”错误解决办法
- g++和gcc基本的编译命令
- Centos 6.9 编译安装gcc 4.8.5
- 关于阿里云ESC上go语言项目编译6l: running gcc failed: Cannot allocate memory
- GCC编译单个或者多个源文件helloworld.c
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题——gcc编译C++代码
- 解决编译问题:arm-none-linux-gnueabi-gcc: directory: No such file or directory
- Ubuntu 12.04嵌入式交叉编译环境arm-linux-gcc搭建过程图解
- gcc 编译选项
- GCC 编译链接命令用法