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代码
假如没有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下已不再加上_
但是其他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下已不再加上_
相关文章推荐
- Linux0.11内核--汇编代码实现C函数
- linux 0.11 内核学习 -- rs_io.s,串口汇编代码
- linux 0.11 内核学习 -- rs_io.s,串口汇编代码
- LINUX0.11内核KERNEL包(内核代码)中的几个重要的文件分析
- Linux内核源码-boot下的汇编代码分析(Linux0.11)
- Linux 0.11内核代码笔记
- Linux 内核代码赏析与应用(二)-链表之实现
- 分享一个函数,linux内核0.11代码Vsprintf中的一个函数
- Linux0.11内核--启动引导代码分析bootsect.s
- Linux内核分析课程--通过反汇编一个简单的c程序,分析汇编代码并理解计算机如何工作的
- Linux0.11内核--启动引导代码分析setup.s
- Linux0.11内核--启动引导代码分析setup.s
- Linux0.11内核--启动引导代码分析setup.s
- Linux0.11内核--系统中断处理程序int 0x80实现原理
- Linux 0.11内核bootsect.s和loader.s的移植为AT&T汇编
- 通过内嵌汇编实现模拟时间片轮转多道程序的内核代码分析
- Linux0.11内核剖析--内核代码(kernel)--sched.c
- Linux0.11内核--系统中断处理程序int 0x80实现原理 .
- linux0.12之内核代码之『深入追踪变参函数的实现』
- Linux0.11内核--启动代码分析setup.s 建立页目录和页表