Runtime
Environment
-
Interfacing
C
with
Assembly
Language
sembler directive. This defines the symbol
as
external and allows the
linker
to
resolve references
to
it.
Similarly,
to
access a C
function
or object from assembly, declare the C
object
with
.global, thus creating
an
undefined external reference
that
the linker must resolve.
5.4.1.1
An
Example
of
an
Assembly
Language
Function
Example 5-1 illustrates a C
function
called
main
which
calls
an
assembly
language
function
called
aSIn-func.
The
aSIn-func
function has one argu-
ment
which
is a pointer
to
an
integer.
aSIn-func
calls another C
function
called
c-func
with
one argument
which
is
a global variable named
gvar.
aSIn-func
takes the value returned from
c_func
and stores
it
in the integer
pointed
to
by its single argument.
Example
5-1.
An
Assembly
Language
Function
extern
int
aSIn-func();
/*
declare
external
asm
function
*/
int
gvar;
/*
define
global
variable
*/
main()
{
int
i,
j;
i =
aSIn-func(&i);
FP
STK
.set
.set
.global
.
global
.
global
-asIn-func:
MMTM
MOVE
MOVE
MOVE
MOVE
CALLA
MOVE
MOVE
MMFM
RETS
(a)
C
Program
A13
A14
-gvar
-c_func
-asIn-func
SP,A7,FP
STK,FP
*FP(-32),A7,1
STK
-*SP
1
@-g~ar,*STK+,l
-c_func
A8,*A7,1
*SP(96),STK,1
SP,A7,FP
2
frame
pointer
program
stack
pointer
declare
global
variable
declare
C
function
declare
this
function
save
registers
on
SP
set
up
FP
get
argument
function
call:
save
STK
push
argument
call
function
result
in
A8
restore
caller's
STK
(pop
arguments)
restore
saved
registers
return
&
pop
caller's
STK
(b)
Assembly
Language
Program
In the C program in Example
5-1,
the extern declaration
of
asmfunc
is
op-
tional, since the
function
returns
an
int. Like C functions, assembly
functions
need
only
be declared
if
they return non-integers.
In the assembly language code in Example
5-1,
note the underscores on all
the C symbol names when used in the assembly code.
5-13