1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-08 21:29:30 +00:00
C02/include/string.a02

206 lines
8.2 KiB
Plaintext
Raw Permalink Normal View History

2018-01-28 18:30:49 +00:00
; C02 library string.h02 assembly language subroutines
2021-09-21 00:54:24 +00:00
; Requires external routines SETSRC, SETDSB, and SETDST
2018-01-28 18:30:49 +00:00
; Requires the following RAM locations be defined
2021-09-21 00:54:24 +00:00
; external zero page words SRCPTR and DSTPTR, bytes
; TEMP0 and TEMP3, and block SYSBFR.
2018-01-28 18:30:49 +00:00
SUBROUTINE STRING
2018-01-28 18:30:49 +00:00
;strcmp(&s) - Compare String (to Destination String)
;Requires: DSTPTR - Pointer to destination string
2018-01-28 18:30:49 +00:00
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to string
2021-09-21 00:54:24 +00:00
; N based on return value of A
2018-01-28 18:30:49 +00:00
;Returns A=$01 and C=1 if Destination > Source
; A=$00 and Z=1, C=1 if Destination = Source
; A=$FF and C=0 if Destination < Source
; Y=Position of first character that differs
STRCMP: JSR SETSRC ;Initialize Source String
2021-09-21 00:54:24 +00:00
STRCML LDA (DSTPTR),Y ;Load Destination Character
CMP (SRCPTR),Y ;Compare Against Source Character
BNE .CMPEND ;If Equal
ORA (SRCPTR),Y ; OR with Source Character
BEQ .RETURN ; If Both are 0, Return 0
2018-01-28 18:30:49 +00:00
INY ; Increment Offset
2021-09-21 00:54:24 +00:00
BPL STRCML ; and Loop if < 128
.CMPEND BCC .RETFLS ;If Source < Destination, Return $FF & Carry Clear
2018-01-28 18:30:49 +00:00
LDA #$01 ;Else Return 1 and Carry Set
.RETURN RTS
2018-01-28 18:30:49 +00:00
2021-09-21 00:54:24 +00:00
;strcnt(a,&s) - Count Occurrences of c in s
;Args: A - Character to count
; X,Y - Pointer to string
;Sets: SRCPTR = Pointer to source string
; N,Z based on return value of A
;Returns: A,X = Number of occurrences
; Y = Length of string
STRCNT: STA TEMP0 ;Save Character
JSR SETSRC ;Setup Source Pointer
LDX #0 ;Initialize Character Count
.CNTLOP LDA (SRCPTR),Y ;Get Next Character
BEQ .CDONE ;If <> NUL
CMP TEMP0 ;
BNE .CNTSKP ; If Character Match
INX ; Inrement Count
.CNTSKP INY ; Increment Index
BPL .CNTLOP ; and Loop if < 128
.CDONE TXA ;ReturnCount
RTS ;and Return
2018-01-28 18:30:49 +00:00
;strchr(c, &s) - Find First Occurance of Character in String
;Args: A = Character to look for
; X,Y = Pointer to string to search in
;Sets: SRCPTR = Pointer to string
2019-03-23 01:18:49 +00:00
; TEMP3 = Character being searched for
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
2018-01-28 18:30:49 +00:00
;Returns: A = Position in string, C=1 if found
; A = $FF, C=0 if not found
; Y = Position of last character scanned
STRCHR: JSR SETSRC ;Initialize Source String
STRCHA: STA TEMP3 ;Save Search Character (alternate entry point)
.CHRLOP LDA (SRCPTR),Y ;Get Next Character
BEQ .CLCFLS ;If NUL, Return $FF and Carry Clear
2019-03-23 01:18:49 +00:00
CMP TEMP3 ;Compare Character
2021-09-21 00:54:24 +00:00
BEQ .LENFIN ;If Found, Return Index
2018-01-28 18:30:49 +00:00
INY ;Increment Counter and Loop if < 128
BPL .CHRLOP ;Else Return $FF and Carry Clear
.CLCFLS CLC ;Clear Carry
.RETFLS LDA #$FF ;Load -1 into Accumulater
2021-09-21 00:54:24 +00:00
BNE .RETURN ;and Return
2018-01-28 18:30:49 +00:00
;strlen(&s) - Return Length of String
;Args: X,Y - Pointer to string
;Sets: SRCPTR = Pointer to source string
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
2018-01-28 18:30:49 +00:00
;Returns: A,Y = Length of string
STRLEN: JSR SETSRC ;Initialize Source String
2021-09-21 00:54:24 +00:00
STRLEA: LDA (SRCPTR),Y ;Get Next Character
BEQ .LENFIN ;If <> NUL
2018-01-28 18:30:49 +00:00
INY ; Increment Index
2021-09-21 00:54:24 +00:00
BPL STRLEA ; and Loop if < 128
.LENFIN TYA ;Transfer Index to Accumulator
2018-01-28 18:30:49 +00:00
RTS ;and Return
;strcpb(&s) - Copy String to System Buffer
2021-09-21 00:54:24 +00:00
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to source string
; DSTPTR = Address of System Buffer
; N,Z based on return value of A
;Returns: A,Y = Length of copied string
STRCPB: JSR SETSRC ;Set Source Pointer to String Address
JSR SETDSB ;Set Destination Pointer to System Buffer
BVC STRCPA ;Execute String Copy
2018-01-28 18:30:49 +00:00
;strcat(&s) Concatenate String (to Destination String)
;Requires: DSTPTR - Pointer to destination string
2018-01-28 18:30:49 +00:00
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to source string
2019-03-23 01:18:49 +00:00
; TEMP3 = Length of source prior to concatenation
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
;Affects: C
2018-01-28 18:30:49 +00:00
;Returns: A,Y = Total length of concatenated string
STRCAT: JSR SETSRC ;Initialize Source String
.CATLOP LDA (DSTPTR),Y ;Find end of Destination String
2021-09-21 00:54:24 +00:00
BEQ .CATFIN ;
2018-01-28 18:30:49 +00:00
INY ;
BPL .CATLOP ;
2021-09-21 00:54:24 +00:00
.CATFIN STY TEMP3 ;Subtract Destination String Length
LDA SRCPTR ; from Source String Pointer
2018-01-28 18:30:49 +00:00
SEC
2019-03-23 01:18:49 +00:00
SBC TEMP3
STA SRCPTR
LDA SRCPTR+1
2018-01-28 18:30:49 +00:00
SBC #$00
STA SRCPTR+1
JMP .CPYLOP ;Execute String Copy
2018-01-28 18:30:49 +00:00
;strcpy(&s) - Copy String (to Destination String)
;Requires: DSTPTR - Pointer to destination string
2018-01-28 18:30:49 +00:00
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to source string
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
2018-08-14 18:14:32 +00:00
;Returns: A,Y = Number of characters copied
2020-09-08 15:51:30 +00:00
STRCPA: LDY #0 ;Alternate entry point
BEQ .CPYLOP ;for when Source already set
2018-01-28 18:30:49 +00:00
STRCPY: JSR SETSRC ;Initialize Source String
2021-09-21 00:54:24 +00:00
.CPYLOP LDA (SRCPTR),Y ;Get Character from Source String
STA (DSTPTR),Y ;Copy to Destination String
BEQ .CPYFIN ;If <> NUL
2018-01-28 18:30:49 +00:00
INY ; Increment Index
BPL .CPYLOP ; and Loop if < 128
2021-09-21 00:54:24 +00:00
.CPYFIN TYA ;Transfer Index to Accumulator
2018-01-28 18:30:49 +00:00
RTS ;and Return
2021-09-21 00:54:24 +00:00
;strcut(n, &s) - Copy from Position n to End of Source
;Requires: DSTPTR - Pointer to destination string
2018-01-28 18:30:49 +00:00
;Args: A = Starting position in start string
; X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to specified position in source string
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
2018-01-28 18:30:49 +00:00
;Returns: A,Y = Length of copied string
STRCUT: JSR SETSRC ;Initialize Source String
CLC
ADC SRCPTR ;Move Source Pointer
STA SRCPTR ; to Specified Position in String
BCC .CPYLOP
INC SRCPTR+1
JMP .CPYLOP ;and Jump Into String Copy Loop
2021-09-21 00:54:24 +00:00
2018-01-28 18:30:49 +00:00
;strstr(&s) - Search for String (in Destination String)
;Requires: DSTPTR - Pointer to destination string
2018-01-28 18:30:49 +00:00
;Args: X,Y = Pointer to search string
;Sets: DSTPTR = Pointer to position in source string
2021-09-21 00:54:24 +00:00
; End of string if not found
; SRCPTR = Pointer to source string
2019-03-23 01:18:49 +00:00
; TEMP3 = Last position checked in destination string
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
2018-01-28 18:30:49 +00:00
;Returns: A = Position, C=1 if found
; A = $FF, C=0 if not found
; Y = Last position checked in source string
STRSTR: JSR SETSRC ;Initialize Source String
2019-03-23 01:18:49 +00:00
STY TEMP3 ;Initialize Position
.STRLOP LDY #$00; ;Initialize Compare Offset
LDA (DSTPTR),Y ;Get Start Character in Destination
BEQ .CLCFLS ;If NUL return $FF and Carry Clear
2021-09-21 00:54:24 +00:00
JSR STRCML ;Jump into Compare Loop
BEQ .STRFIN ;If Not Equal
BMI .STRNXT ; If Source is Greater
LDA (SRCPTR),Y ; If at End of Source String
2021-09-21 00:54:24 +00:00
BEQ .STRFIN ; Return Current Position
.STRNXT INC TEMP3 ; Else Increment Position
BMI .CLCFLS ; If > 127 return $FF and Carry Clear
INC DSTPTR ; Increment Source Pointer
BNE .STRLOP
INC DSTPTR+1 ; If not End of Memory
BNE .STRLOP ; Loop
BEQ .CLCFLS ; Else return $FF and Carry Clear
2021-09-21 00:54:24 +00:00
.STRFIN SEC ;Else Set Carry
2019-03-23 01:18:49 +00:00
LDA TEMP3 ; Load Position
2018-01-28 18:30:49 +00:00
RTS ; and Return
2021-09-21 00:54:24 +00:00
;strrch(c, &s) - Find Last Occurrence of Character in String
2018-01-28 18:30:49 +00:00
;Args: A = Character to look for
; X,Y = Pointer to string to search in
;Sets: SRCPTR = Pointer to string
2019-03-23 01:18:49 +00:00
; TEMP3 = Character being searched for
2021-09-21 00:54:24 +00:00
; N,Z based on return value of A
;Affects: Y,C
;Returns: A,X = Position of last occurrence in string
2019-03-23 01:18:49 +00:00
; $FF if not found
; Y = Length of String
2018-01-28 18:30:49 +00:00
STRRCH: JSR SETSRC ;Initialize Source String
2021-09-21 00:54:24 +00:00
STA TEMP3 ;Save Search Character (alternate entry point)
2019-03-23 01:18:49 +00:00
LDX #$FF ;Initialize Position
.RCHLOP LDA (SRCPTR),Y ;Get Next Character
2021-09-21 00:54:24 +00:00
BEQ .RCHFIN ;If NUL, Exit with Position
2019-03-23 01:18:49 +00:00
CMP TEMP3 ;Compare Character
BNE .RCHNXT ;If Found
2019-03-23 01:18:49 +00:00
TYA ; Store Counter
TAX
.RCHNXT INY ;Increment Counter
BPL .RCHLOP ; and Loop if < 128
2021-09-21 00:54:24 +00:00
.RCHFIN TXA ;Copy Position to Accumulater
2018-01-28 18:30:49 +00:00
RTS ; and Return
ENDSUBROUTINE