Define _ ASM block as a C macro

Source: Internet
Author: User

For Microsoft

The C macro provides a simple way to insert assembly code. Source code, but they require additional issues because macros are expanded to a logical line.
To create a non-difficult macro, follow these rules:

  • The _ ASM block is enclosed in braces.

  • The _ ASM keyword before each Assembly command.

  • Use the old c annotation (/* Comment */) instead of the Assembly annotation (
    ; Comment) or single line comment C (// comment ).

To declare a macro, the following example defines a simple macro:

#define PORTIO __asm      \/* Port output */         \{                         \   __asm mov al, 2        \   __asm mov dx, 0xD007   \   __asm out dx, al       \}

At first glance, the last three _ ASM keywords are redundant.
Because macro expansion is a single row, but they are required:

__asm /* Port output */ { __asm mov al, 2  __asm mov dx, 0xD007 __asm out dx, al }

The third and fourth _ ASM keywords must be a statement separator.
The statement Block Separator uniquely identified by _ ASM is a line break and
_ ASM keyword. Because the block defined as a macro is a logical line, you must use
Every command of _ ASM.

Braces are very important. If they are omitted, the compiler can be confused by the C or C ++ statements related to the same row on the right side of the macro call.
The compiler cannot tell the Assembly Code location to stop because it does not have the right braces. Moreover, it can see the C or C ++ statement after the _ ASM block is used as the assembly instruction.

Annotate the Assembly style from the semicolon (;) To start the end of the row.
This always occurs in the macro, because the compiler ignores all content after the annotation, and ends with the logic line. The same is true for single-line C or C ++ annotations (
// Comment ). To prevent errors, use the old c annotation (
/* Comment */) is defined as a macro In the _ ASM block.

And when the C ++. Macro can contain parameters, _ ASM block write.
Unlike normal c macros, _ ASM macros cannot return values.
In this way, you can not use these macros in C or C ++ expressions.

Do not call this type of macro differently. For example, if the macro Declaration and
_ Fastcall conventions may cause unexpected results.

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.