前置知识
系统调用
指令级别
参数传值顺序
EBX,EXC,EDX,ESI,EDI,EBP
系统调用
time系统调用
time_asm.c
#include<stdio.h>
#include<time.h>
int main(){
time_t tt;
struct tm *t;
asm volatile(
"mov $0,%%ebx\n\t"
"mov $0xd,%%eax\n\t"
"int 0x80\n\t"
"mov %%eax,%0\n\t"
:"=m"(tt)
);
t = localtime(&tt);
printf("time:%d:%d:%d:%d:%d:%d:\n",t->tm_year+1900,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
return 0;
}
这个代码有个地方好像是错的 t->tm_mda ===>t->tm_mday
rename 系统调用
rename.c
#include<stdio.h>
int main(){
int ret;
char *oldname="hello.c";
char *newname="newhello.c";
ret = rename(oldname,newname);
if(ret == 0)
printf("Rename ok");
else
printf("Rename failed");
return 0;
}
rename_asm.c
#include<stdio.h>
int main(){
int ret;
char *oldname="hello.c";
char *newname="newhello.c";
asm volatile(
"movl %2,%%ecx\n\t"
"movl %1,%%ebx\n\t"
"movl $0x26,%%eax\n\t"
:"=a"(ret)
:"b"(oldname),"c"(newname)
);
if(ret ==0 )
printf("Rename ok!");
else
printf("Rename failed");
return 0;
}
syscall.c
#include<stdio.h>
#include<sys/syscall.h>
int main(){
int ret;
char *oldname="hello.c";
char *newname="newhello.c";
ret = syscall(SYS_reanme,oldname,newname);
if(ret ==0 )
printf("Rename ok!");
else
printf("Rename failed");
return 0;
遇到问题
估计是因为 实验楼的环境是64位,所以会出错 加上这个,应该就行gcc rename_asm.c -o rename_asm -m32
|