9.5 Variables 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, a value assigned to a variable may be stored in a register. During this time, the memory location associated with the variable may not hold the live 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 14.2 Register Conventions, 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__("r0");
  my_reg += special;
  return my_reg;
}