]> git.sur5r.net Git - cc65/blob - libsrc/common/strncpy.s
Simplify code generated for the ?: operator when type conversion code for the
[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
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     popax           ; get src
20         sta     ptr1
21         stx     ptr1+1
22         jsr     popax           ; get dest
23         sta     ptr2
24         stx     ptr2+1
25         stx     tmp3            ; remember for function return
26
27 ; Copy src -> dest up to size bytes
28
29         ldx     tmp1            ; Load low byte of ones complement of size
30         ldy     #$00
31 L1:     inx
32         bne     L2
33         inc     tmp2
34         beq     L9
35
36 L2:     lda     (ptr1),y        ; Copy one character
37         sta     (ptr2),y
38         beq     L5              ; Bail out if terminator reached (A = 0)
39         iny
40         bne     L1
41         inc     ptr1+1
42         inc     ptr2+1          ; Bump high bytes
43         bne     L1              ; Branch always
44
45 ; Fill the remaining bytes.
46
47 L3:     inx                     ; Counter low byte
48         beq     L6              ; Branch on overflow
49 L4:     sta     (ptr2),y        ; Clear one byte
50 L5:     iny                     ; Bump pointer
51         bne     L3
52         inc     ptr2+1          ; Bump high byte
53         bne     L3              ; Branch always
54
55 ; Bump the counter high byte
56
57 L6:     inc     tmp2
58         bne     L4
59
60 ; Done, return dest
61
62 L9:     lda     ptr2            ; Get low byte
63         ldx     tmp3            ; Get unchanged high byte
64         rts
65
66 .endproc
67
68
69