Vault 7: Projects

This publication series is about specific projects related to the Vault 7 main publication.

MPLAB
®
XC16 C Compiler Users Guide
DS50002071C-page 126 2012-2013 Microchip Technology Inc.
will assign the pointer with the address of the string "hello world ".
8.10 STANDARD TYPE QUALIFIERS
Type qualifiers provide additional information regarding how an object may be used.
The MPLAB XC16 compiler supports both ANSI C qualifiers and additional special
qualifiers which are useful for embedded applications and which take advantage of the
PIC MCU and dsPIC DSC architectures.
8.10.1 Const Type Qualifier
The compiler supports the use of the ANSI type qualifiers const and volatile.
The const type qualifier is used to tell the compiler that an object is read only and will
not be modified. If any attempt is made to modify an object declared const, the
compiler will issue a warning or error.
User-defined objects declared const are placed, by default, in the program space and
may be accessed via the program visibility space, see Section 10.4 Variables in Pro-
gram Space. Usually a const object must be initialized when it is declared, as it
cannot be assigned a value at any point at runtime. For example:
const int version = 3;
will define version as being an int variable that will be placed in the program mem-
ory, will always contain the value 3, and which can never be modified by the program.
The memory model -mconst-in-data will allocate const-qualified objects in data
space, which may be writable.
8.10.2 Volatile Type Qualifier
The volatile type qualifier is used to tell the compiler that an object cannot be guar-
anteed to retain its value between successive accesses. This prevents the optimizer
from eliminating apparently redundant references to objects declared volatile
because it may alter the behavior of the program to do so.
Any SFR which can be modified by hardware or which drives hardware is qualified as
volatile, and any variables which may be modified by interrupt routines should use
this qualifier as well. For example:
extern volatile unsigned int INTCON1 __attribute__((__sfr__));
The code produced by the compiler to access volatile objects may be different to
that to access ordinary variables, and typically the code will be longer and slower for
volatile objects, so only use this qualifier if it is necessary. However failure to use
this qualifier when it is required may lead to code failure.
Another use of the volatile keyword is to prevent variables being removed if they
are not used in the C source. If a non-volatile variable is never used, or used in a
way that has no effect on the programs function, then it may be removed before code
is generated by the compiler.
A C statement that consists only of a volatile variables name will produce code that
reads the variables memory location and discards the result. For example the entire
statement:
PORTB;
will produce assembly code the reads PORTB, but does nothing with this value. This is
useful for some peripheral registers that require reading to reset the state of interrupt
flags. Normally such a statement is not encoded as it has no effect.
Some variables are treated as being volatile even though they may not be qualified
in the source code. See Chapter 16. Mixing C and Assembly Code if you have
assembly code in your project.

e-Highlighter

Click to send permalink to address bar, or right-click to copy permalink.

Un-highlight all Un-highlight selectionu Highlight selectionh