System call using assembly language and C

3 篇文章 0 订阅
2 篇文章 0 订阅

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

  • 选择一个系统调用(13号系统调用time除外),系统调用列表参见http://codelab.shiyanlou.com/xref/linux-3.18.6/arch/x86/syscalls/syscall_32.tbl
  • 参考视频中的方式使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
  • 根据本周所学知识分析系统调用的工作过程,撰写一篇署名博客,并在博客文章中注明“真实姓名(与最后申请证书的姓名务必一致) + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ”,博客内容的具体要求如下:
    • 题目自拟,内容围绕系统调用的工作机制进行
    • 博客中需要使用实验截图
    • 博客内容中需要仔细分析汇编代码调用系统调用的工作过程,特别是参数的传递的方式等。
    • 总结部分需要阐明自己对“系统调用的工作机制”的理解。

Let’s start!

Michael He 原创作品转载请注明出处《Linux内核分析》MOOC课程

select chdir to be the system call

numberabinameentry pointcompat entry point
12i386chdirsys_chdir

Using API to call chdir

#include<stdio.h>

int main()
{
    char buf[80];
    getcwd(buf, sizeof(buf)); 
    printf("Firstly the current directory is: %s\n", buf);

    chdir("/var");
    getcwd(buf, sizeof(buf)); 
    printf("Then the current directory is: %s\n", buf);

    return 0;
}

Using assembly language embed C to implement system call chdir

#include<stdio.h>

static __attribute__((used)) char * dir;
int main()
{
    char buf[80];
    getcwd(buf, sizeof(buf)); 
    printf("Firstly the current directory is: %s\n", buf);

    dir = "/var";
    //chdir("/var");
    asm volatile(
        "mov dir, %ebx\n\t"
        "mov $0xc, %eax\n\t"
        "int $0x80\n\t"
    );

    getcwd(buf, sizeof(buf)); 
    printf("Then the current directory is: %s\n", buf);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值