Experiment four: Use the same system call using the Library function API and the embed assembly code in C code two ways

Source: Internet
Author: User

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

Experimental Purpose:

Use the same system call in the Library function API and the C code to embed the assembly code, to understand how the system calls work.

Experimental process: This paper selects 24th and 47th system calls, respectively, to obtain the current user UID (user ID) and GID (group ID), that is, the simulation of the Linux system "id" command.

Write two pieces of code, respectively, using the Library function API and C code embedded assembly code, the source code is as follows:

UIDGID.C (using library function API mode):



Program to get the current execution user UID and GID by calling the Getuid () and Getgid () functions

Reference: "Advanced programming in the UNIX environment"

UIDGID_ASM.C (embed assembly code in C code mode):


Inline assembly code version of the source code in the original two lines through the API function to obtain the UID and GID codes commented out, with the assembly code replacement.

First, the EBX register is zeroed to indicate that no parameters are passed in.

The 0x18 and 0x2f (decimal 24 and 47) are then assigned to the EAX register, which indicates the system call number that needs to be called, and 24 is getuid,47 as Getgid.

Executes an int 0x80 to perform a system call.

The EAX register then holds the return value and assigns it to the output UID or GID variable, respectively.

Complete the system call for the entire assembly code.

Compile two source files, execute system ID commands and two compiled programs separately


The above respectively indicates that the normal user Ubuntu and the administrator user root executes the system with the command ID separately, the Library function API way Uidgid, the inline assembly method Uidgid_asm these three kinds of ways to run the result is the same.

Experimental Analysis:

Through the experimental results, the program successfully completed the system call to obtain the current user UID and GID operation, through the embedded assembly code can clearly see the call system calls work process.

First, the EBX register is zeroed to indicate that no parameters are passed in.

The 0x18 and 0x2f (decimal 24 and 47) are then assigned to the EAX register, which indicates the system call number that needs to be called, and 24 is getuid,47 as Getgid.

Executes an int 0x80 to perform a system call.

The EAX register then holds the return value and assigns it to the output UID or GID variable, respectively.

Complete the system call for the entire assembly code.

In a Linux system, the system call is triggered by activating the 0x80 interrupt, and the calling system call number implementation is assigned to the EAX memory, if there is an incoming parameter assignable to the EBX register, if more than 1 are assigned to EBX, ECX, edx, ESI, EDI, EBP, If there are more than 6, the pointer variable points to the other stack area, and if no parameter is passed in, the assignment is 0.

Experiment Summary:

Although the Intel X86 CPU has 4 execution level 0~3, only 0 and 3 levels are used in the Linux system, respectively, to represent the kernel state and the user state .

Some of the underlying, hardware, core operations must be in the kernel state to allow execution, for operating system programs and drivers exclusive, ordinary programs can only be executed in the user state. If the normal program needs to involve the operation of the kernel state, it needs to be implemented by the system call . The advantage of this is that the shielding platform related operation reduces the software development difficulty, enhances the system security, makes the program has the better portability (the Linux system and other UNIX systems follow the unified standard, the system calls basically the same).

Experiment four: Use the same system call using the Library function API and the embed assembly code in C code two ways

Related Article

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.