一.fork的嵌入式汇编执行
#include <stdio.h> #include <unistd.h> int main(){ pid_t pid; asm volatile( "mov $0,%%ebx " "mov $0x2,%eax " "int 0x80 " "mov %%eax,%0 " :"=m"(tt) ); if(pid==0){ printf("child is running.. "); } else{ printf("father is running... "); } }
二.read的系统调用(带参数的系统调用)
int read(int fd,char buf[],int len){ int ret; asm volatile( "mov %3,%%edx " //count->edx "mov %2,%%ecx " //buf->ecx "mov %1,%%ebx " //fd->ebx "mov $0x3,%%eax " "int $0x80 " :"=m"(ret) :"b"(fd),"c"(buf),"d"(count) ); return ret; }
三.实验总结
通过这次实验,让我更加深刻地理解了系统调用的执行原理。当我们应用程序调用一个系统调用时,会触发一个int $0x80中断,由用户态进入内核态,同时由eax传入系统调用号,来最终使内核确定执行相应的内核程序。