Programming in assembly language in Linux: Using C-library functions

Source: Internet
Author: User
In the Linux environment, the compilation language programming is preliminary-using C library functions-general Linux technology-Linux programming and kernel information. The following is a detailed description. Cpuid. the s program uses the Linux System Call (int $0x80) to display the string and exit the program. If the C function library is installed in the system, the C function can also be used to achieve the same purpose.

This article describes how to use C language library functions in assembly languages. The example program is the improved cpuid. s version cpuid2.s that displays CPU vendor information. In cpuid2.s, the printf and exit functions of the Standard C library are used to replace Linux system calls. The routine is as follows:

# Cpuid2.s -- Using C labrary cballs
. Section. data
Output:
. Asciz "The processor Vender is '% s' \ n"
. Section. bss
. Lcomm buffer, 12
. Section. text
. Globl _ start
_ Start:
Movl $0, % eax
Cpuid
Movl $ buffer, % edi
Movl % ebx, (% edi) // indicates the buffer memory directed to % edi, which is the same as cpuid. s.
Movl % edx, 4 (% edi) // write the content of the three registers at the position, which stores
Movl % ecx, 8 (% edi) // string of CPU vendor information (12 bytes)
Pushl $ buffer // stack buffer and output to provide parameters for printf
Pushl $ output
Call printf // call C's printf function
Addl $8, % esp // roll back the stack pointer to 8 bytes to clear the printf Parameter
// Purpose
Pushl $0
Call exit

Here,. asciz adds an empty character (\ 0 in C) at the end of the string when defining the string, so that printf can understand the string .. Lcomm declares a fixed length of uninitialized data in the local memory area. Here, it initializes 12 bytes of space.

In The program, The buffer and output memory locations are The parameter values to be passed to printf. One is The "The processor Vender is '% s' \ n" string, another buffer is filled with the result returned by cpuid (in the three registers of ebx, edx, and ecx. Parameters need to be passed through the stack, so we use

Pushl $ buffer
Pushl $ output

Import the parameters to the stack. The parameters obtained by printf are from the right to the left, that is, buffer and output are obtained first. Therefore, buffer must be followed into the stack. After the parameter is imported into the stack, call printf using the call command. Exit is the same as that of exit. A constant 0 is used.

Then compile the Connection Program:

$ As-o cpuid2.o cpuid2.s
$ Ld-o cpuid2 cpuid2.o
Cpuid2.o: In function '_ start ':
Cpuid2.o (. text + 0x3f): undefined reference to 'printf'
Cpuid2.o (. text + 0x46): undefined reference to 'eg'
$

There was no problem during assembly, but an error occurred while connecting the program in the original way. The reason is that ld does not search for the C library itself and must manually specify the C library file to connect to the program. Use the command-l, which is basically the same as the-l of gcc. The-l parameter is the library name (such as libc. so c is the name of the database) instead of the full name of the database file.

$ Ld-o cpuid2-lc cpuid2.o
$./Cpuid2
Bash:./cpuid2: No such file or directory
$

There is no connection problem, but there is another problem when running the program. This is because dynamic connection is used, so the C function is not included in the executable program and needs to be loaded by another program at runtime. ld does not know where the program is located, so we have to manually specify

$ Ld-dynamic-linker/lib/ld-linux.so.2-lc-o cpuid2 cpuid2.o
$./Cpuid2
The processor Vender is 'genuineintel'
$

The operation is successful, where the ld-linux.so.2 is a dynamic loader to look up libc. so

Summary:
If an external C-library function is used in the assembler, you must specify relevant parameters to connect to the library during assembly connection, in contrast, many tasks are automatically completed during gcc c program compilation, which is much more convenient.
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.