10.5 Variable in Registers

Allocating variables to registers, rather than to a memory location, can make code more efficient. With MPLAB XC32 C/C++ Compiler, variables may be allocated to registers as part of code optimizations. For optimization levels 1 and higher, the values assigned to variables may cached in a register. During this time, the memory location associated with the variable may not hold a valid value.

The register keyword may be used to indicate your preference for the variable to be allocated a register, but this is just a recommendation and may not be honored. The specific register may be indicated as well, but this is not recommended as your register choice may conflict with the needs of the compiler. Using a specific register in your code may cause the compiler to generate less efficient code.

As indicated in 16.6 Function Parameters, parameters may be passed to a function via a register.

Variables in Registers

volatile unsigned int special;
unsigned int example (void)
{
  register unsigned int my_reg __asm__("$4");
  my_reg += special;
  return my_reg;
}