
In-line Assembly Code
Motorola
Mixing C and Assembly Language
For More Information On This Product,
Go to: www.freescale.com
5-3
5.2.1 Instruction Template
The first argument of the
_ _asm()
extension is the instruction template or assembler
instruction template. This instruction template is mandatory, and describes the line or lines
of assembly language to be placed into the compiler’s assembly language output (see
Section in Section 5.2.4). This template is not parsed for assembly language syntax
violations and is simply written to the compiler output. As a result, the compiler will not
detect assembly-time errors. These errors will be caught by the assembler.
More than one assembly instruction can be put into a single instruction template by using
the line separator “\n”. The line separator, or newline, can be utilized as in a normal C
statement. The line continuation sequence, a “\” followed by an immediate newline, is
particularly useful when an instruction template contains an assembly instruction that is
too long to fit in one source line (see Section 5-18 and Section in Section 5.2.4). Other C
language character constants such as “\t”, “\f”, etc. can also be used in the instruction
template.
In many situations, it is desirable to use the values of C variables and/or expressions
directly in the instruction template. Since all memory and register accesses are
accomplished through variables, manipulating memory and registers directly using
assembly code requires knowledge of their locations. Without optimizations, the current
value of a variable will be maintained in memory at a specific address. However, an
optimizing C compiler such as DSP56KCC may retain a variable in a register and perform
operations on that variable without updating the memory location corresponding to the
variable between operations. This enhances the performance of the compiled code but
makes accessing variables in memory risky. In order to guarantee that the correct value of
a variable is returned when it is referenced, a mechanism called operand expansion string
(OES) is provided.
The OES allows a variable to be securely accessed even though its current location is
unknown. The operand expansion string is a substring of the instruction template and
begins with the character “%”. This string is usually two or three characters long and
provides the compiler with special information about an operand, and how its reference
should be printed. An OES must reference only one C variable or expression, which in
turn must be listed in either one or both operand lists (see Section 5.2.2). The OES is
parsed by the compiler and gives sufficient information to allow the variable to be
correctly referenced by the assembly language instruction in the instruction template.
Most examples in Section 5.2.4 include an OES.
The OES syntax is:
% [modifier] operand_id_number
F
Freescale Semiconductor, Inc.
n
.