Updated and cleaned up module string

This commit is contained in:
Curtis F Kaylor 2021-09-20 20:54:24 -04:00
parent 92f1611315
commit b1eded3399
3 changed files with 124 additions and 155 deletions

View File

@ -1,4 +1,4 @@
Common String Manipulation Functions for C02
Common String Handling Functions for C02
Strings are zero-terminated arrays of type char with a maximum length
of 128 characters.
@ -14,7 +14,8 @@ Due to the limitations of parameter passing in C02, the argument lists of
most of these functions do not match those in standard C and C++.
For functions that operate on two string, one of the strings is specified
using the setdst() function from the stddef module.
using the setdst() function from module stddef. Unless otherwise noted,
none of the functions change dstptr.
Usage: at the beginning of the program use the directives
@ -25,29 +26,26 @@ The following functions are defined:
n = strlen(s); Returns length of string.
Note: Leaves address of s in srcptr.
Note: Sets srcptr to &s.
p = strchr(c, s); Searches string s for character c.
n = strchr(c, s); Searches string s for character c.
Returns position of first occurance of character
in string, or 255 if character was not found.
Note: Leaves address of s in srcptr.
Note: Sets srcptr to &s.
p = strrch(c, s); Searches end of string s for character c.
Returns position of last occurance of character
n = strrch(c, s); Returns position of last occurance of character
in string, or 255 if character was not found.
Note: Leaves address of s in srcptr.
Note: Sets srcptr to &s.
n = strcat(s); Concatenates source string s onto the end of
destination string set by prior setdst() call.
Returns total length of concatenated string.
Note: leaves the address of destination string
in dstptr.
Note: Sets srcptr to &s.
c = strcmp(s); Compares source string s against destination
string set by prior setdst() call.
@ -58,63 +56,36 @@ The following functions are defined:
These results can be evaluated using the C02
unary comparator ! or the test-operators :+ or :-.
Note: leaves the address of s in srcptr and
the address of the destination string in dstptr.
Note: Sets srcptr to &s.
n = strcpy(s); Copies wource string s into destination string set
by prior setdst() call, replacing previous contents.
Returns number of characters copied.
Note: leaves the address of s in srcptr and
the address of the destination string in dstptr.
To copy the first n characters from string s to
string d, the following code can be used:
setdst(&d); strcpy(s); s[n]=0;
p = strstr(s); Searches for destination string s in source string
set by prior setdst() call.
Note: Sets srcptr to &s.
n = strstr(s); Searches for string s in destination string set.
Returns position of source string in destination
string, or 255 if character was not found.
Note: calls strcmp(), leaving the address of s
in srcptr and the position of the source string in
the destination string (or the end of the destination
string if the source string was not found) in dstptr.
Note: calls strcmp(), leaving srcptr pointing to
the matched string in the destination string
(or the end if not found).
These functions are not part of the standard C and C++ string libraries.
They are included because they are more efficient than equivalent C02 code.
p = strapd(c, s); Append character c to string s.
Returns length of appended string.
If the string length exceeds 127 prior to the
append, no action is taken and the existing
length is returned.
Note: calls strlen(), leaving the address of
s in srcptr.
strppd(c, s); Prepend character c to string s.
Returns length of prepended string.
If the string length exceeds 127 prior to the
prepend, no action is taken and the existing
length is returned.
Note: calls strlen(), leaving the address of
s in srcptr.
strcpb(s) Copies string s to the system buffer (sysbfr).
Note: calls strcpy(), leaving the address of s
in srcptr and the address of the system buffer
in dstptr.
Note: sets srcptr to &s and dstptr to &sysbfr,
then calls strcpy().
n = strcut(n, s); Copies from source string s, starting at position n,
into destination string set by prior setdst() call,
@ -128,9 +99,8 @@ They are included because they are more efficient than equivalent C02 code.
setdst(&d); strcut(s, n); s[i] = 0;
Note: calls routine strcat(), leaving the address
of the destination string in dstptr and address of
position n in the source string in srcptr.
Note: Sets srcptr to the address of position n
in s and calls strcpy().
Note: This library expects the following functions to be defined

View File

@ -1,75 +1,58 @@
; C02 library string.h02 assembly language subroutines
; Requires external routines SETSRC and SETDST
; Requires external routines SETSRC, SETDSB, and SETDST
; Requires the following RAM locations be defined
; external zero page byte pairs SRCPTR and DSTPTR
; and external bytes TEMP0 and TEMP1
; external zero page words SRCPTR and DSTPTR, bytes
; TEMP0 and TEMP3, and block SYSBFR.
SUBROUTINE STRING
;strapd(c, s) - Append charecter c to string s
;Args: A = Character to be appended
; X,Y = Pointer to string
;Affects N,Z
;Returns A,Y = New String Length
STRAPD: STA TEMP0 ;Save Character to Append
JSR STRLEN ;Get Length of String
BMI .RETFLS ;Return 255 if > 127
LDA TEMP0 ;Restore Character to Append
.APDLOP STA (SRCPTR),Y ;Store at End of String
BEQ .LENEND ;Exit if NUL
INY ;Increment Past New Character
LDA #0 ;Set Character to NUL
BEQ .APDLOP ;and Append to String
;strppd(c, s) - Prepend Charecter to String
;Args: A = Character to be appended
; X,Y = Pointer to string
;Sets: N,Z
;Returns A = Insterted Character
STRPPD: STA TEMP0 ;Save Character to Append
JSR STRLEN ;Get Length of String
BMI .RETFLS ;Return 255 if > 127
INY ;Bump up string length
TYA ;Push string length onto stack
PHA
.PPDLOP DEY ;Copy Preceding Chacter
LDA (SRCPTR),Y
INY ;into Currebt Position
STA (SRCPTR),Y
DEY ;Move to Preceding Position
BNE .PPDLOP ;and Loop
LDA TEMP0 ;Retrieve Character
STA (SRCPTR),Y ;and Store in Position 0
PLA ;Retrieve new string length
BEQ .RETORA ;Set Flags and Return`
;strcmp(&s) - Compare String (to Destination String)
;Requires: DSTPTR - Pointer to destination string
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to string
;Affects N,Z
; N based on return value of A
;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
.CMPLOP LDA (DSTPTR),Y ;Load Destination Character
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
INY ; Increment Offset
BPL .CMPLOP ; and Loop if < 128
BPL STRCML ; and Loop if < 128
.CMPEND BCC .RETFLS ;If Source < Destination, Return $FF & Carry Clear
LDA #$01 ;Else Return 1 and Carry Set
.RETURN RTS
;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
;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
; TEMP3 = Character being searched for
;Affects: N,Z
; N,Z based on return value of A
;Returns: A = Position in string, C=1 if found
; A = $FF, C=0 if not found
; Y = Position of last character scanned
@ -78,32 +61,32 @@ 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
CMP TEMP3 ;Compare Character
BEQ .LENEND ;If Found, Return Index
BEQ .LENFIN ;If Found, Return Index
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
RTS ;and Return
BNE .RETURN ;and Return
;strapd(c, &s) - Append Character to String
;Args: A = Character to Append
; X,Y = Pointer to String
;Sets: SRCPTR, SRCPTR+1 - Pointer to String
;strlen(&s) - Return Length of String
;Args: X,Y - Pointer to string
;Sets: SRCPTR = Pointer to source string
; N,Z based on return value of A
;Returns: A,Y = Length of string
; N,Z based on A
STRLEN: JSR SETSRC ;Initialize Source String
.LENLOP LDA (SRCPTR),Y ;Get Next Character
BEQ .LENEND ;If <> NUL
STRLEA: LDA (SRCPTR),Y ;Get Next Character
BEQ .LENFIN ;If <> NUL
INY ; Increment Index
BPL .LENLOP ; and Loop if < 128
.LENEND TYA ;Transfer Index to Accumulator
.RETORA ORA #0 ;Set N and Z flags
BPL STRLEA ; and Loop if < 128
.LENFIN TYA ;Transfer Index to Accumulator
RTS ;and Return
;strcpb(&s) - Copy String to System Buffer
;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
@ -113,14 +96,15 @@ STRCPB: JSR SETSRC ;Set Source Pointer to String Address
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to source string
; TEMP3 = Length of source prior to concatenation
;Affects: C,N,Z
; N,Z based on return value of A
;Affects: C
;Returns: A,Y = Total length of concatenated string
STRCAT: JSR SETSRC ;Initialize Source String
.CATLOP LDA (DSTPTR),Y ;Find end of Destination String
BEQ .CATEND ;
BEQ .CATFIN ;
INY ;
BPL .CATLOP ;
.CATEND STY TEMP3 ;Subtract Destination String Length
.CATFIN STY TEMP3 ;Subtract Destination String Length
LDA SRCPTR ; from Source String Pointer
SEC
SBC TEMP3
@ -134,25 +118,25 @@ STRCAT: JSR SETSRC ;Initialize Source String
;Requires: DSTPTR - Pointer to destination string
;Args: X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to source string
;Affects: N,Z
; N,Z based on return value of A
;Returns: A,Y = Number of characters copied
STRCPA: LDY #0 ;Alternate entry point
BEQ .CPYLOP ;for when Source already set
STRCPY: JSR SETSRC ;Initialize Source String
.CPYLOP LDA (SRCPTR),Y ;Get Character from Source String
STA (DSTPTR),Y ;Copy to Destination String
BEQ .CPYEND ;If <> NUL
.CPYLOP LDA (SRCPTR),Y ;Get Character from Source String
STA (DSTPTR),Y ;Copy to Destination String
BEQ .CPYFIN ;If <> NUL
INY ; Increment Index
BPL .CPYLOP ; and Loop if < 128
.CPYEND TYA ;Transfer Index to Accumulator
.CPYFIN TYA ;Transfer Index to Accumulator
RTS ;and Return
;strcut(n, &s) - Copy from Position n to End of Source (into Destination)
;strcut(n, &s) - Copy from Position n to End of Source
;Requires: DSTPTR - Pointer to destination string
;Args: A = Starting position in start string
; X,Y = Pointer to source string
;Sets: SRCPTR = Pointer to specified position in source string
;Affects: N,Z
; N,Z based on return value of A
;Returns: A,Y = Length of copied string
STRCUT: JSR SETSRC ;Initialize Source String
CLC
@ -161,15 +145,15 @@ STRCUT: JSR SETSRC ;Initialize Source String
BCC .CPYLOP
INC SRCPTR+1
JMP .CPYLOP ;and Jump Into String Copy Loop
;strstr(&s) - Search for String (in Destination String)
;Requires: DSTPTR - Pointer to destination string
;Args: X,Y = Pointer to search string
;Sets: DSTPTR = Pointer to position in source string
; End of string if not found
; End of string if not found
; SRCPTR = Pointer to source string
; TEMP3 = Last position checked in destination string
;Affects: N,Z
; N,Z based on return value of A
;Returns: A = Position, C=1 if found
; A = $FF, C=0 if not found
; Y = Last position checked in source string
@ -178,11 +162,11 @@ STRSTR: JSR SETSRC ;Initialize Source String
.STRLOP LDY #$00; ;Initialize Compare Offset
LDA (DSTPTR),Y ;Get Start Character in Destination
BEQ .CLCFLS ;If NUL return $FF and Carry Clear
JSR .CMPLOP ;Jump into Compare Loop
BEQ .STREND ;If Not Equal
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
BEQ .STREND ; Return Current Position
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
@ -190,31 +174,32 @@ STRSTR: JSR SETSRC ;Initialize Source String
INC DSTPTR+1 ; If not End of Memory
BNE .STRLOP ; Loop
BEQ .CLCFLS ; Else return $FF and Carry Clear
.STREND SEC ;Else Set Carry
.STRFIN SEC ;Else Set Carry
LDA TEMP3 ; Load Position
RTS ; and Return
;strrch(c, &s) - Find Last Occurance Character in String
;strrch(c, &s) - Find Last Occurrence of Character in String
;Args: A = Character to look for
; X,Y = Pointer to string to search in
;Sets: SRCPTR = Pointer to string
; TEMP3 = Character being searched for
;Affects: Y,C,N,Z
;Returns: A,X = Position of last occurance in string
; N,Z based on return value of A
;Affects: Y,C
;Returns: A,X = Position of last occurrence in string
; $FF if not found
; Y = Length of String
STRRCH: JSR SETSRC ;Initialize Source String
STA TEMP3; ;Save Search Character (alternate entry point)
STA TEMP3 ;Save Search Character (alternate entry point)
LDX #$FF ;Initialize Position
.RCHLOP LDA (SRCPTR),Y ;Get Next Character
BEQ .RCHEND ;If NUL, Exit with Position
BEQ .RCHFIN ;If NUL, Exit with Position
CMP TEMP3 ;Compare Character
BNE .RCHNXT ;If Found
TYA ; Store Counter
TAX
.RCHNXT INY ;Increment Counter
BPL .RCHLOP ; and Loop if < 128
.RCHEND TXA ;Copy Position to Accumulater
.RCHFIN TXA ;Copy Position to Accumulater
RTS ; and Return
ENDSUBROUTINE

View File

@ -2,55 +2,69 @@
* string - String Handling Routines for C02 *
*********************************************/
/* Find Character in String *
* Args: c - Character to find *
* &s - String to search *
* Returns: c in s *
* $FF if not found */
char strchr();
/* Get Length of String *
* Args: s - string *
* Returns: Length of string */
char strlen();
/* Sets Destination String for *
* functions that use two strings *
* Args: &s - Destination string */
void strdst();
/* Concatenate Destination String to Source String *
* Args: &s - Source string *
* Sets: Destination string specified by strdst() *
* Sets: Destination string specified by setdst() *
* Returns: Total Length of concatenated string */
char strcat();
/* Find Character in String *
* Args: c - Character to find *
* s - String to search *
* Returns: Position in s *
* $FF if not found */
char strchr();
/* Compare Destination String against Source String *
* Args: &s - Source string *
* Uses: Destination string specified by strdst() *
* Returns: Result of comparison *
* -1 = Destination < Source *
* Uses: Destination string specified by setdst() *
* Returns: -1 = Destination < Source *
* 0 = Destination = Source *
* 1 = Destination > Source */
char strcmp();
/* Count Character in String *
* Args: c - Character to count *
* s - String to search *
* Returns: Occurences */
char strcnt();
/* Copy Source String to Destination String *
* Args: &s - Source string *
* Sets: Destination string specified by strdst() *
* Sets: Destination string specified by setdst() *
* Returns: Number of characters copied */
char strcpy();
/* Copy Part of Source String to Destination String *
* Args: n - Starting point in source string *
* &s - Source string *
* Sets: Destination string specified by strdst() *
* Uses: Destination string specified by setdst() *
* Returns: Number of characters copied */
char strcut();
/* Read Next Entry from String List *
* Args: &d - Destination string *
* Returns: char n - Number of characters in entry *
* int p - New value or srcptr */
char strget();
/* Get Length of String *
* Args: s - string *
* Returns: Length of string */
char strlen();
/* Find Last of Character in String *
* Args: c - Character to find *
* s - String to search *
* Returns: c in s *
* $FF if not found */
char strrch();
char strstr();
/* Find String in String *
* Args: &s - String to Find *
* Uses: String to search specified by strdst() *
* Sets: strdst() pointer to position of string *
* Uses: String to search specified by setdst() *
* Sets: setdst() pointer to position of string *
* End of string if not found *
* Returns: Position of s in Destination string *
* $FF if not found */