Using inline assembler in Visual C + +

Source: Internet
Author: User

I. Advantages and disadvantages of inline compilation

Because using inline assembler in Visual C + + does not require additional compilers and connectors, and can handle things that are not possible in Visual C + +, and can be used in C + + variables, it is convenient. The inline assembly is mainly used in the following situations:

1. Use assembly language to write functions;

2. Code that requires very high speed;

3. Direct access to hardware in the device driver;

4. Initialization and ending code for "naked" call.

(。 "Naked", understand the meaning, but do not know how to translate, presumably is not required C/s compiler (SMART) generated function initialization and closure code, see MSDN "Naked <i>function</i>s" description)

Inline assembler code is not easy to migrate, and if your program is intended to run on different types of machines (such as x86 and Alpha), you should avoid using inline compilations as much as possible. At this point you can use MASM because the MASM supports more convenient macro directives and data indicators.

Second, inline assembly keywords

The __ASM keyword is used in Visual C + + using inline assembler, and there are two ways to use this keyword:

1. Simple __asm block

__asm
  {
  MOV   AL, 2
  MOV   DX, 0xD007
  OUT   AL, DX
  }

2. Add __asm keyword before each assembly instruction

__asm MOV AL, 2

__asm MOV DX, 0xd007

__asm out AL, DX

Because the __ASM keyword is a statement delimiter, you can put the assembly instructions on the same line:

__asm MOV AL, 2

__asm MOV DX, 0xd007

__asm out AL, DX

Obviously, the first method is consistent with the style of C + + and has many other advantages, so the first method is recommended.

Unlike the "{}" in C + +, the __asm block's "{}" does not affect the scope of the C + + variable. At the same time, __asm blocks can be nested, and nesting does not affect the scope of variables.

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.