
In-line Assembly Code
Motorola
Mixing C and Assembly Language
For More Information On This Product,
Go to: www.freescale.com
5-15
Example 5-17. Read-Write Operand
Read-Write Operand: The same result will be obtained as in Example 5-16. Notice how
the operand id is changed according to the placement of the C variables.
int foo, result;
_ _
asm(“add %2,%0” : “=D” (result) : “0” (result), “S”
(foo));
Example 5-18. Multiple Instruction — Single-Line
Multiple Instruction — Single-Line: An in-line assembly program which places a value
(e.g. $709) in register a and negates the result is written in one line. This one line will
generate two lines of assembly code in the C compiler output.
_ _
asm(“move #$709,A\n neg A”);
Example 5-19. Multiple Instruction — Multiple-Line
Multiple Instruction — Multiple-Line: The two lines of in-line assembly code in this
example have the same effect as the one line in Section 5-18. Notice that using two lines
increases the in-line assembly code readability. The line continuation character ‘\’ used at
the end of the in-line assembly codes first line makes this possible.
_ _
asm(“move #$709,A\n\
neg A”);
Example 5-20. Mulitple Use of _ _asm()
Multiple use of
_ _asm()
. This example and Example 5-21 are done in-line with the
compiler performing all register allocation and all operands are referenced via C
expressions.This in-line program is a single memory space version of the Dr. BuB IIR
filter program iir1.asm. The method used to write this in-line assembly program is to use
an
_ _asm()
statement for each assembly language instruction.
int iir1( int data, int* sptr, int* cptr )
{
int state1, state2, coef;
_ _
asm ( “move y:(%1)+,%0” : “=R” (state1) : “A” (sptr)
);
_ _
asm ( “move y:(%1)+,%0” : “=R” (coef) : “A” (cptr)
);
_ _
asm ( “mac
%2,%3,%0\ty:(%4)-,%1”
: “=D” (data), “=R” (coef)
: “R” (state1), “1” (coef), “A” (cptr), “0” (data) );
_ _
asm ( “move y:(%1),%0” : “=S” (state2) : “A” (sptr)
);
_ _
asm ( “macr %1,%2,%0\t%3,y:(%4)-”
F
Freescale Semiconductor, Inc.
n
.