C Language and assembly language mutual invocation principle and example

Source: Internet
Author: User

The following two are a FOO.ASM (assembly language file), BAR.C (c file)
 First to understand why C language can call assembly language, and why assembly language can call C. In fact, whether it is the C language or assembly language want to execute is the final    compile link   
 note that    compile link    This two-step,    compile    The resulting binary file is not executable,    link   
 It's important to make sure that compiling and linking is two steps, and the resulting file format is not the same. 
 The compiled generated file is in a certain format, including the function symbol table, parameter table ... Such information, which is primarily provided for use in the link stage,    How is a function call called? is the symbol that specifies the function to use? So the link stage is to turn the symbol of the function call into a relative address (pay special attention to this phase, because this process makes it possible for C and Assembly languages to invoke each other)   
gcc-m32-c-o bar.o bar.c   Generated BAR.O (is an intermediate file, not an executable file, with a symbol table, a parameter table, and so on)   
nasm-f elf-o foo.o foo.asm  generates FOO.O (   is an intermediate file, not an executable file,   
 Now that you have generated the. O Intermediate file in the same format, the LD linker can link two intermediate files into a binary executable, and the   ld the main task is to BAR.O The function symbol for the reciprocal reference in FOO.O      relative address    (relative addresses are converted to absolute addresses when binary files are loaded into memory)   
Ld-m elf_i386-s-o foobar foo.o bar.o (link becomes binary executable)
Process:
foo.asm
extern choose; [section. data]num1st DQ 3num2nd dq 4[section. Text]global mainglobal myprintmain:push Qword [num2nd] Push Qword [num1s  T] Call Choose Add esp,8 mov ebx,0 mov eax,1 int 0x80;  Pop Qword [num1st];  Pop Qword [Num2nd]myprint:mov edx,[esp+8] mov ecx,[esp+4] mov ebx,1 mov eax,4 int 0x80;  Pop Qword [num1st]; Pop Qword [num2nd] ret  
bar.c
void myprint(char * msg ,int len);int choose(int a,int b) {   if (a>=b){    myprint("the 1st one\n",13);}  else {    myprint("the 2nd one\n",13);}  return 0;}

C Language and assembly language mutual invocation principle and example

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.