@gain
2016-03-23T08:41:20.000000Z
字数 980
阅读 1683
Linux内核
Linux
操作系统
@干宇航
原创作品转载请注明出处 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
实验环境:
Ubuntu 32bit 15.10
选择用系统调用exit
,系统调用号0x1
c代码,来自cppprefernce.com
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("data.txt","r");
if (fp == NULL) {
fprintf(stderr, "error opening file data.txt in function main()\n");
exit(1);
}
fclose(fp);
printf("Normal Return\n");
}
输出结果
error opening file data.txt in function main()
修改后的C内联汇编代码
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("data.txt","r");
if (fp == NULL) {
fprintf(stderr, "error opening file data.txt in function main()\n");
//exit(1);
asm volatile(
"mov $0x1,%ebx\n\t" ebx作为exit(1)中的参数1
"mov $0x1,%eax\n\t" eax中储存的系统调用号1
"int $0x80\n\t" 调用中断,进入内核态
);
}
fclose(fp);
printf("Normal Return\n");
}
输出结果
error opening file data.txt in function main()
asm volatile(
"mov $0x1,%ebx\n\t" ebx作为exit(1)中的参数1
"mov $0x1,%eax\n\t" eax中储存的系统调用号1
"int $0x80\n\t" 调用中断,进入内核态
);
总结:汇编代码调用系统调用时,ebx作为参数传入,然后eax是系统调用号。然后调用中断进入系统内核态。进入sys_exit,立即把eax的值压入内核栈。