]> git.sur5r.net Git - cc65/blob - libsrc/common/strncpy.s
un-remove TABs in doc/using-make.sgml
[cc65] / libsrc / common / strncpy.s
1 ;
2 ; Ullrich von Bassewitz, 2003-05-04
3 ;
4 ; char* __fastcall__ strncpy (char* dest, const char* src, unsigned size);
5 ;
6
7         .export         _strncpy
8         .import         popax, popptr1
9         .importzp       ptr1, ptr2, tmp1, tmp2, tmp3
10
11 .proc   _strncpy
12
13         eor     #$FF
14         sta     tmp1
15         txa
16         eor     #$FF
17         sta     tmp2            ; Store -size - 1
18
19         jsr     popptr1         ; get src
20         jsr     popax           ; get dest
21         sta     ptr2
22         stx     ptr2+1
23         stx     tmp3            ; remember for function return
24
25 ; Copy src -> dest up to size bytes
26
27         ldx     tmp1            ; Load low byte of ones complement of size
28         ldy     #$00
29 L1:     inx
30         bne     L2
31         inc     tmp2
32         beq     L9
33
34 L2:     lda     (ptr1),y        ; Copy one character
35         sta     (ptr2),y
36         beq     L5              ; Bail out if terminator reached (A = 0)
37         iny
38         bne     L1
39         inc     ptr1+1
40         inc     ptr2+1          ; Bump high bytes
41         bne     L1              ; Branch always
42
43 ; Fill the remaining bytes.
44
45 L3:     inx                     ; Counter low byte
46         beq     L6              ; Branch on overflow
47 L4:     sta     (ptr2),y        ; Clear one byte
48 L5:     iny                     ; Bump pointer
49         bne     L3
50         inc     ptr2+1          ; Bump high byte
51         bne     L3              ; Branch always
52
53 ; Bump the counter high byte
54
55 L6:     inc     tmp2
56         bne     L4
57
58 ; Done, return dest
59
60 L9:     lda     ptr2            ; Get low byte
61         ldx     tmp3            ; Get unchanged high byte
62         rts
63
64 .endproc
65
66
67