6.10 Optimizing

The presence of an __asm block in a function affects optimization in several ways. First, the compiler doesn't try to optimize the __asm block itself. What you write in assembly language is exactly what you get.

Second, the presence of an __asm block affects register variable storage. Under normal circumstances (unless you suppress optimization with the /Od option) the compiler automatically stores variables in registers. This is not done, however, in any function that contains an __asm block. To get register variable storage in such a function, you must request it with the register keyword.

Since the compiler stores register variables in the SI and DI registers, these registers represent variables in functions that request register storage. The first eligible variable is stored in SI and the second in DI. Preserve SI and DI in such functions unless you want to change the register variables.

Keep in mind that the name of a variable declared with register translates directly into a register reference (assuming a register is available for such use). For example, if you declare

register int sample;

and the variable sample happens to be stored in SI, the __asm instruction

__asm mov ax, sample

is equivalent to

__asm mov ax, si

If you declare a variable with register and the compiler cannot store the variable in a register, the compiler issues a warning to that effect at compile time. You must remove the register declaration from that variable to get rid of the warning.

Register variables are the exception to the general rule that an assembly-language statement can contain no more than one C or C++ symbol. If one of the symbols is a register variable, for example,

register int v1;

int v2;

then an instruction can use two C or C++ symbols, as in

mov v1, v2

Finally, the presence of inline assembly code inhibits the following optimizations for the entire function in which the code appears:

Loop ( /Ol )

Global register allocation ( /Oe )

Global optimizations and common subexpressions ( /Og )

These optimizations are suppressed no matter which compiler options you use.