11.8 C Calls to Assembly Language

In Microsoft C/C++, you can write assembly-language programs either by using the inline assembler or by creating a stand-alone module using the Microsoft Macro Assembler (MASM). If you use the inline assembler, you do not need to take any special precautions other than those outlined in Chapter 6, “Using the Inline Assembler.” This section explains the techniques for interfacing your assembly-language routines with your C program.

When deciding whether to use the inline assembler or MASM, there are several considerations. Here is a list of advantages MASM provides over the inline assembler:

MASM supports declaration of data in MASM format; inline assembly does not.

MASM has a more powerful macro capability than does inline assembly.

Modules written for MASM can be interfaced more easily with modules written in more than one Microsoft high-level language.

MASM assembles large assembly-language programs more quickly than the inline assembler.

MASM supports assembly-language code written prior to the existence of the inline assembler.

MASM error messages and warnings are more complete than those of the inline assembler.

The inline assembler is far more efficient for some assembly-language programming tasks. Here are some of the benefits of the inline assembler:

You can do spot optimizations by including short sections of assembly-language code in your C programs with the inline assembler.

Code written in inline assembler does not necessarily incur the overhead of a function call; code assembled using MASM always does.

You can include inline assembly code in your C source files; code written for MASM must be in a separate file.