5.4.3 Objects in Program Space

Variables with static storage duration and that are qualified const are placed into program memory. Some auto objects might also be positioned there, as discussed in 5.1.2.4 Const Auto Objects. Note that some optimizations (see 4.6.6.8 Cacheconst Option) attempt to cache const-qualified objects into data memory, where they are accessed more efficiently.

Accessing data located in program memory is much slower than accessing objects in the data memory. The code associated with the access is also larger.

Enhanced Mid-range devices can directly read their program memory, although the compiler will still usually store data as retlw instructions. This way the compiler can either produce code that can call these instructions to obtain the program memory data as with the ordinary Mid-range devices, or directly read the operand to the instruction (the LSB of the retlw instruction). The most efficient access method can be selected by the compiler when the data needs to be read.

Data can be stored as individual bytes in the program memory of PIC18 devices. This can be read using table read instructions.

Note: On some devices, it is possible to read-protect some blocks of program memory by enabling bits within a Configuration Word, such as the EBTRx bits on some PIC18 devices. If this protection feature is enabled, code might fail to read objects stored in the blocks of program memory that have been protected. Ensure that program-memory sections holding data are not unintentionally linked into protected program memory blocks.

For other 8-bit PIC devices, the program space is not directly readable by the device. For these devices, the compiler stores data in the program memory by means of retlw instructions which can be called and will return a byte of data in the W register. The compiler will generate the code necessary to make it appear that program memory is being read directly.