SPRUI04F july 2015 – april 2023
Global and static data objects can be accessed in the following two ways:
__near keyword | The compiler assumes that the data item can be accessed relative to the data page pointer. For example:
|
__far keyword | The compiler cannot access the data item via the DP. This can be required if the total amount of program data is larger than the offset allowed (32K) from the DP. For example:
|
Be consistent with near and far declarations. If an object is defined to be far, all external declarations of this object in other C files or headers must also contain the __far keyword, or you will likely get compiler or linker errors. If an object is defined to be near, you can safely declare it as __far in other C files or headers, but you will have slower data access for that variable.
If you use the DATA_SECTION pragma, the object is indicated as a far variable, and this cannot be overridden. If you reference this object in another file, then you need to use extern __far when declaring this object in the other source file. This ensures access to the variable, since the variable might not be in the .bss section. For details, see Section 7.9.6.
Defining Global Variables in Assembly Code
If you also define a global variable in assembly code with the .usect directive (where the variable is not assigned in the .bss section) or you allocate a variable into separate section using a #pragma DATA_SECTION directive; and you want to reference that variable in C code, you must declare the variable as extern __far. This ensures the compiler does not try to generate an illegal access of the variable by way of the data page pointer.
When data objects do not have the __near or __far keyword specified, the compiler will use far accesses to aggregate data and near accesses to non-aggregate data. For more information on the data memory model and ways to control accesses to data, see Section 8.1.4.1.