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

Product codes
SW006021-1
Page of 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 175
5.5.4
Absolute Variables
Most variables can be located at an absolute address by following its declaration with 
the construct @ address, where address is the location in memory where the 
variable is to be positioned. Such a variables is known as an absolute variables.
5.5.4.1
ABSOLUTE VARIABLES IN DATA MEMORY
Absolute variables are primarily intended for equating the address of a C identifier with 
a special function register, but can be used to place ordinary variables at an absolute 
address in data memory.
For example:
volatile unsigned char Portvar @ 0x06;
will declare a variable called Portvar located at 06h in the data memory. The compiler 
will reserve storage for this object (if the address falls into general-purpose RAM) and 
will equate the variable’s identifier to that address.
The auto variables cannot be made absolute as they are located in a compiled stack. 
SeSection 5.5.2.2.1 “Compiled Stack Operation”. The compiler does not make 
any checks for overlap of absolute variables with other absolute variables, so this must 
be considered when choosing the variable locations. There is no harm in defining more 
than one absolute variable to live at the same address if this is what you require. The 
compiler will not locate ordinary variables over the top of absolutes, so there is no over-
lap between these objects. 
When compiling for an enhanced mid-range PIC device, the memory allocated for 
some objects may be spread over multiple RAM banks. Such objects will only ever be 
accessed indirectly in assembly code, and will use the linear GPR memory imple-
mented on these devices. A linear address (which can be mapped back to the ordinary 
banked address) will be used with these objects internally by the compiler.
The address specified for absolute objects on these devices may either be the tradi-
tional banked memory address or the linear address. As the linear addresses start 
above the largest banked address, it is clear which address is intended. In the following 
example:
int inputBuffer[100] @ 0x2000;
it is clear that inputBuffer should placed at address 0x2000 in the linear address 
space, which is address 0x20 in bank 0 RAM in the traditional banked address space. 
See the device data sheet for exact details regarding your selected device.
Absolute variables in RAM cannot be initialized when they are defined. Define the 
absolute variables, then assign them a value at a suitable point in your main-line code.
Note:
Defining absolute objects can fragment memory and may make it impossi-
ble for the linker to position other objects. Avoid absolute objects if at all 
possible. If absolute objects must be defined, try to place them at either end 
of a memory bank or page so that the remaining free memory is not frag-
mented into smaller chunks.