Linux Platform x86 compilation (16): Invoking C library functions in assembly language

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"
in fact, in assembly language can also use C library functions, this section we look at how to call C library function in assembly language, so that our program looks very convenient to interact with the user. C Library includes the C program common drink many functions, such as printf and exit, and so on, we immediately followed the knowledge of the previous section to achieve a two-integer to calculate and output the calculation results of the program.
# libc.s.section. DataOutput:    . Asciz "The result is%d. \ n"     # The printf function requires a string that ends with a null character. Section. BSS    . Lcomm result , 4.section. Text.globl _start_start:    NOP    movl $, result    movl%esp,%EBP    PUSHL 8 (%EBP)           # Press the first command-line argument pointer into the stack               call Atoi # Converts the argument string to integer    movl%eax, and the result       # Atoi function return value is returned to the EAX register PUSHL    (%EBP          # Press the second command-line argument pointer into the stack call    atoi    addl%eax, result    pushl result            # passes the arguments to the function printf, which must be pressed into the stack.    PUSHL $output            # The arguments are placed in the order opposite the order that printf gets. Call    printf    PUSHL $ call    exit
Direct make compile link result:
$ makeas-gstabs-  o libc.o libc.sld-o arg libc.olibc.o:libc.s:18:undefined reference to ' Atoi ' Libc.o:libc.s:22:unde Fined reference to ' atoi ' libc.o:libc.s:27:undefined reference to ' printf ' libc.o:libc.s:30:undefined reference to ' exit ' Make: * * * [ARG] Error 1
the hint function is undefined, stating that the C library file is not connected to the program target code. On Linux systems, there are two ways to connect C functions to assembly languages: static links and dynamic links. Static link the Function object file code is directly linked to the application's executable file, so that the executable file becomes huge, in fact, we write our own library is static link, and the system's library generally use dynamic link. Dynamic linking does not link the function code to the executable program, and the dynamic link library is called by the operating system when the program starts. On Linux systems, the standard C dynamic library is located in the libc.so.x file, where x values represent the version of the library. In order to link the libc.so file, you must use the-l parameter of the GNU connector, which is linked in a similar way to the C-Language link library. So connect using the following method when linking:ld-o [email protected] $ (src_obj)-DYNAMIC-LINKER/LIB/LD-LINUX.SO.2-LCafter the modification, the compilation succeeds.
$./LIBC 1 The result is 46. $./LIBC 23The result is 123. $
this way the program prints out the results directly. Compared to our previous program, the current program can directly output the results in the terminal, without the need to see the program's return code.

Linux Platform x86 compilation (16): Invoking C library functions in assembly language

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.