1/* 2Copyright (c) 2009 Nick Clifton <nickc@redhat.com> 3 */ 4#include <picolibc.h> 5 6 .file "strncat.S" 7 8 .section .text 9 .global _strncat 10 .type _strncat,@function 11_strncat: 12 ;; On entry: r1 => Destination 13 ;; r2 => Source 14 ;; r3 => Max number of bytes to copy 15#ifdef __RX_DISALLOW_STRING_INSNS__ 16 cmp #0, r3 ; If max is zero we have nothing to do. 17 beq 2f 18 19 mov r1, r4 ; Leave the desintation pointer intact for the return value. 20 211: mov.b [r4+], r5 ; Find the NUL byte at the end of the destination. 22 cmp #0, r5 23 bne 1b 24 25 sub #1, r4 26 273: mov.b [r2+], r5 ; Copy bytes from the source into the destination ... 28 mov.b r5, [r4+] 29 cmp #0, r5 ; ... until we reach a NUL byte ... 30 beq 2f 31 sub #1, r3 32 bne 3b ; ... or we have copied N bytes. 33 342: rts 35#else 36 mov r1, r4 ; Save a copy of the dest pointer. 37 mov r2, r5 ; Save a copy of the source pointer. 38 mov r3, r14 ; Save a copy of the byte count. 39 40 mov #0, r2 ; Search for the NUL byte. 41 mov #-1, r3 ; Search until we run out of memory. 42 suntil.b ; Find the end of the destination string. 43 sub #1, r1 ; suntil.b leaves r1 pointing to the byte beyond the NUL. 44 45 mov r14, r3 ; Restore the limit on the number of bytes copied. 46 mov r5, r2 ; Restore the source pointer. 47 mov r1, r5 ; Save a copy of the dest pointer. 48 smovu ; Copy source to destination. 49 50 add #0, r14, r3 ; Restore the number of bytes to copy (again), but this time set the Z flag as well. 51 beq 1f ; If we copied 0 bytes then we already know that the dest string is NUL terminated, so we do not have to do anything. 52 mov #0, r2 ; Otherwise we must check to see if a NUL byte 53 mov r5, r1 ; was included in the bytes that were copied. 54 suntil.b 55 beq 1f ; Z flag is set if a match was found. 56 add r14, r5 ; Point at byte after end of copied bytes. 57 mov.b #0, [r5] ; Store a NUL there. 581: 59 mov r4, r1 ; Return the original dest pointer. 60 rts 61#endif 62 .size _strncat, . - _strncat 63 64