Example 6−2. An Assembly Language Function
Example 6−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.
(a) C program
extern int asmfunc(); /* declare external asm function
int gvar;
main()
{
int i;
i = asmfunc(i);
}
(b) Assembly language program
_asmfunc:
POPD
*+
SAR
AR0, *+
SAR
AR1, *
LARK AR0, 1
LAR
AR0, *0+, AR2 ; Set up FP and SP
LDPK _gvar
SSXM
LAC
_gvar
LARK AR2, −3
MAR
*0+
ADD
*, AR0
SACL _gvar
LARP AR1
SBRK 2
LAR
AR0, *
PSHD *
RET
In the C program in Example 6−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.
Interfacing C with Assembly Language
/* define global variable
/* call function normally
; Move return address to C stack
; Save FP
; Save SP
; Size of frame
; Point to gvar
; Set sign extension
; Load gvar
; Offset of argument
; Point to argument
; Add arg to gvar
; Save in gvar
; Pop off frame
; Restore frame pointer
; Move return addr to C2x stack
Run-Time Environment
*/
*/
*/
6-21
Need help?
Do you have a question about the TMS320C2x and is the answer not in the manual?
Questions and answers