4.6 Code Directive

The MPASM CODE directive starts a section containing program code.

Suggested Replacement

Use the predefined code psect, or a create a similar psect, ensuring the flags are suitable for a section containing executable code on the target device. If the psects must be padded to a multiple of a certain size, use the ALIGN directive after the code you place inside the psect.

All program code must be placed in a psect (or section) using the PSECT directive. The PIC Assembler provides the code psect once you include <xc.inc>. You can use this psect without having to specify any psect flags, for example:
PSECT code
;place code for any device here

Alternatively, you can define your own psect with any name and suitable psect flags. The psect's space flag must be 0, to indicate that the psect should be positioned in program memory; however, this is the default value. Typically, you would use the class flag to assign the psect to the CODE linker class, which is also predefined by the driver, so that the psect will be positioned somewhere in the memory associated with this class without you having to specify any linker options. You could also position this psect at a particular address using the linker's -p option, passed to the linker from the driver's -Wl option.

For PIC18 devices, set the psect's reloc flag to be 2, to indicate that the psect contents must be aligned on even addresses. Use the default delta value of 1. For example:
PSECT myText,reloc=2,class=CODE
;PIC18 code goes here

For all other devices, use the default reloc flag value of 1, but set the delta value to be 2, to indicate that the device uses (16-bit) word addressable program memory. For example:

PSECT myText,delta=2,class=CODE
;Baseline/Mid-range code goes here
You can add more content to the same psect later in the source file by using the PSECT directive with the psect's name, but you do not need to repeat the psect flags. You could, for example, concatenate more content to the psects defined above by using:
PSECT myText
;more content goes here