前置知识
系统调用
指令级别
参数传值顺序
EBX,EXC,EDX,ESI,EDI,EBP
查看系统调用号的路径
/home/shiyanlou/LinuxKernel/linux-3.18.6/arch/x86/syscalls/syscall_32.tbl
系统调用
mkdir系统调用
查出mkdir的系统调用号
函数格式
asmlinkage long sys_mkdir(const char __user *pathname, umode_t mode);
pathname
创建文件夹的路径名称
mode
创建文件夹的权限
编写test.c
程序测试mkdir 系统调用
创建成功返回0,否则返回-1
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(){
int ret;
ret = mkdir("xzx",0666);
if(ret == -1)
printf("mkdir xzx failed");
else
printf("mkdir xzx success!");
return 0;
}
test_asm.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(){
int ret;
char *filename="xxxx";
int mode=0666;
asm volatile(
"movl %2,%%ecx\n\t"
"movl %1,%%ebx\n\t"
"movl $0x27,%%eax\n\t"
"int 0x80\n\t"
:"=a"(ret)
:"b"(filename),"c"(mode)
);
if(ret == -1)
printf("make failed");
else
printf("make success!");
return 0;
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" //newname是第二个值,所以赋给ecx
"movl %1,%%ebx\n\t" //oldname 是第一个值,所以赋给ebx
"movl $0x26,%%eax\n\t" //rename的系统调用号是38
:"=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