Invoking Assembly Function in ANSI-C Source File

An function implemented in an assembly source file ( mixasm.asm in the following listing) can be invoked in a C source file ( Listing: Example C source code file: mixc.c). During the implementation of the function in the assembly source file, you should pay attention to the parameter passing scheme of the ANSI-C compiler you are using in order to retrieve the parameter from the right place.

Listing: Example of an assembly file: mixasm.asm

         XREF CData
         XDEF AddVar

         XDEF ASMData

DataSec: SECTION

ASMData: DS.B 1

CodeSec: SECTION

AddVar:

         ADD D2, CData ; add CData to the parameter in register A

         ST D2, ASMData ; result of the addition in ASMData

         RTS

We recommend that you generate a header file for each assembly source file, as listed in the above listing. This header file ( mixasm.h in the following listing) should contain the interface to the assembly module.

Listing: Header file for the assembly mixasm.asm file: mixasm.h

/* mixasm.h */
#ifndef _MIXASM_H_

#define _MIXASM_H_

void AddVar(unsigned char value);

/* function that adds the parameter value to global CData */

/* and then stores the result in ASMData */

/* variable which receives the result of AddVar */

extern char ASMData;

#endif /* _MIXASM_H_ */

The function can then be invoked in the usual way, using its name.