![Texas Instruments TMS320C2x User Manual Download Page 144](http://html1.mh-extra.com/html/texas-instruments/tms320c2x/tms320c2x_user-manual_1097073144.webp)
Interfacing C with Assembly Language
6-21
Run-Time Environment
2 illustrates a C function called main, which calls an assembly
language function called asmfunc. The asmfunc function takes its single argu-
ment, adds it to the C global variable called gvar, and returns the result.
Example 6
−
2. An Assembly Language Function
(a) C program
extern
int
asmfunc(); /* declare external asm function */
int gvar; /* define global variable */
main()
{
int i;
i = asmfunc(i); /* call function normally */
}
(b) Assembly language program
_asmfunc:
POPD *+ ; Move return address to C stack
SAR AR0, *+ ; Save FP
SAR AR1, * ; Save SP
LARK AR0, 1 ; Size of frame
LAR AR0, *0+, AR2 ; Set up FP and SP
LDPK _gvar ; Point to gvar
SSXM ; Set sign extension
LAC _gvar ; Load gvar
LARK AR2, −3 ; Offset of argument
MAR *0+ ; Point to argument
ADD *, AR0 ; Add arg to gvar
SACL _gvar ; Save in gvar
LARP AR1 ; Pop off frame
SBRK 2
LAR AR0, * ; Restore frame pointer
PSHD * ; Move return addr to C2x stack
RET
2, the extern declaration of asmfunc is
optional, since the return type is int. Like C functions, you need to declare
assembly functions only if they return noninteger values or pass noninteger
parameters.
Further, in Example 6
2 it is not necessary to move the return address from
the hardware stack to the software stack, because asmfunc makes no calls.
The code is in the example to illustrate how it is accomplished.
Summary of Contents for TMS320C2x
Page 8: ...viii...
Page 69: ...2 47 C Compiler Description...
Page 159: ...6 36...
Page 226: ...8 6...