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