Enhanced asm facility

Enhanced asm facility

Although the ability to write portable code is one reason for using the C language, sometimes it is necessary to introduce machine-specific assembly language instructions into C code. This need arises most often within operating system code that must deal with hardware registers that would otherwise be inaccessible from C. The asm facility makes it possible to introduce this assembly code.

In earlier versions of C the asm facility was primitive. You included a line that looked like a call on the function asm , which took one argument, a string:

   	asm("assembly instruction here");
Unfortunately this technique has shortcomings when the assembly instruction needs to reference C language operands. You have to guess the register or stack location into which the compiler would put the operand and encode that location into the instruction. If the compiler's allocation scheme changed, or, more likely, if the C code surrounding the asm changed, the correct location for the operand in the asm would also change. You would have to be aware that the C code would affect the asm and change it accordingly.

The new facility presented here is upwardly compatible with old code, since it retains the old capability. In addition, it allows you to define asm macros that describe how machine instructions should be generated when their operands take particular forms that the compiler recognizes, such as register or stack variables.

NOTE: Although this enhanced asm facility is easier to use than before, you are still strongly discouraged from using it for routine applications because those applications will not be portable to different machines. The primary intended use of the asm facility is to help implement operating systems in a clean way.

The optimizer (cc -O) may work incorrectly on C programs that use the asm facility, particularly when the asm macros contain instructions or labels that are unlike those that the C compiler generates. Furthermore, you may need to rewrite asm code in the future to maximize its benefits as new optimization technology is introduced into the compilation system.

Next topic: Definition of terms

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003