Linux Platform x86 compilation (17): Using Linux system calls in assemblies

Source: Internet
Author: User

"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
as we have seen in the previous chapters, starting a system call requires the use of an int directive. When the Linux system call is located at Interrupt 0x80, when the int instruction is executed, all operations are transferred to the system call handler in the kernel, and the next instruction after the transfer to the int instruction is completed after execution. Linux system calls can be viewed in the following file (32-bit system):
$ cat/usr/ Include/asm/unistd_32.h#ifndef _asm_x86_unistd_32_h#define _asm_x86_unistd_32_h/* * This file contains the system call Numbers. */#define __nr_restart_syscall 0#define __nr_exit 1#define __nr_fork 2#define __nr_read 3#define __NR_write 4#def Ine __nr_open 5#define __nr_close 6#define __nr_waitpid 7#define __nr_creat 8#define __nr_link 9#define __NR_unlink 1 0 ... 
the integer after the system call name in the above file is the system call value, and each system call is assigned a unique number to identify it. in the example in the previous section we already know that the EAX register is used to hold the system call value. The expected value is passed to the EAX register before the int instruction is executed, and the system call is ready, and the next step is to give the system a call to prepare the parameters. and our own function input values are stored in the stack, the input parameters of the system call are stored in the register. The order in which the input values are stored in the register is also important, and the order in which the system calls the expected input values (1th through 5th parameters) is as follows:ebx, ecx, edx, ESI, EDI. System calls that require more than 6 input parameters use different methods. Arguments are passed to the system call, using the EBX register to hold a pointer to the memory location of the input parameter. in front of this series of articles we print "Hello world! The call code for the write system is used in the following section:
_start:                                 #函数在屏幕上输出hello world!movl $len,%edx               #第三个参数: string length movl $msg,%ecx             #第二个参数: Hello world! string movl $,% EBX                  #第一个参数: Output file descriptor Movl $4,%eax                  #系统调用号sys_writeint $0x80                            #调用内核功能
The input values are assigned to the EBX, ECX, edx registers, and the system call number 4 is assigned to the EAX register. The return value of the system call is stored in the EAX register, which can be used to determine the return value of the system call. Note that the system call function and the C library function are distinguished by the input parameters of the C library function in the stack, while the input parameters of the system call are in the register. In the use of C library functions or system call functions, the principle is to see which method is more suitable for the application of time. The advantage of C library is that it is easy to migrate between operating systems and C libraries can use shared libraries between programs to reduce memory requirements. The advantage of a Linux system call is that you don't need it. The external library is linked to the program, creating the shortest and fastest code possible.

Linux Platform x86 compilation (17): Using Linux system calls in assemblies

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.