; *********************************************************************** ; * * ; * Function: Copy a NUL-terminated String * ; * * ; *********************************************************************** ; Author: John Zaitseff ; Date: 9th September, 2002 ; Version: 1.4 ; This file contains a function, written in assembly language, to copy a ; C-style string from one location to another. C-style strings are ASCII ; strings with a NUL character (code 0) at the end. This function, along ; with the files "strcopy-a.s" and "strcopy-c.c", is a demonstration of ; using multiple source modules in a single executable. ; You can use "make" to create the executables that depend on this source ; file; the source modules that make use of this file are "strcopy-a.s" ; (assembly language version) and "strcopy-c.c" (C language version). .text .equ NUL, 0 ; ASCII code for NUL (end of string indicator) .global strcopy ; Make the label visible to all modules ; The function "strcopy" needs to agree with its callers about what to ; expect (in registers and in memory) on entry and on exit. ARM defines ; the ARM-Thumb Procedure Call Standard to help you with this---this ; standard is on the CD-ROM in the "reference" directory. ; On entry to this function, R0 = pointer to (ie, address of) the ; destination string; R1 = pointer to the source string. Nothing (at ; least worthwhile) is returned on exit. This corresponds to the C ; definition: ; void strcopy (char *dest, const char *src) ; and is very similar to the C function "strcpy". ; DANGER, Will Robinson! This function should not be used in real code. ; What happens if the source string is longer than the buffer set aside ; for the destination? Search for "buffer overflow" on www.google.com to ; find out... strcopy: ; Entry to "strcopy" function ; R0 = address of destination string ; R1 = address of source string copyloop: ldrb r2,[r1],#1 ; Load byte into R2 and update R1 strb r2,[r0],#1 ; Store the byte and update R0 cmp r2,#NUL ; Check for NUL terminator bne copyloop ; Repeat loop if not mov pc,lr ; Return to the caller ; By the way, symbols are case-sensitive: if you replace "cmp r2,#NUL" ; above with "cmp r2,#nul", the GNU Assembler will complain: ; Error: internal relocation (type 232) not fixed up (IMMEDIATE) ; Yes, this is cryptic! The reason is that the GNU Assembler treats all ; undefined symbols (in this case, "nul") as being defined in an external ; module; that is not allowed for #-type parameters. .end