| |||
| Home > Compiler Features > Named register variables | |||
The compiler enables you to access registers of an ARM architecture-based processor using named register variables.
Named register variables are declared by combining the register keyword
with the __asm keyword. The __asm keyword
takes one parameter, a character string, that names the register.
For example, the declaration:
register int R0 __asm("r0");
declares R0 as a named register variable
for the register r0. See Named
register variables in the Compiler
Reference Guide for more information on the registers of
ARM architecture-based processors that can be accessed using named
register variables.
You can declare named register variables as global variables. You can declare some, but not all, named register variables as local variables. In general, do not declare Vector Floating-Point (VFP) registers and core registers as local variables. Do not declare caller-save registers, such as R0, as local variables. (Caller-save registers are registers that the caller must save the values of, if it wants the values after the subroutine completes.) Your program might still compile if you declare these locally, but you risk unexpected runtime behavior if you do this.
A typical use of named register variables is to access bits
in the Application Program Status Register (APSR)
(see The Application Program Status
Register (APSR) in the Assembler
Guide). Example 3-3 shows the use of named register variables
to set the saturation flag Q in the APSR.
Example 4.3. Setting bits in the APSR using a named register variable
#ifndef __BIG_ENDIAN // bitfield layout of APSR is sensitive to endianness
typedef union
{
struct
{
int mode:5;
int T:1;
int F:1;
int I:1;
int _dnm:19;
int Q:1;
int V:1;
int C:1;
int Z:1;
int N:1;
} b;
unsigned int word;
} PSR;
#else /* __BIG_ENDIAN */
typedef union
{
struct
{
int N:1;
int Z:1;
int C:1;
int V:1;
int Q:1;
int _dnm:19;
int I:1;
int F:1;
int T:1;
int mode:5;
} b;
unsigned int word;
} PSR;
#endif /* __BIG_ENDIAN */
register PSR apsr __asm("apsr");
void set_Q(void)
{
apsr.b.Q = 1;
}