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