zoukankan      html  css  js  c++  java
  • 《Linux内核分析》 week4作业-使用嵌入式汇编调用一个系统调用

    一.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传入系统调用号,来最终使内核确定执行相应的内核程序。

  • 相关阅读:
    二分专题
    数据结构-图
    Linux文件基本属性(以ls -l输出为例解释)
    shell脚本版素数筛
    Linux whereis,which
    Linux外网代理配置
    Linux三剑客
    Elasticsearch集群搭建(Linux)
    测试之路
    我的另一半
  • 原文地址:https://www.cnblogs.com/sixue/p/4457540.html
Copyright © 2011-2022 走看看