User guide

String copying example
Example 4-1 shows how to use labels and branches in a string copy routine.
This code is also in install_directory\examples\inline\strcopy.c.
The syntax of labels inside assembler blocks is the same as in C. Function calls that use BL from inline assembly
language must specify the input registers, the output registers, and the corrupted registers. In this example, the
inputs to my_strcpy() are a and b, there are no outputs, and the default ATPCS registers, r0-r3, r12, lr, and PSR,
are corrupted.
Example 4-1 String copy
#include <stdio.h>
void my_strcpy(const char *src, char *dst)
{
int ch;
__asm
{
loop:
#ifndef __thumb
// ARM version
LDRB ch, [src], #1
STRB ch, [dst], #1
#else
// Thumb version
LDRB ch, [src]
ADD src, #1
STRB ch, [dst]
ADD dst, #1
#endif
CMP ch, #0
BNE loop
}
}
int main(void)
{
const char *a = "Hello world!";
char b[20];
my_strcpy (a, b);
printf("Original string: '%s'\n", a);
printf("Copied string: '%s'\n", b);
return 0;
}
4.1.2 ARM and Thumb instruction sets
The ARM and Thumb instruction sets are described in the ARM Architecture Reference Manual. All instruction
opcodes and register specifiers can be written in either lowercase or uppercase.
Operand expressions
Any register or constant operand can be an arbitrary C or C++ expression so that variables can be read or written.
The expression must be integer assignable, that is, of type char, short, or int. No sign extension is performed on
char and short types. You must perform sign extension explicitly for these types. The compiler might add code to
evaluate these expressions and allocate them to registers.
When an operand is used as a destination, the expression must be assignable (an lvalue). When writing code that
uses both physical registers and expressions, you must take care not to use complex expressions that require too
many registers to evaluate. The compiler issues an error message if it detects conflicts during register allocation.
Physical registers
The inline assemblers allow restricted access to the physical registers. It is illegal to write to pc. Only branches using
B and BL are allowed. In addition, it is inadvisable to intermix inline assembler instructions that use physical registers
and complex C or C++ expressions.
The compiler uses r12 (ip) and, in tcc and tcpp, r3 for intermediate results, and r0-r3, r12 (ip), r14 (lr) for function
calls while evaluating C expressions, so these cannot be used as physical registers at the same time.
Physical registers, like variables, must be set before they can be read. When physical registers are used the compile
saves and restores C and C++ variables that might be allocated to the same physical register. However, the compiler
cannot restore sp, sl, fp, or sb in calling standards where these registers have a defined role.
Mixing C, C++, and Assembly Language
Copyright ?1999 2001 ARM Limited 4-2