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