汇编和C同步使用
内核编写基础
使用global、extern,使C和汇编语言之间来回切换
实例:
demo1.c
void myprintf(char* msg,int len);
int demo(int a)
{
if(a == 2){
myprintf("ok,equal\n",10);
}
else{
myprintf("no,wahaha\n",11);
}
}
demo2.asm
extern demo
[section .data]
numa dd 2
[section .text]
global _start
global myprintf
_start:
push dword [numa]
call demo
add esp,4
mov ebx,0
mov eax,1
int 0x80
myprintf:
mov edx,[esp+8]
mov ecx,[esp+4]
mov ebx,1
mov eax,4
int 0x80
ret
编译链接方法:
[root@localhost demo1]# nasm -f elf demo2.asm -o demo2.o
[root@localhost demo1]# gcc -c demo1.c -o demo1.o
[root@localhost demo1]# ld -s demo1.
demo1.c demo1.c~ demo1.o
[root@localhost demo1]# ld -s demo1.o demo2.o -o demo
[root@localhost demo1]# ./demo
ok,equal