Invoking system calls using assembly code

Source: Internet
Author: User

"Casualet + Original works reproduced please specify the source +" Linux kernel analysis "MOOC course http://mooc.study.163.com/course/USTC-1000029000",

Linux systems provide a lot of system calls, this is the user program into 0 privilege level and hardware interaction interface. When we are programming in C, a lot of library functions are used to encapsulate system calls. All system calls are implemented through the int 0x80 in a soft interrupt way, in order to distinguish different system calls, there is a system call number, can be queried through this URL: http://codelab.shiyanlou.com/xref/ LINUX-3.18.6/ARCH/X86/SYSCALLS/SYSCALL_32.TBL This system call number can be passed through the EAX register. That is, we can call the appropriate system call (interrupt handler) by passing the system call number to the register EAX and then using the INT 0x80 directive. After the system call execution completes, there will be a return value for the function, which is returned by the EAX register. So, after the execution of the system call, we can get the return value of the system call through EAX, we show this process through an example program:

#include <stdio.h> #include <time.h> #include <unistd.h>int main () {    pid_t my_id;    ASM volatile (        "mov $20,%%eax\n\t"//20 is transmitted as EAX, indicating the use of number 20th system call         "int $0x80\n\t"//Interrupt, System call entry        "mov%%eax,%0\n\t" EAX is returned to, written to the memory variable my_id.        : "=m" (my_id)    );    printf ("asm_result=%d, api_result=%d\n", my_id, Getpid ());    return 0;}

We save the above program as SYS_ASM.C and then compile it through GCC sys_asm.c-o sys_asm-m32 and then use the./sys_asm to run.

The execution result of the above program is:

Represents the result of the assembly code run, and the results of the API run. We can see that the results are the same.

Invoking system calls using assembly code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.