]> git.sur5r.net Git - cc65/blob - libsrc/common/strncat.s
Merge pull request #494 from jedeoric/master
[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         .macpack        cpu
12         
13 _strncat:
14     eor #$FF        ; one's complement to count upwards
15     sta tmp1
16     txa
17     eor #$FF
18     sta tmp2
19     
20     jsr popax       ; get src
21     sta ptr1
22     stx ptr1+1
23
24     jsr popax       ; get dest
25     sta ptr3        ; remember for function return
26     stx ptr3+1          
27     stx ptr2+1
28     tay             ; low byte as offset in Y
29 .if (.cpu .bitand ::CPU_ISET_65SC02)
30     stz ptr2
31 .else
32     ldx #0
33     stx ptr2        ; destination on page boundary
34 .endif
35
36 ; find end of dest
37
38 L1: lda (ptr2),y
39     beq L2
40     iny
41     bne L1
42     inc ptr2+1
43     bne L1
44
45 ; end found, apply offset to dest ptr and reset y
46 L2: sty ptr2
47
48 ; copy src. We've put the ones complement of the count into the counter, so
49 ; we'll increment the counter on top of the loop
50
51 L3: ldy #0
52     ldx tmp1        ; low counter byte
53
54 L4: inx
55     bne L5
56     inc tmp2
57     beq L6          ; jump if done
58 L5: lda (ptr1),y
59     sta (ptr2),y
60     beq L7
61     iny
62     bne L4
63     inc ptr1+1
64     inc ptr2+1
65     bne L4
66
67 ; done, set the trailing zero and return pointer to dest
68
69 L6: lda #0
70     sta (ptr2),y
71 L7: lda ptr3
72     ldx ptr3+1
73     rts