2009-10-26 18:05:23 +08:00
|
|
|
.file "strncat.S"
|
|
|
|
|
|
|
|
.section .text
|
|
|
|
.global _strncat
|
|
|
|
.type _strncat,@function
|
|
|
|
_strncat:
|
|
|
|
;; On entry: r1 => Destination
|
|
|
|
;; r2 => Source
|
|
|
|
;; r3 => Max number of bytes to copy
|
2015-04-09 16:20:00 +08:00
|
|
|
#ifdef __RX_DISALLOW_STRING_INSNS__
|
|
|
|
cmp #0, r3 ; If max is zero we have nothing to do.
|
|
|
|
beq 2f
|
2009-10-26 18:05:23 +08:00
|
|
|
|
2015-04-09 16:20:00 +08:00
|
|
|
mov r1, r4 ; Leave the desintation pointer intact for the return value.
|
|
|
|
|
|
|
|
1: mov.b [r4+], r5 ; Find the NUL byte at the end of the destination.
|
|
|
|
cmp #0, r5
|
|
|
|
bne 1b
|
|
|
|
|
|
|
|
sub #1, r4
|
|
|
|
|
|
|
|
3: mov.b [r2+], r5 ; Copy bytes from the source into the destination ...
|
|
|
|
mov.b r5, [r4+]
|
|
|
|
cmp #0, r5 ; ... until we reach a NUL byte ...
|
|
|
|
beq 2f
|
|
|
|
sub #1, r3
|
|
|
|
bne 3b ; ... or we have copied N bytes.
|
|
|
|
|
|
|
|
2: rts
|
|
|
|
#else
|
2009-10-26 18:05:23 +08:00
|
|
|
mov r1, r4 ; Save a copy of the dest pointer.
|
|
|
|
mov r2, r5 ; Save a copy of the source pointer.
|
2009-11-20 20:54:29 +08:00
|
|
|
mov r3, r14 ; Save a copy of the byte count.
|
2009-10-26 18:05:23 +08:00
|
|
|
|
|
|
|
mov #0, r2 ; Search for the NUL byte.
|
|
|
|
mov #-1, r3 ; Search until we run out of memory.
|
|
|
|
suntil.b ; Find the end of the destination string.
|
|
|
|
sub #1, r1 ; suntil.b leaves r1 pointing to the byte beyond the NUL.
|
|
|
|
|
2009-11-20 20:54:29 +08:00
|
|
|
mov r14, r3 ; Restore the limit on the number of bytes copied.
|
2009-10-26 18:05:23 +08:00
|
|
|
mov r5, r2 ; Restore the source pointer.
|
|
|
|
mov r1, r5 ; Save a copy of the dest pointer.
|
|
|
|
smovu ; Copy source to destination.
|
|
|
|
|
2009-11-20 20:54:29 +08:00
|
|
|
add #0, r14, r3 ; Restore the number of bytes to copy (again), but this time set the Z flag as well.
|
2009-10-26 18:05:23 +08:00
|
|
|
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.
|
|
|
|
mov #0, r2 ; Otherwise we must check to see if a NUL byte
|
|
|
|
mov r5, r1 ; was included in the bytes that were copied.
|
|
|
|
suntil.b
|
|
|
|
beq 1f ; Z flag is set if a match was found.
|
2009-11-20 20:54:29 +08:00
|
|
|
add r14, r5 ; Point at byte after end of copied bytes.
|
2009-10-26 18:05:23 +08:00
|
|
|
mov.b #0, [r5] ; Store a NUL there.
|
|
|
|
1:
|
|
|
|
mov r4, r1 ; Return the original dest pointer.
|
|
|
|
rts
|
2015-04-09 16:20:00 +08:00
|
|
|
#endif
|
|
|
|
.size _strncat, . - _strncat
|
|
|
|
|