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 54
 2012 Microchip Technology Inc.
3.4.7.2
WHAT DO I NEED OTHER THAN INSTRUCTIONS IN AN ASSEMBLY 
SOURCE FILE?
Assembly code typically needs assembler directives as well as the instructions them-
selves. The operation of all the directives are described in the subsections of 
Section 6.4.9 “Assembler Directives”. Common directives required are mentioned 
below.
All assembly code must be placed in a psect so it can be manipulated as a whole by 
the linker and placed in memory. SeSection 5.15.1 “Program Sections” for general 
information on psects; see Section 6.4.9.3 “PSECT” for information on the directive 
used to create and specify psects.
The other commonly used directive is GLOBAL, defined in Section 6.4.9.1 “GLOBAL” 
which is used to make symbols accessible across multiple source files.
3.4.7.3
HOW DO I ACCESS C OBJECTS FROM ASSEMBLY CODE?
Most C objects are accessible from assembly code. There is a mapping between the 
symbols used in the C source and those used in the assembly code generated from 
this source. Your assembly should access the assembly-equivalent symbols which are 
detailed in Section 5.12.3 “Interaction Between Assembly and C Code”.
Instruct the assembler that the symbol is defined elsewhere by using the GLOBAL 
assembler directive, seSection 6.4.9.1 “GLOBAL”. This is the assembly equivalent 
of a C declaration, although no type information is present. This directive is not needed 
and should not be used if the symbol is defined in the same module as your assembly 
code.
Any C variable accessed from assembly code will be treated as if it were qualified vol-
atile
, see Section 5.4.7.2 “Volatile Type Qualifier”. Specifically specifying the 
volatile
 qualifier in C code is preferred as it makes it clear that external code may 
access the object.
3.4.7.4
HOW CAN I ACCESS SFRS FROM WITHIN ASSEMBLY CODE?
The safest way to gain access to SFRs in assembly code is to have symbols defined 
in your assembly code that equate to the corresponding SFR address. Header files are 
provided with the compiler so that you do not need to define these yourselves, and they 
are detailed in Section 5.12.3.2 “Accessing Registers from Assembly Code”.
There is no guarantee that you will be able to access symbols generated by the com-
pilation of C code, even code that accesses the SFR you require.
3.4.7.5
WHAT THINGS MUST I MANAGE WHEN WRITING ASSEMBLY CODE?
If you are hand-writing assembly code there are several things that you must take con-
trol of.
• Whenever accessing a RAM variable, you must ensure that the bank of the vari-
able is selected before you read or write the location. This is done by one or more 
assembly instructions. The exact code is based on the device you are using and 
the location of the variable. Bank selection is not be required if the object is in 
common memory, (which is called the access bank on PIC18 devices) or if you 
are using an instruction that takes a full address (such as the MOVFF instruction on 
PIC18 devices). Check your device data sheet to see the memory architecture of 
your device, and the instructions and registers which control bank selection. Fail-
ure to select the correct bank will lead to code failure.
The BANKSEL pseudo instruction can be used to simplify this process, see 
Section 6.4.1.2 “Bank and Page Selection”.