您的位置:首页 > 运维架构 > Linux

Linux0.11内核--汇编代码实现C函数

2011-03-17 22:22 597 查看
Linux0.11内核中有很多函数都是在C语言里面声明,然后通过汇编来实现。例如/kernel/blk_drv/hd.c中函数中声明extern void hd_interrupt (void);
但是其他C代码中没有实现,找了好久,终于知道它在汇编代码中实现了。
下面自己写了个简单的实例来证实
test.c代码
#include<stdio.h>
extern void test1(void);
extern void test2(void);
extern void test3(void);

int main(void)
{
printf("0x%x/n",test1);
printf("0x%x/n",test2);
printf("0x%x/n",test3);
return 0;
}
[/code]
shixain.s代码
.text
.globl test1,test2,test3
test1:
pushl %eax
popl %eax
ret
test2:
pushl %eax
popl %eax
ret
test3:
pushl %eax
popl %eax
ret

假如没有shixian.s代码,编译test.c会报错
test.c: In function ‘main’:
test.c:8: warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type ‘void (*)(void)’
test.c:9: warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type ‘void (*)(void)’
test.c:10: warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type ‘void (*)(void)’
/tmp/ccqsI4JA.o: In function `main':
test.c:(.text+0x15): undefined reference to `test1'
test.c:(.text+0x29): undefined reference to `test2'
test.c:(.text+0x3d): undefined reference to `test3'
collect2: ld returned 1 exit status
但是加入汇编实现代码
命令:
as -gstabs -o shixian.o shixian.s
gcc -c -o test.o test.c
gcc -o result test.o shixian.o
编译生成可执行文件result
运行./result
结果:
0x8048440
0x8048443
0x8048446
注:在mingW下GCC和早期Linux版本下GCC,将汇编中的函数标号前加上_(一个下划线),而在如今的linux GCC下已不再加上_
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息