Optimization of two string functions (size & speed).
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
;
|
||||
; Ullrich von Bassewitz, 31.05.1998
|
||||
; Christian Krueger: 2013-Jul-24, minor optimization
|
||||
;
|
||||
; char* strcat (char* dest, const char* src);
|
||||
;
|
||||
@@ -12,44 +13,35 @@ _strcat:
|
||||
sta ptr1 ; Save src
|
||||
stx ptr1+1
|
||||
jsr popax ; Get dest
|
||||
sta ptr2
|
||||
stx ptr2+1
|
||||
sta tmp3 ; Remember for function return
|
||||
ldy #0
|
||||
tay
|
||||
lda #0
|
||||
sta ptr2 ; access from page start, y contains low byte
|
||||
stx ptr2+1
|
||||
|
||||
; find end of dest
|
||||
|
||||
sc1: lda (ptr2),y
|
||||
beq sc2
|
||||
findEndOfDest:
|
||||
lda (ptr2),y
|
||||
beq endOfDestFound
|
||||
iny
|
||||
bne sc1
|
||||
bne findEndOfDest
|
||||
inc ptr2+1
|
||||
bne sc1
|
||||
bne findEndOfDest
|
||||
|
||||
; end found, get offset in y into pointer
|
||||
endOfDestFound:
|
||||
sty ptr2 ; advance pointer to last y position
|
||||
ldy #0 ; reset new y-offset
|
||||
|
||||
sc2: tya
|
||||
clc
|
||||
adc ptr2
|
||||
sta ptr2
|
||||
bcc sc3
|
||||
inc ptr2+1
|
||||
|
||||
; copy src
|
||||
|
||||
sc3: ldy #0
|
||||
sc4: lda (ptr1),y
|
||||
copyByte:
|
||||
lda (ptr1),y
|
||||
sta (ptr2),y
|
||||
beq sc5
|
||||
beq done
|
||||
iny
|
||||
bne sc4
|
||||
bne copyByte
|
||||
inc ptr1+1
|
||||
inc ptr2+1
|
||||
bne sc4
|
||||
bne copyByte ; like bra here
|
||||
|
||||
; done, return pointer to dest
|
||||
|
||||
sc5: lda tmp3 ; X does still contain high byte
|
||||
; return pointer to dest
|
||||
done: lda tmp3 ; X does still contain high byte
|
||||
rts
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
;
|
||||
; Ullrich von Bassewitz, 31.05.1998
|
||||
; Christian Krueger, 2013-Aug-04, minor optimization
|
||||
;
|
||||
; const char* strchr (const char* s, int c);
|
||||
;
|
||||
@@ -9,40 +10,38 @@
|
||||
.importzp ptr1, tmp1
|
||||
|
||||
_strchr:
|
||||
sta tmp1 ; Save c
|
||||
jsr popax ; get s
|
||||
sta ptr1
|
||||
stx ptr1+1
|
||||
ldy #0
|
||||
sta tmp1 ; Save c
|
||||
jsr popax ; get s
|
||||
tay ; low byte of pointer to y
|
||||
stx ptr1+1
|
||||
lda #0
|
||||
sta ptr1 ; ptr access page wise
|
||||
|
||||
Loop: lda (ptr1),y ; Get next char
|
||||
beq EOS ; Jump on end of string
|
||||
cmp tmp1 ; Found?
|
||||
beq Found ; Jump if yes
|
||||
Loop: lda (ptr1),y ; Get next char
|
||||
beq EOS ; Jump on end of string
|
||||
cmp tmp1 ; Found?
|
||||
beq Found ; Jump if yes
|
||||
iny
|
||||
bne Loop
|
||||
inc ptr1+1
|
||||
bne Loop ; Branch always
|
||||
bne Loop
|
||||
inc ptr1+1
|
||||
bne Loop ; Branch always
|
||||
|
||||
; End of string. Check if we're searching for the terminating zero
|
||||
|
||||
EOS: lda tmp1 ; Get the char we're searching for
|
||||
bne NotFound ; Jump if not searching for terminator
|
||||
EOS:
|
||||
lda tmp1 ; Get the char we're searching for
|
||||
bne NotFound ; Jump if not searching for terminator
|
||||
|
||||
; Found. Calculate pointer to c.
|
||||
; Found. Set pointer to c.
|
||||
|
||||
Found: ldx ptr1+1 ; Load high byte of pointer
|
||||
tya ; Low byte offset
|
||||
clc
|
||||
adc ptr1
|
||||
bcc Found1
|
||||
inx
|
||||
Found1: rts
|
||||
Found:
|
||||
ldx ptr1+1 ; Load high byte of pointer
|
||||
tya ; low byte is in y
|
||||
rts
|
||||
|
||||
; Not found, return NULL
|
||||
|
||||
NotFound:
|
||||
lda #0
|
||||
lda #0
|
||||
tax
|
||||
rts
|
||||
|
||||
|
||||
Reference in New Issue
Block a user