操作系统学习笔记(14) C和汇编相互调用
; 编译链接方法
; (ld 的‘-s’选项意为“strip all”)
; gcc -c not link
;
; [root@XXX XXX]# nasm -f elf foo.asm -o foo.o
; [root@XXX XXX]# gcc -c bar.c -o bar.o
; [root@XXX XXX]# ld -s foo.o bar.o -o foobar
; [root@XXX XXX]# ./foobar
; the 2nd one
; [root@XXX XXX]#
extern choose ; int choose(int a, int b);
[section .data] ; 数据在此
num1st dd 3
num2nd dd 4
[section .text] ; 代码在此
global _start ; 我们必须导出 _start 这个入口,以便让链接器识别。
global myprint ; 导出这个函数为了让 bar.c 使用
_start:
push num2nd ; ┓
push num1st ; ┃
call choose ; ┣ choose(num1st, num2nd);
add esp, 4 ; ┛
mov ebx, 0
mov eax, 1 ; sys_exit
int 0x80 ; 系统调用
; void myprint(char* msg, int len)
myprint:
mov edx, [esp + 8] ; len
mov ecx, [esp + 4] ; msg
mov ebx, 1
mov eax, 4 ; sys_write
int 0x80 ; 系统调用
ret
void myprint(char* msg, int len);
int choose(int a, int b)
{
if(a >= b){
myprint("the 1st one\n", 13);
}
else{
myprint("the 2nd one\n", 13);
}
return 0;
}
[root@localhost testc]# nasm -f elf foo.asm -o foo.o
[root@localhost testc]# gcc -c bar.c -o bar.o
[root@localhost testc]# ld -s foo.o bar.o -o foobar
[root@localhost testc]# ./foobar
the 2nd one
[root@localhost testc]#
本文来源于 自己动手写操作系统
相关文档:
pid_t pid=fork()
it has 3 situation for the return result pid
0 child
>0 parent process
<0 fork fail
fork create a new process and it parent live alse when the child process had been created ......
在此摘录C编译时出现的警告信息的意义。
1) warning: ISO C90 forbids mixed declarations and code
C语言是面向过程的语言,这个警告通常表示声明应该在其他代码的前面。
2) warning: initialization from incompatible pointer type
在Linux kernel中有许多callback函数,这个警告表明callback函数的实现中,或者返 ......
最近做的一些工作需要用到线程池技术,因此参考了一些资料和书籍,如《0bug c/c++商用工程之道》。
为此在linux平台上用纯c写了一个线程池的实现。
在此列出了原代码。
主要用到的数据结构有
1.struct thread_pool_t // thread pool 的实现代码
2.struct thread_pool_token_t &nb ......
来自bccn C语言论坛
首先声明一点,本文为转贴。
浅谈C中的malloc和free
作者:lj_860603 阅读人次:43013 文章来源:本站原创 发布时间:2006-8-5 网友评论(32)条
原帖及讨论:http://bbs.bccn.net/thread-82212-1-1.html
在C语言的学习中,对内 ......
下面说到的C语言中的知识,我只是在工作中经常见到或用到,但从未深究为什么,今天才却道原来简简单单下面孕育这无穷的知识点和我的盲点,是该边学习边记录了。
预处理器(Preprocessor)
1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)
#define ......