The __asm Keyword

The __asm keyword invokes the inline assembler and can appear wherever a 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.

Below is a simple __asm block enclosed in braces. (The code prints the “beep” character, ASCII 7.)

__asm

{

mov ah, 2

mov dl, 7

int 21h

}

Alternatively, you can put __asm in front of each assembly instruction:

__asm mov ah, 2

__asm mov dl, 7

__asm int 21h

Since the __asm keyword is a statement separator, you can also put assembly
instructions on the same line:

__asm mov ah, 2 __asm mov dl, 7 __asm int 21h

Summary: Braces can prevent ambiguity and needless repetition.

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 code and avoid needless repetition of the __asm keyword. Braces can also prevent ambiguities. If you want to put a 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 statements begin. Finally, since the text in braces has the same format as ordinary MASM text, you can easily cut and paste text from existing MASM source files.

The braces enclosing an __asm block don't affect variable visibility, as do braces in C. You can also nest __asm blocks, but the nesting doesn't affect variable visibility.