C Program code embedded as assembly (four, macro function)

Source: Internet
Author: User

First come to recall how the macro is defined in the C language, the most basic is the definition of constants: #define Max_len 1024 This is the simplest constant macro definition, where the macro name is capitalized, the macro name will be replaced by the following value at Precompilation (Precompiled Four steps: 1, replace the macro name with the value after the macro name; 2 , load the contents of the included header file, 3, judge the logic false, and remove the logic false code; 4, delete the comment).

C language Macro function definition format: #define FUN_NAME (A, B, c) (function body) It is worth mentioning that the last one in the parameter is the return value; All the code in the function body must be displayed on one line, so a range character (backslash \) and the drive must follow the ENTER key. Otherwise, the battery life is not used;

Here is an example of the macro function, which is immediately followed by the previous blog jump question: If the same function is loaded repeatedly, and the function contains ASM assembly and the use of the tag location jump, then the program will report an error. Now let's do this with the macro function:

  #define GREATER (A, B, result) ({         asm ("CMP%1,%2\n\t"             "Jge ff\n\t" \      ///If an error is made, replace the FF with 0f             "movl%1,%0\n\t" c5/> "jmp ef\n" \/        /If error, replace EF with 1f             "ff:\n\t" \/         /If error, replace FF with 0             "movl%2,%0\n"             "EF:" \/             /If error , replace EF with 1             : "=r" (Result)             : "R" (a), "R" (b));})  void Fun1 () {     int a = 2;     int b = n;      int ret = 0;     GREATER (A, B, ret);     printf ("Greater result is:%d\n", ret); }  int Main () {     fun1 ();      int i = 1;     int j =;      int result = 0;      GREATER (I,j,result);     printf ("Greater result is:%d\n", result);     return 0; }


C Program code embedded as assembly (four, macro function)

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.