Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
MPLAB
®
 XC8 C Compiler User’s Guide
DS52053B-page 200
 2012 Microchip Technology Inc.
5.12
MIXING C AND ASSEMBLY CODE
Assembly language code can be mixed with C code using two different techniques: 
writing assembly code and placing it into a separate assembler module, or including it 
as inline assembly in a C module.
5.12.1
Integrating Assembly Language Modules 
Entire functions may be coded in assembly language as separate .as or .asm source 
files included into your project. They will be assembled and combined into the output 
image using the linker.
By default, such modules are not optimized by the assembler optimizer. Optimization 
can be enabled by using the --OPT option, see Section 4.8.42 “--OPT: Invoke Com-
piler Optimizations”
.
The following are guidelines that must be adhered to when writing a C-callable assem-
bly routine.
• Select, or define, a suitable psect for the executable assembly code (See 
Section 5.15.1 “Program Sections” for an introductory guide to these.)
• Select a name (label) for the routine using a leading underscore character
• Ensure that the routine’s label is globally accessible from other modules
• Select an appropriate C-equivalent prototype for the routine on which argument 
passing can be modelled
• Limit arguments and return values to single byte-sized objects (Assembly routines 
may not define variables that reside in the compiled stack. Use global variables 
for additional arguments.)
• Optionally, use a signature value to enable type checking when the function is 
called
• Use bank selection instructions and mask addresses of any variable symbols
The following example goes through these steps for a mid-range device. The process 
is the same for other devices. A mapping is performed on the names of all C functions 
and non-static global variables. See Section 5.12.3 “Interaction Between 
Assembly and C Code”
 for a complete description of mappings between C and 
assembly identifiers.
An assembly routine is required which can add an 8-bit quantity passed to the routine 
with the contents of PORTB and return this as an 8-bit quantity.
Most compiler-generated executable code is placed in psects called textn, where n is 
a number. (see Section 5.15.2 “Compiler-Generated Psects”). We will create our 
own text psect based on the psect the compiler uses. Check the assembly list file to 
see how the text psects normally appear for assembly generated from C code. You may 
see a psect, such as the following, generated by the code generator when compiling 
for baseline or mid-range devices.
PSECT text0,local,class=CODE,delta=2
Note:
The more assembly code a project contains, the more difficult and time con-
suming will be its maintenance. As the project is developed, the compiler 
may perform different optimizations a these are based on the entire pro-
gram. Assembly code may need revision if the compiler is updated due to 
differences in the way the updated compiler may work. These factors do not 
affect code written in C.
If assembly must be added, it is preferable to write this as a self-contained 
routine in a separate assembly module, rather than in-lining it in C code.