MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
16-Bit C <strong>Compiler</strong> User’s Guide<br />
EXAMPLE 9-1: MIXING C AND ASSEMBLY<br />
/*<br />
** file: ex1.c<br />
*/<br />
extern unsigned int asmVariable;<br />
extern void asmFunction(void);<br />
unsigned int cVariable;<br />
void foo(void)<br />
{<br />
asmFunction();<br />
asmVariable = 0x1234;<br />
}<br />
The file ex2.s defines asmFunction <strong>and</strong> asmVariable as required <strong>for</strong> use in a<br />
linked application. The assembly file also shows how to call a C function, foo, <strong>and</strong> how<br />
to access a C defined variable, cVariable.<br />
;<br />
; file: ex2.s<br />
;<br />
.text<br />
.global _asmFunction<br />
_asmFunction:<br />
mov #0,w0<br />
mov w0,_cVariable<br />
return<br />
.global _begin<br />
_main:<br />
call _foo<br />
return<br />
.bss<br />
.global _asmVariable<br />
.align 2<br />
_asmVariable: .space 2<br />
.end<br />
In the C file, ex1.c, external references to symbols declared in an assembly file are<br />
declared using the st<strong>and</strong>ard extern keyword; note that asmFunction, or<br />
_asmFunction in the assembly source, is a void function <strong>and</strong> is declared<br />
accordingly.<br />
In the assembly file, ex1.s, the symbols _asmFunction, _main <strong>and</strong> _asmVariable<br />
are made globally visible through the use of the .global assembler directive <strong>and</strong> can<br />
be accessed by any other source file. The symbol _main is only referenced <strong>and</strong> not<br />
declared; there<strong>for</strong>e, the assembler takes this to be an external reference.<br />
The following compiler example shows how to call an assembly function with two<br />
parameters. The C function main in call1.c calls the asmFunction in call2.s<br />
with two parameters.<br />
DS51284H-page 120 © 2008 <strong>Microchip</strong> Technology Inc.