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个字节转成了字符串指针,但该指针是不确定会出现段错误!

猜你喜欢

转载自sy198704.iteye.com/blog/1005744