summaryrefslogtreecommitdiff
path: root/programs/utils.s
diff options
context:
space:
mode:
Diffstat (limited to 'programs/utils.s')
-rw-r--r--programs/utils.s257
1 files changed, 257 insertions, 0 deletions
diff --git a/programs/utils.s b/programs/utils.s
index 6032a47..d8d4aa2 100644
--- a/programs/utils.s
+++ b/programs/utils.s
@@ -5,6 +5,16 @@
hex_char:
.byte "0123456789ABCDEF"
+; Compare, and return table for pre-tokens.
+ptok_tab:
+ .byte ".@:=+-><(),xy\"\'#;$%"
+; Compare, and return table for isdelm.
+dtab:
+ .byte "\n,\"\' "
+; Compare, and return table for isdelm2.
+dtab2:
+ .byte "),.+<>-=;\n"
+
.org utils
print_hi:
@@ -134,3 +144,250 @@ charcpy:
sta strbuf, x ; Save it in the string buffer.
inc idx3 ; Increment the string index.
rts ; End of charcpy.
+
+
+strlen:
+ ldb #1 ; Set the second pointer
+ jsr set_ptr ; to the passed pointer.
+ deb ; Reset B.
+ tba ; Reset A.
+ tax ; Reset X.
+ phy #2 ; Preserve Y.
+ txy ; Reset Y.
+@loop:
+ lda (ptr2), y ; Are we at the end of the string?
+ beq @end ; Yes, so we're done.
+ iny ; No, so increment the index.
+ jmp @loop ; Keep looping.
+@end:
+ tyx ; Return the length in X.
+ ply #2 ; Get the preserved value back.
+ rts ; End of strlen.
+
+
+strcmp:
+ ldb #1 ; Set the second pointer
+ jsr set_ptr ; to the passed pointer.
+ deb ; Reset B.
+ tba ; Reset A.
+ phy #2 ; Preserve Y.
+ tay ; Reset Y.
+@loop:
+ ldb #0 ; Set the islong flag to false.
+ lda (ptr), y ; Are we at the end of the first string?
+ beq cmpr ; Yes, so check if we're too short, or too long.
+ ldb #1 ; No, so set the islong flag to true.
+ cmp (ptr2), y ; Is the character of both strings, the same?
+ bne cmpr ; No, so check if we're too short, or too long.
+ iny ; Yes, so increment the index.
+ jmp @loop ; Keep looping.
+
+strcasecmp:
+ ldb #1 ; Set the second pointer
+ jsr set_ptr ; to the passed pointer.
+ deb ; Reset B.
+ tba ; Reset A.
+ phy #2 ; Preserve Y.
+ tay ; Reset Y.
+@loop:
+ ldb #0 ; Set the islong flag to false.
+ lda (ptr), y ; Are we at the end of the first string?
+ beq cmpr ; Yes, so check if we're too short, or too long.
+ ldb #1 ; No, so set the islong flag to true.
+ jsr tolower ; Convert the character of string 1 to lowercase.
+ phb #1 ; Preserve the islong flag.
+ pha #1 ; Preserve the converted character.
+ lda (ptr2), y ; Get the character of the second string.
+ jsr tolower ; Convert the character of string 2 to lowercase.
+ tab ; Place it in B.
+ pla #1 ; Get the character of string 1 back.
+ cab ; Is the character of both strings, the same?
+ plb #1 ; Get the islong flag back.
+ bne cmpr ; No, so check if we're too short, or too long.
+ iny ; Yes, so increment the index.
+ jmp @loop ; Keep looping.
+
+cmpr:
+ lda (ptr2), y ; Are we at the end of the second string?
+ beq @islong ; Yes, so check the islong flag.
+@isshort:
+ lda (ptr), y ; No, but are we at the end of the first string?
+ beq @short ; Yes, so return -1.
+@islong:
+ cpb #1 ; Is the islong flag true?
+ bne @equ ; No, so return 0.
+@long:
+ lda #1 ; Yes, so return 1.
+ jmp @end ; We are done.
+@equ:
+ lda #0 ; Return 0.
+ jmp @end ; We are done.
+@short:
+ lda #$FF ; Return -1.
+@end:
+ ply #2 ; Get the preserved value back.
+ rts ; End of strcmp.
+
+
+isdigit:
+ sec ; Prepare for a non carrying subtraction.
+ sbc #'0' ; Subtract $30 from the passed character.
+ and #$FF ; Make sure that we have only one byte.
+ cmp #10 ; Is the subtracted value, less than 10?
+ bcs @false ; No, so return false.
+@true:
+ lda #1 ; Yes, so return true.
+ jmp @end ; We are done.
+@false:
+ lda #0 ; Return false.
+@end:
+ rts ; End of isdigit.
+
+isxdigit:
+ pha #1 ; Preserve the character.
+ jsr isdigit ; Is this character, a decimal digit?
+ pla #1 ; Get the character back.
+ bne @true ; Yes, so return true.
+@alpha:
+ sec ; No, so prepare for a non carrying subtract.
+ ora #$20 ; Convert it to lowercase.
+ sbc #'a' ; Subtract $61 from the character.
+ and #$FF ; Make sure that we have only one byte.
+ cmp #6 ; Is the subtracted value, less than 6?
+ bcs @false ; No, so return false.
+@true:
+ lda #1 ; Yes, so return true.
+ jmp @end ; We are done.
+@false:
+ lda #0 ; Return false.
+@end:
+ rts ; End of isxdigit.
+
+
+isupper:
+ sec ; Prepare for a non carrying subtraction.
+ sbc #'A' ; Subtract $41 from the passed character.
+ jmp isletter ; Check if it's less than 26.
+islower:
+ sec ; Prepare for a non carrying subtraction.
+ sbc #'a' ; Subtract $61 from the passed character.
+isletter:
+ and #$FF ; Make sure that we have only one byte.
+ cmp #26 ; Is the subtracted value, less than 26?
+ bcs @false ; No, so return false.
+@true:
+ lda #1 ; Yes, so return true.
+ jmp @end ; We are done.
+@false:
+ lda #0 ; Return false.
+@end:
+ rts ; End of isletter.
+
+
+tolower:
+ pha #1 ; Preserve the character.
+ jsr isupper ; Is this character, an uppercase character?
+ pla #1 ; Get the character back.
+ beq @end ; No, so we're done.
+@lower:
+ ora #$20 ; Yes, so convert it to lowercase.
+@end:
+ rts ; End of tolower.
+
+
+toupper:
+ pha #1 ; Preserve the character.
+ jsr islower ; Is this character, a lowercase character?
+ pla #1 ; Get the character back.
+ beq @end ; No, so we're done.
+@upper:
+ and #$5F ; Yes, so convert it to uppercase.
+@end:
+ rts ; End of toupper.
+
+
+isdelm2:
+ ldx #0 ; Reset X.
+@loop:
+ ldb dtab2, x ; Get the compare value.
+ beq @other ; We hit the end of the table, so check for the others.
+ cab ; Are they the same?
+ beq @r1 ; Yes, so return 1.
+ inx ; No, so increment the table index.
+ jmp @loop ; Keep looping.
+@other:
+ ldx #0 ; Reset X.
+ cmp #0 ; Is this a null terminator?
+ beq @r1 ; Yes, so return 1.
+ cmp #'\t' ; No, but is it a tab?
+ beq @r2 ; Yes, so return 2.
+ cmp #' ' ; No, but is it a space?
+ beq @r2 ; Yes, so also return 2.
+@r0:
+ lda #0 ; Return 0.
+ rts ; End of isdelm2.
+@r1:
+ ldx #0 ; Reset X.
+ lda #1 ; Return 1.
+ rts ; End of isdelm2.
+@r2:
+ lda #2 ; Return 2.
+ rts ; End of isdelm2.
+
+
+isdelm:
+ ldx #0 ; Reset X.
+@loop:
+ ldb dtab, x ; Get the compare value.
+ beq @other ; We hit the end of the table, so check for the others.
+ cab ; Are they the same?
+ beq @rshft ; Yes, so return 1 << index.
+ inx ; No, so increment the table index.
+ jmp @loop ; Keep looping.
+@other:
+ ldx #0 ; Reset X.
+ cmp #0 ; Is this a null terminator?
+ beq @rshft ; Yes, so return 1.
+ ldx #4 ; No, so set the shift amount to 4.
+ cmp #'\t' ; Is this a tab?
+ beq @rshft ; Yes, so return 16.
+ ldx #0 ; No, so reset X.
+@r0:
+ lda #0 ; Return 0.
+ rts ; End of isdelm.
+@rshft:
+ stx a ; Save the shift value.
+ ldx #0 ; Reset X.
+ lda #1 ; Set up the bitshift.
+ lsl a ; Return 1 << X.
+ rts ; End of isdelm.
+
+
+get_ptok:
+ ldx #0 ; Reset X.
+ jsr tolower ; Conver the character to lowercase.
+@loop:
+ ldb ptok_tab, x ; Get the compare value.
+ beq @other ; We hit the end of the table, so check for the others.
+ cab ; Are they the same?
+ beq @rtab ; Yes, so return X.
+ inx ; No, so increment the table index.
+ jmp @loop ; Keep looping.
+@rtab:
+ txa ; Return X.
+ rts ; End of get_ptok.
+@other:
+ tab ; Preserve the character.
+ jsr isdigit ; Is this character a digit?
+ bne @rnum ; Yes, so return PTOK_NUM.
+ tba ; No, so get the character back.
+ jsr islower ; Is it an alphabetical character?
+ bne @ralph ; Yes, so return PTOK_ALPH.
+ lda #PTOK_OTHR ; No, so return PTOK_OTHR.
+ rts ; End of get_ptok.
+@rnum:
+ lda #PTOK_NUM ; Return PTOK_NUM.
+ rts ; End of get_ptok.
+@ralph:
+ lda #PTOK_ALPH ; Return PTOK_ALPH.
+ rts ; End of get_ptok.