2000-05-28 13:40:48 +00:00
|
|
|
;
|
2003-05-04 22:21:13 +00:00
|
|
|
; Ullrich von Bassewitz, 2003-05-04
|
2000-05-28 13:40:48 +00:00
|
|
|
;
|
2003-05-04 22:21:13 +00:00
|
|
|
; char* __fastcall__ strncpy (char* dest, const char* src, unsigned size);
|
2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _strncpy
|
|
|
|
.import popax
|
|
|
|
.importzp ptr1, ptr2, tmp1, tmp2, tmp3
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-05-04 22:21:13 +00:00
|
|
|
.proc _strncpy
|
|
|
|
|
|
|
|
eor #$FF
|
|
|
|
sta tmp1
|
|
|
|
txa
|
|
|
|
eor #$FF
|
|
|
|
sta tmp2 ; Store -size - 1
|
2003-05-04 22:27:20 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax ; get src
|
|
|
|
sta ptr1
|
|
|
|
stx ptr1+1
|
|
|
|
jsr popax ; get dest
|
|
|
|
sta ptr2
|
|
|
|
stx ptr2+1
|
|
|
|
stx tmp3 ; remember for function return
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-05-04 22:21:13 +00:00
|
|
|
; Copy src -> dest up to size bytes
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-05-04 22:21:13 +00:00
|
|
|
ldx tmp1 ; Load low byte of ones complement of size
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #$00
|
2003-05-04 22:21:13 +00:00
|
|
|
L1: inx
|
|
|
|
bne L2
|
|
|
|
inc tmp2
|
|
|
|
beq L9
|
|
|
|
|
|
|
|
L2: lda (ptr1),y ; Copy one character
|
|
|
|
sta (ptr2),y
|
2003-08-17 20:15:01 +00:00
|
|
|
beq L5 ; Bail out if terminator reached (A = 0)
|
2003-05-04 22:21:13 +00:00
|
|
|
iny
|
|
|
|
bne L1
|
|
|
|
inc ptr1+1
|
|
|
|
inc ptr2+1 ; Bump high bytes
|
|
|
|
bne L1 ; Branch always
|
2003-05-04 22:27:20 +00:00
|
|
|
|
2003-08-18 11:25:31 +00:00
|
|
|
; Fill the remaining bytes.
|
2003-05-04 22:21:13 +00:00
|
|
|
|
2003-08-18 11:25:31 +00:00
|
|
|
L3: inx ; Counter low byte
|
|
|
|
beq L6 ; Branch on overflow
|
|
|
|
L4: sta (ptr2),y ; Clear one byte
|
|
|
|
L5: iny ; Bump pointer
|
2003-05-04 22:21:13 +00:00
|
|
|
bne L3
|
|
|
|
inc ptr2+1 ; Bump high byte
|
|
|
|
bne L3 ; Branch always
|
|
|
|
|
2003-08-18 11:25:31 +00:00
|
|
|
; Bump the counter high byte
|
|
|
|
|
|
|
|
L6: inc tmp2
|
|
|
|
bne L4
|
|
|
|
|
2003-05-04 22:21:13 +00:00
|
|
|
; Done, return dest
|
|
|
|
|
2003-05-04 22:27:20 +00:00
|
|
|
L9: lda ptr2 ; Get low byte
|
|
|
|
ldx tmp3 ; Get unchanged high byte
|
2003-05-04 22:21:13 +00:00
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
|
|
|
|