13.2 Changing Register Contents

The assembly generated from C source code by the compiler will use certain registers that are present on the DSC device. Most importantly, the compiler assumes that nothing other than code it generates can alter the contents of these registers. So if the assembly loads a register with a value and no subsequent code generation requires this register, the compiler will assume that the contents of the register are still valid later in the output sequence.

The registers that are special and which are managed by the compiler are: W0-W15, F0-F31 (for dsPIC33A), RCOUNT, SR, PSVPAG or DSRPAG (for dsPIC33C/E/Fand dsPIC30F), and SPLIM. If fixed point support is enabled, the compiler may allocate A or B and may adjust the value in CORCON.

The state of these register must never be changed directly by C code, or by any assembly code in-line with C code. The following example shows a C statement and in-line assembly that violates these rules and changes the ZERO bit in the STATUS register.

#include <xc.h>
void badCode(void)
{
  asm (“mov #0, w8”);
  WREG9 = 0; /* Working registers are not memory mapped for dsPIC33A devices. */
}

The compiler is unable to interpret the meaning of in-line assembly code that is encountered in C code. Nor does it associate a variable mapped over an SFR to the actual register itself. Writing to an SFR register using either of these two methods will not flag the register as having changed and may lead to code failure.