| |||
| Home > Mixing C, C++, and Assembly Language > Calls to assembly language from C | |||
To be able to call an assembly language routine from C, you
must use the EXPORT directive to export the function
symbol:
Example 13. Assembly language string copy subroutine
PRESERVE8
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 null terminator.
BNE strcopy ; Keep going if not.
BX lr ; Return.
END
To call this assembly language subroutine from C, declare
it with extern:
Example 14. Calling assembly language from C
#include <stdio.h> 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);
}