C Language and assembly language mutual invocation principle and example

Source: Internet
Author: User

C-language and assembly language mutual invocation principle and example 1. Principle

In fact, whether it is the C language or assembly language want to do is the final compile link to become a binary file.
It is important to make sure that compilation and linking are two steps, and the resulting file format is not the same.
Compile the generated file is a certain format, including the function symbol table, parameter table ... Such information, which is mainly provided to the link stage use, how is the 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).

2. The following two are a HELLO.ASM (assembly language file), MAIN.C (c file)
;hello.asmextern print_helloworld  [section .text]  global print_two_hello_world  print_two_hello_world:    call print_helloworld  call print_helloworld  /* main.c */#include "stdio.h"  extern void print_two_hello_world();    char *strhello = "Hello,world!\n";    void print_helloworld ()  {          printf ("%s",strhello);  }    int  main ()  {          print_two_hello_world();          return 0;  }  

First look at the NASM code, first import an external function print_helloworld (), this function is a C language definition of a function. The next step is to define a function Print_two_hello_world, which is exported with the Global keyword so that it can be called in C, and the contents of the function are called two times Print_helloword ().
In the C code, the code is very simple to say, the main number here we put the main function in the C code to write, because we want to use C code in the function library of the printf () function, if there is no library in C code of the dependency can put the main function in the assembly code

3.NASM and C call each other's variables

NASM Code

 global string  extern strhello  [section .data]  string:      db ‘I am Chinese.‘,0x0A,0x0  [section .text]      global print_hello      global cpy_mem  print_hello:          mov edx, 13      mov ecx,[strhello]      mov ebx,1      mov eax,4      int 0x80  

C code

#include "stdio.h"  #include "string.h"  extern char *string;  extern void print_hello();  extern cpy_mem (void *dest, int len);  char *strhello = "Hello,world!\n";  char *str = NULL;  int  main ()  {          printf ("%x\n",&string);          str = &string;          printf ("%s", str);                   print_hello ();          return 0;  }  

The results of the make are as follows:
I am Chinese.
hello,world!
Specific code content on the part of the analysis, here are mainly two key places.

1. In C, a Strhello string variable is defined, in C language Strhello represents the first address of the string, such as the address of the string is 0xa00001, and Strhello is a pointer 4 bytes whose address is 0xb00001, In the C language, the value represented by Strhello is the first address of the 0xa00001 string, but the first address of the Strhello variable represented in NASM is 0xb00001, so

NASM Code

mov ecx,[strhello]  

It is important to note that the brackets in the code indicate the contents, otherwise there will be an error!!

2. The 2nd note is that in NASM, a string is defined in the C language, which is the first address of the string, so to refer to the string must take its address, do not directly into the (char*) type directly with, otherwise an error, this is because the direct turn, The first 4 bytes of the string are converted to a string pointer, but the pointer is indeterminate and a segment error occurs!

C language and assembly language mutual invocation principle and instance

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.