With the MPLAB® XC8 compiler, making an object absolute is the easiest way of specifying a link location. However you are limited to the object being at a known address. Using psects is more complex, but does allow you to use the features of the linker to allocate memory for the object. You might, for example have the linker place the object anywhere in an address range, or anywhere provided it is above a specific address or after some other object.
In either case consider whether you need to do this as it immediately creates non-portable code. If you must place objects at a known location, try to place the object at a sensible location. If it is located in the middle of a bank or page, this will severely hamper the ability of the linker to position other objects and "can't find space" errors may result.
These are the various ways the functions and variables can be stored at an absolute address:
- Using __section specifier with CCI syntax enabled
- Using __at specifier with CCI syntax enabled
- Absolute variables in Data Space using '@' qualifier:
- Absolute variables in Program Space using '@' qualifier:
- Absolute functions using '@' qualifier:
1. Using __section specifier with CCI syntax enabled
To use the __section specifier make sure that you have enabled the CCI syntax under MPLAB X IDE:
Project Properties » MPLAB XC8 compiler » Preprocessing and messages » Use CCI Syntax = Checked
Variable “commonFlag” will be placed in section “tomSec”.
Function “mul” will be placed in section “mytext”.
With XC8, the __section() specifier cannot be used with the interrupt functions.
2. Using __at specifier with CCI syntax enabled
The following shows usage of __at specifier.
3. Absolute variables in Data Space using '@' qualifier
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.
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 fragmented into smaller chunks.
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.
Trying to initialize the absolute variables in RAM will throw a warning as:
initialization of absolute variable "x" in RAM is not supported
4. Absolute variables in Program Space using '@' qualifier
@ symbol will place ‘marker’ at 0x200 and the array ‘table’ at address 0x100 in the program space. Both initialized and uninitialized const objects can be made absolute.
5. Absolute functions using '@' qualifier
The function readInput is placed at 0x200 in Program Space.
Always check either the assembly list or map file to ensure that the object has been located as you expect.