Runtime
Environment
-
Interfacing
C
with
Assembly
Language
5.4.1.2
Defining
Variables
in
Assembly
Language
It
is
sometimes useful for a C program to access variables that
are
defined in
assembly language. There
are
several methods for defining a variable in as-
sembly language; accessing the variable
is
straightforward:
In
Assembly
Language:
1 ) Define the variable:
a) Use the .bss directive
to
define the variable
in
the .bss section.
Ol
b) Define the variable in a named, initialized section (.sect).
or
c) Define the variable in a named, uninitialized section (.usect).
2)
Use
the .global directive
to
make the definition external.
3) Remember
to
precede the variable name
with
an
underscore.
In
C:
4)
Declare the variable
as
extern, and access
it
normally.
The C compiler
uses
the first method by defining variables
in
the .bss section.
Example
5-2
a shows examples that
use
these three methods
to
define vari-
ables. Example
5-2
b shows
how
you
can
use
C code
to
access the first var-
iable defined in Example
5-2
a; you
can
access the other variables similarly.
Example
5-2.
Accessing
an
Assembly-Language
Variable
from
C
5-14
**
Method
1:
**
Define
variable
var
in
the
.bss
section
**
.bss
-var,
32
.
global
-var
Define
the
variable
Declare
it
as
external
**
Method
2:
**
Define
variable
table
in
a
named,
initialized
section
**
-table
.sect
.word
.word
.word
"more_vars"
01234h
05678h
09ABCh
-table
Define
the
variable
.
global
Declare
it
as
external
**
Method
2:
**
Define
variable
buffer
in
a
named,
uninitialized
**
section
**
-buffer
.usect
"ramvars"
,
32
.
global
-buffer
Declare
the
variable
Declare
it
as
external
(a)
Assembly
Language
Program
/*
This
examples
shows
you
can
access
the
variable
*/
/*
named
var,
which
was
defined
above;
you
can
*/
/*
access
the
other
variables
similarly.
*/
extern
int
var;
var
=
1;
/*
External
variable
*/
/*
Use
the
variable
*/
(b)
C
Program