File: strncat.s

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (71 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;
; Ullrich von Bassewitz, 31.05.1998
; Christian Krueger: 12-Aug-2013, minor optimizations
;
; char* strncat (char* dest, const char* src, size_t n);
;

        .export         _strncat
        .import         popax, popptr1
        .importzp       ptr1, ptr2, ptr3, tmp1, tmp2
        .macpack        cpu
        
_strncat:
    eor #$FF        ; one's complement to count upwards
    sta tmp1
    txa
    eor #$FF
    sta tmp2
    
    jsr popptr1     ; get src

    jsr popax       ; get dest
    sta ptr3        ; remember for function return
    stx ptr3+1          
    stx ptr2+1
    tay             ; low byte as offset in Y
.if (.cpu .bitand ::CPU_ISET_65SC02)
    stz ptr2
.else
    ldx #0
    stx ptr2        ; destination on page boundary
.endif

; find end of dest

L1: lda (ptr2),y
    beq L2
    iny
    bne L1
    inc ptr2+1
    bne L1

; end found, apply offset to dest ptr and reset y
L2: sty ptr2

; copy src. We've put the ones complement of the count into the counter, so
; we'll increment the counter on top of the loop

L3: ldy #0
    ldx tmp1        ; low counter byte

L4: inx
    bne L5
    inc tmp2
    beq L6          ; jump if done
L5: lda (ptr1),y
    sta (ptr2),y
    beq L7
    iny
    bne L4
    inc ptr1+1
    inc ptr2+1
    bne L4

; done, set the trailing zero and return pointer to dest

L6: lda #0
    sta (ptr2),y
L7: lda ptr3
    ldx ptr3+1
    rts