当用户态进程调用一个系统调用时,CPU切换为内核态,并开始执行一个内核函数;
在Linux中是通过执行int $0x80来执行系统调用,这条汇编指令产生向量为128的编程异常。
内核实现了很多不同的系统调用,进程需要指明系统调用号作为参数进行调用,使用eax寄存器。
寄存器传参数有如下限制:
1> 每个参数的长度不能超过寄存器的长度,即32位;
2> 在系统调用号(eax)之外,参数的个数不能超过6个(ebx,ecx,edx,esi,edi,ebp)。
下面通过对getpid系统调用函数从调用API和汇编方式进行分析:
<span style="font-size:18px;">#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { pid_t pid; pid = getpid(); printf("The pid of this program is %d\n", pid); return 0; }</span>
<span style="font-size:18px;">#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { pid_t pid; //the first arg use %ebx,here not have input arg. asm volatile ( <span style="white-space:pre"> </span>"mov $0x14,%%eax\n\t" "int $0x80\n\t" "movl %%eax,%0\n\t" : "+r" (pid) ); printf("The pid of this program is %d\n", pid); return 0; }</span>
原文:http://blog.csdn.net/kaiandshan/article/details/44627215