07.04.2013 Views

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

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>MPLAB</strong> ® C COMPILER FOR<br />

<strong>PIC24</strong> <strong>MCUs</strong> AND <strong>dsPIC</strong> ® <strong>DSCs</strong><br />

USER’S GUIDE<br />

Chapter 9. Mixing Assembly Language <strong>and</strong> C Modules<br />

9.1 INTRODUCTION<br />

9.2 HIGHLIGHTS<br />

This section describes how to use assembly language <strong>and</strong> C modules together. It gives<br />

examples of using C variables <strong>and</strong> functions in assembly code <strong>and</strong> examples of using<br />

assembly language variables <strong>and</strong> functions in C.<br />

Items discussed in this chapter are:<br />

• Mixing Assembly Language <strong>and</strong> C Variables <strong>and</strong> Functions – Separate<br />

assembly language modules may be assembled, then linked with compiled C<br />

modules.<br />

• Using Inline Assembly Language – Assembly language instructions may be<br />

embedded directly into the C code. The inline assembler supports both simple<br />

(non-parameterized) assembly language statement, as well as extended<br />

(parameterized) statements, where C variables can be accessed as oper<strong>and</strong>s of<br />

an assembler instruction.<br />

9.3 MIXING ASSEMBLY LANGUAGE AND C VARIABLES AND FUNCTIONS<br />

The following guidelines indicate how to interface separate assembly language<br />

modules with C modules.<br />

• Follow the register conventions described in Section 4.12 “Register<br />

Conventions”. In particular, registers W0-W7 are used <strong>for</strong> parameter passing. An<br />

assembly language function will receive parameters, <strong>and</strong> should pass arguments<br />

to called functions, in these registers.<br />

• Functions not called during interrupt h<strong>and</strong>ling must preserve registers W8-W15.<br />

That is, the values in these registers must be saved be<strong>for</strong>e they are modified <strong>and</strong><br />

restored be<strong>for</strong>e returning to the calling function. Registers W0-W7 may be used<br />

without restoring their values.<br />

• Interrupt functions must preserve all registers. Unlike a normal function call, an<br />

interrupt may occur at any point during the execution of a program. When returning<br />

to the normal program, all registers must be as they were be<strong>for</strong>e the interrupt<br />

occurred.<br />

• Variables or functions declared within a separate assembly file that will be<br />

referenced by any C source file should be declared as global using the assembler<br />

directive.global. External symbols should be preceded by at least one<br />

underscore. The C function main is named _main in assembly <strong>and</strong> conversely an<br />

assembly symbol _do_something will be referenced in C as do_something.<br />

Undeclared symbols used in assembly files will be treated as externally defined.<br />

The following example shows how to use variables <strong>and</strong> functions in both assembly<br />

language <strong>and</strong> C regardless of where they were originally defined.<br />

The file ex1.c defines foo <strong>and</strong> cVariable to be used in the assembly language file.<br />

The C file also shows how to call an assembly function, asmFunction, <strong>and</strong> how to<br />

access the assembly defined variable, asmVariable.<br />

© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 119

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!