Example 4-9 and Example 4-10 on page 4-21 show a C program that uses a call to an
assembly language subroutine to copy one string over the top of another string.
Example 4-9 Calling assembly language from C
#include
extern void strcopy(char *d, const char *s);
int main()
{
const char *srcstr = "First string - source ";
char dststr[] = "Second string - destination ";
/* dststr is an array since we’re going to change it */
printf("Before copying:\n");
printf(" %s\n %s\n",srcstr,dststr);
strcopy(dststr,srcstr);
printf("After copying:\n");
printf(" %s\n %s\n",srcstr,dststr);
return (0);
}
Example 4-10 Assembly language string copy subroutine
AREA SCopy, CODE, READONLY
EXPORT strcopy
strcopy ; r0 points to destination string.
; r1 points to source string.
LDRB r2, [r1],#1 ; Load byte and update address.
STRB r2, [r0],#1 ; Store byte and update address.
CMP r2, #0 ; Check for zero terminator.
BNE strcopy ; Keep going if not.
MOV pc,lr ; Return.
END
Example 4-9 on page 4-20 is located in install_directory\examples\asm as strtest.c
and scopy.s. Follow these steps to build the example from the command line:
1. Type armasm -g scopy.s to build the assembly language source.
2. Type armcc -c -g strtest.c to build the C source.
3. Type armlink strtest.o scopy.o -o strtest to link the object files
4. Type armsd -e strtest execute the example.