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.