3.5 Predefined Psects
All code, data objects, or anything that is to be linked into the device memory must be placed in a psect.
Psects—short for program sections—are containers that group and hold related parts of the program, even though the source code for these parts might not be physically adjacent in the source file, or may even be spread over several modules. They are the smallest entities positioned by the linker into memory.
To place code or objects into a psect, precede their definition with a
PSECT
directive and the name of the psect you wish to use. The
first time a psect is used in a module, you must define the psect flags that are
relevant for the psect. These flags control which memory space the psect will reside in
and further refine how the psect is linked.
To assist with code migration and development, several psects are predefined
with appropriate flags by the PIC Assembler and are available once you include the
<xc.inc>
file into your source. A full list of these psects
can be found in the MPLAB® XC8 PIC® Assembler User's Guide.
<xc.inc>
include file to be able to use the predefined psects
supplied by the assembler.code
psect was used to hold
most of the program's code. You can see it being specified in the
lines:PSECT code
main:
;set up the oscillator
movlw 0x62
...
thereby
placing the main
label and the instructions that follow into the
code
psect. The udata_acs
psect has been used to
place the max
and tmp
labels and the reserved memory
they are associated with in the PIC18 Access Bank memory, as shown in the
lines:PSECT udata_acs
max:
DS 1 ;reserve 1 byte for max
tmp:
DS 1 ;1 byte for tmp
The configuration data also needs to be inside a psect so that it will appear in the HEX
file at the appropriate address; however, the CONFIG
directives will
automatically ensure that their output is in a psect that will be linked to the required
location, so you should not precede them with a PSECT
directive in your
code. You can see where the configuration data is linked in the map file, which is
illustrated in section Building the Example.
One advantage of using predefined psects is that they are already associated with a suitable linker class, so they will be automatically linked into an appropriate region of memory without you having to stipulate any linker options when you build your project.