Segmentation

The Linker memory space may be partitioned into several segments. The Compiler allows attributing a certain segment name to certain global variables or functions, which the Linker then allocates into that segment. An entry in the Linker parameter file determines where that segment actually lies.

Three pragmas specify code segments and data segments:


#pragma DATA_SEG [__SHORT_SEG] <name>

#pragma CONST_SEG [__LINEAR_SEG] <name>

#pragma STRING_SEG [__LINEAR_SEG] <name>

All remain valid until the Compiler encounters the next pragma of the same kind. Unless you specify different segments, the Compiler assumes two default segments named DEFAULT_ROM (the default code segment) and DEFAULT_RAM (the default data segment). To explicitly set these default segments as the current segments, use the segment name DEFAULT ():

Listing: Explicit Default Segments
#pragma CODE_SEG DEFAULT
#pragma DATA_SEG DEFAULT

The additional keyword __SHORT_SEG informs the Compiler that a data segment is allocated in the zero page (address range from 0x0000 to 0x00FF):

#pragma DATA_SEG __SHORT_SEG <segment_name>

or

#pragma DATA_SEG __SHORT_SEG DEFAULT

Using the zero page enables the Compiler to generate much denser code because it uses DIRECT addressing mode instead of EXTENDED.

Note: It is the programmer's responsibility to actually allocate __SHORT_SEG segments in the zero page in the Linker parameter file. For more information, see the Linker section of the Build Tools manual.