User guide

{
MOV ip, #3
ADDS x, x, #0x12345678 // this instruction is expanded
ORR x, x, ip
}
The compiler uses ip as a temporary register when it expands the ADD instruction, and corrupts the value 3 in ip.
An error message is issued.
Do not use physical registers to address variables, even when it seems obvious that a specific variable is
mapped onto a specific register. If the compiler detects this it either generates an error message or puts the
variable into another register to avoid conflicts:
int bad_f(int x) // x in r0
{
__asm
{
ADD r0, r0, #1 // wrongly asserts that x is still in r0
}
return x; // x in r0
}
This code returns x unaltered. The compiler assumes that x and r0 are two different variables, despite the fact
that x is allocated to r0 on both function entry and function exit. As the assembly language code does not do
anything useful, it is optimized away. The instruction should be written as:
ADD x, x, #1
Do not save and restore physical registers that are used by an inline assembler. The compiler will do this for
you. If physical registers other than CPSR and SPSR are read without being written to, an error message is
issued. For example:
int f(int x)
{
__asm
{
STMFD sp!, {r0} // save r0 - illegal: read before write
ADD r0, x, 1
EOR x, r0, x
LDMFD sp!, {r0} // restore r0 - not needed.
}
return x;
}
4.1.5 Examples
Example 4-2 to Example 4-5 demonstrates some of the ways that you can use inline assembly language effectively.
Enabling and disabling interrupts
Interrupts are enabled or disabled by reading the CPSR flags and updating bit 7. Example 4-2 shows how this can be
done by using small functions that can be inlined.
This code is also in install_directory\examples\inline\irqs.c.
These functions work only in a privileged mode, because the control bits of the CPSR and SPSR cannot be changed
while in User mode.
Example 4-2 Interrupts
__inline void enable_IRQ(void)
{
int tmp;
__asm
{
MRS tmp, CPSR
BIC tmp, tmp, #0x80
MSR CPSR_c, tmp
}
}
__inline void disable_IRQ(void)
{
Mixing C, C++, and Assembly Language
Copyright ?1999 2001 ARM Limited 4-5