]> git.sur5r.net Git - cc65/blob - libsrc/common/strncat.s
Added further optimizations and unit tests.
[cc65] / libsrc / common / strncat.s
1 ;
2 ; Ullrich von Bassewitz, 31.05.1998
3 ; Christian Krueger: 12-Aug-2013, minor optimizations
4 ;
5 ; char* strncat (char* dest, const char* src, size_t n);
6 ;
7
8         .export         _strncat
9         .import         popax
10         .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
11
12 _strncat:
13     eor #$FF        ; one's complement to count upwards
14     sta tmp1
15     txa
16     eor #$FF
17     sta tmp2
18     
19     jsr popax       ; get src
20     sta ptr1
21     stx ptr1+1
22
23     jsr popax       ; get dest
24     sta ptr3        ; remember for function return
25     stx ptr3+1          
26     stx ptr2+1
27     tay             ; low byte as offset in Y
28 .if (.cpu .bitand ::CPU_ISET_65SC02)
29     stz ptr2
30 .else
31     ldx #0
32     stx ptr2        ; destination on page boundary
33 .endif
34
35 ; find end of dest
36
37 L1: lda (ptr2),y
38     beq L2
39     iny
40     bne L1
41     inc ptr2+1
42     bne L1
43
44 ; end found, apply offset to dest ptr and reset y
45 L2: sty ptr2
46
47 ; copy src. We've put the ones complement of the count into the counter, so
48 ; we'll increment the counter on top of the loop
49
50 L3: ldy #0
51     ldx tmp1        ; low counter byte
52
53 L4: inx
54     bne L5
55     inc tmp2
56     beq L6          ; jump if done
57 L5: lda (ptr1),y
58     sta (ptr2),y
59     beq L7
60     iny
61     bne L4
62     inc ptr1+1
63     inc ptr2+1
64     bne L4
65
66 ; done, set the trailing zero and return pointer to dest
67
68 L6: lda #0
69     sta (ptr2),y
70 L7: lda ptr3
71     ldx ptr3+1
72     rts
73
74