
Out-of-line Assembly Code
Motorola
Mixing C and Assembly Language
For More Information On This Product,
Go to: www.freescale.com
5-33
5.4.2 Global C and Static Variables in C
The global C variables are accessed using labels generated by the C compiler. Any
variables that are static to an assembly language subroutine will be accessed the same
way. These variables are placed into memory at compile-time and are referenced
symbolically according to the labels automatically generated by the compiler. However, it
is possible to override the default labels by using the
_ _asm()
keyword as explained in
Section 5.2.5, "Controlling Labels Generated by the Compiler,"
.
For example, using the default labeling convention, the global integer,
Ginteger
which
can be declared within the C statement “extern int Ginteger;” is loaded into the input
register x0 in assembly code as follows:
move
y:FGinteger,x0
When declaring C global variables in an assembly language file, the programmer must be
careful to follow the label generating convention or use the
_ _
asm() keyword to report to
the compiler that the labeling convention has been changed. In both cases, the assembler
directive global is used to export the labels to the C files. DO NOT use the XDEF/XREF
pair of directives. NOTE that it is the programmers responsibility to
allocate
space for the
global variables declared in this manner. In the example below, this is done with the
assembler directive dc. Also, ANSI C requires that all global variables be initialized to
zero if they are not explicitly initialized.
Example 5-32. Global Label in Assembly Language
Global Label in Assembly Language. This example shows assembly code that defines a
global integer (named FGinteger) which is normally accessed as Ginteger in the C
environment and FGinteger in the assembly programming environment.
FGinteger
globalFGinteger
dc
$0
Example 5-33. Global Variable Declaration
Global Variable Declaration. This is the C code equivalent to Section which defines the
global integer Ginteger.
extern
int
Ginteger;
F
Freescale Semiconductor, Inc.
n
.