The __asm keyword invokes the inline assembler and can appear wherever a C or C++ statement is legal. It cannot appear by itself. It must be followed by an assembly instruction, a group of instructions enclosed in braces, or, at the very least, an empty pair of braces. The term “__asm block” here refers to any instruction or group of instructions, whether or not in braces.
The following code fragment is a simple __asm block enclosed in braces:
__asm
{
mov al, 2
mov dx, 0xD007
out al, dx
}
Alternatively, you can put __asm in front of each assembly instruction:
__asm mov al, 2
__asm mov dx, 0xD007
__asm out al, dx
Because the __asm keyword is a statement separator, you can also put assembly instructions on the same line:
__asm mov al, 2 __asm mov dx, 0xD007 __asm out al, dx
All three examples generate the same code, but the first style (enclosing the __asm block in braces) has some advantages. The braces clearly separate assembly code from C or C++ code and avoid needless repetition of the __asm keyword. Braces can also prevent ambiguities. If you want to put a C or C++ statement on the same line as an __asm block, you must enclose the block in braces. Without the braces, the compiler cannot tell where assembly code stops and C or C++ statements begin. Finally, because the text in braces has the same format as ordinary MASM text, you can easily cut and paste text from existing MASM source files.
Unlike braces in C and C++, the braces enclosing an __asm block don’t affect variable scope. You can also nest __asm blocks; nesting does not affect variable scope.