From d4d3453b59babfbf7d89baa18853dd2cb1cf718b Mon Sep 17 00:00:00 2001 From: Curtis F Kaylor Date: Mon, 20 Sep 2021 20:56:07 -0400 Subject: [PATCH] Added module stringm --- doc/stringm.txt | 72 ++++++++++++++++++++++++++++ include/stringm.a02 | 114 ++++++++++++++++++++++++++++++++++++++++++++ include/stringm.h02 | 31 ++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 doc/stringm.txt create mode 100644 include/stringm.a02 create mode 100644 include/stringm.h02 diff --git a/doc/stringm.txt b/doc/stringm.txt new file mode 100644 index 0000000..cb5d67c --- /dev/null +++ b/doc/stringm.txt @@ -0,0 +1,72 @@ +String Manipulation Module for C02 + +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. + + + #include + #include + #include + +The following functions are defined: + + n = strapd(c, s); Appends character c to string s. + + Returns the new length of the string. + + Note: Calls strlen(), then writes c into the end + position and a 0 byte into the following position. + + n = straps(s); Appends a space to string s. + + Returns the new length of the string. + + Note: Executes strspd with arguments ' ' and s. + + n = strppd(c, s); Prepends character c to string s. + + Returns the new length of the string. + + Note: Calls strlen(), shifts the entire string one + character to the right and writes c to position 0. + + n = strpps(s); Prepends a space to string s. + + Returns the new length of the string. + + Note: Executes strppd with arguments ' ' and s. + + n = strpad(v,s); Right pads string s with spaces to length v. + + If v is less than the current length of the + string. the string is not changed. + + Returns the new length of the string. + + Note: Calls strlen(), and if the result is less + than v, appends the appropriate number of spaces. + + n = strlwr(s); Converts letters in string s to lowercase. + + Returns the length of the string. + + Note: Sets srcptr to &s, then iterates through + the string, calling tolowr() on each character, + + n = strupr(s); Converts letters in string s to uppercase. + + Returns the length of the string. + + Note: Sets srcptr to &s, then iterates through + the string, calling touppr() on each character, + +Note: This module expects the following functions to be defined + + setdst(s); Set destination pointer + setsrc(s); Set source pointer and initialize index + +along with the zero page variable pairs + + srcptr Source String Pointer + dstptr Destination String Pointer + diff --git a/include/stringm.a02 b/include/stringm.a02 new file mode 100644 index 0000000..2240710 --- /dev/null +++ b/include/stringm.a02 @@ -0,0 +1,114 @@ +; C02 module stringm.h02 assembly language subroutines +; Requires external routines SETSRC, TOLOWR, and TOUPPR +; Requires the following RAM locations be defined +; external zero page word SRCPTR +; and external bytes TEMP0 and TEMP1 + + SUBROUTINE STRINGM + +;straps(c, s) - Append space to string s +;Args: A = Character to be appended +; X,Y = Pointer to string +;Affects N,Z +;Returns A,Y = New String Length +STRAPS: LDA ' ' ;Set Append Character to Space + +;strapd(c, s) - Append character 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 .ERROR ;Return 255 if > 127 + LDA TEMP0 ;Restore Character to Append +.APDLOP STA (SRCPTR),Y ;Store at End of String + BEQ .RETLEN ;Exit if NUL + INY ;Increment Past New Character + LDA #0 ;Terminate String + STA (SRCPTR),Y + BEQ .RETLEN ;and Append to String + +;strpps(c, s) - Prepend space to string s +;Args: A = Character to be appended +; X,Y = Pointer to string +;Affects N,Z +;Returns A,Y = New String Length +STRPPS: LDA ' ' ;Set Append Character to Space + +;strppd(c, s) - Prepend Character to String +;Args: A = Character to be appended +; X,Y = Pointer to string +;Sets: N,Z +;Returns A = New String Length +STRPPD: STA TEMP0 ;Save Character to Append + JSR STRLEN ;Get Length of String + BMI .ERROR ;Return 255 if > 127 + INY ;Bump up string length + TYA ;Push string length onto stack + PHA +.PPDLOP DEY ;Copy Preceding Character + LDA (SRCPTR),Y + INY ;into Current 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 + RTS +.ERROR LDA #$FF ;Return Error Code FALSE + RTS + +;STRPAD(n, &s) - Pad or Truncate String +;Args: A - New Length of String +; X,Y - Pointer to string +;Sets: SRCPTR = Pointer to source string +;Returns: A,Y = Length of string +; N,Z based on A +STRPAD: STA TEMP0 ;Save New Length + JSR STRLEN ;Get Current Length of String + CMP TEMP0 + BCS .PDONE ;If Shorter than New Length + LDA #' ' +.PLOOP STA (SRCPTR),Y ; Fill with Spaces + INY + BMI .PDONE ; Until 128 or + CPY TEMP0 ; New Length is Reached + BNE .PLOOP +.PDONE LDA #0 ;Terminate String + STA (SRCPTR),Y + TYA ;and Return Length + RTS + +;STRLWR(&s) - Convert String to Lower Case +;Args: X,Y - Pointer to string +;Sets: SRCPTR = Pointer to source string +;Returns: A,Y = Length of string +; N,Z based on A +STRLWR: JSR SETSRC ;Initialize Source String +.LLOOP LDA (SRCPTR),Y ;Get Next Character + BEQ .RETLEN ;If <> NUL + JSR TOLOWR ; Convert to Lower Case + STA (SRCPTR),Y ; and Replace in String + INY ; Increment Index + BPL .LLOOP ; and Loop if < 128 + TYA ;Transfer Index to Accumulator + RTS ;and Return + +;STRUPR(&s) - Convert String to Lower Case +;Args: X,Y - Pointer to string +;Sets: SRCPTR = Pointer to source string +;Returns: A,Y = Length of string +; N,Z based on A +STRUPR: JSR SETSRC ;Initialize Source String +.ULOOP LDA (SRCPTR),Y ;Get Next Character + BEQ .RETLEN ;If <> NUL + JSR TOUPPR ; Convert to Lower Case + STA (SRCPTR),Y ; and Replace in String + INY ; Increment Index + BPL .ULOOP ; and Loop if < 128 +.RETLEN TYA ;Transfer Index to Accumulator + RTS ;and Return + + ENDSUBROUTINE diff --git a/include/stringm.h02 b/include/stringm.h02 new file mode 100644 index 0000000..76d9878 --- /dev/null +++ b/include/stringm.h02 @@ -0,0 +1,31 @@ +/************************************************** + * stringm - String Modification Routines for C02 * + **************************************************/ + +/* Append Character to String * + * Args: c - Character to append * + * s - String to append to * + * Returns: New length of string */ +char strapd(); + +/* Convert String to Lowercase * + * Args: int &s - String * + * Returns: char l - Length */ +char strlwr(); + +/* Pad or Truncate String * + * Args: char n - New Length * + * int &s - string * + * Returns: char l - Length */ +char strpad(); + +/* Prepend Character to String * + * Args: c - Character to prepend * + * s - String to prepend to * + * Returns: New length of string */ +char strppd(); + +/* Convert String to Uppercase * + * Args: int &s - String * + * Returns: char l - Length */ +char strupr();