Determine if a string is numeric: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) m (coming back later to reduce lag on my computer) |
Puppydrum64 (talk | contribs) |
||
Line 9: | Line 9: | ||
<br><br> |
<br><br> |
||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
For this program, a valid numeric string literal consists of only numbers (ascii $30-$39), up to one <i>leading</i> minus sign, and no more than one decimal point. Anything else, including the null string, is considered non-numeric. |
|||
Macros used: |
|||
<pre> |
|||
macro loadpair,regs,addr |
|||
lda #<\addr |
|||
sta \regs |
|||
lda #>addr |
|||
sta \regs+1 |
|||
endm |
|||
macro pushY |
|||
tya |
|||
pha |
|||
endm |
|||
macro popY |
|||
pla |
|||
tay |
|||
endm |
|||
</pre> |
|||
The code: |
|||
<lang 6502asm>*=$0801 |
|||
db $0E,$08,$0A,$00,$9E,$20,$28,$32,$30,$36,$34,$29,$00,$00,$00 ;required init code on commodore 64 floppy disks |
|||
*=$0810 |
|||
lda #$0e |
|||
jsr chrout ;required for my printing routine to work. |
|||
z_HL equ $02 |
|||
z_L equ $02 |
|||
z_H equ $03 |
|||
z_B equ $04 |
|||
loadpair z_HL,TestString0 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString1 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString2 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString3 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString4 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString5 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString6 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString7 |
|||
jsr isStringNumeric |
|||
loadpair z_HL,TestString8 |
|||
jsr isStringNumeric |
|||
rts ;return to basic |
|||
isStringNumeric: |
|||
; input: z_HL = source address |
|||
pushY |
|||
ldy #0 |
|||
sty z_B ;our tally for decimal points |
|||
checkFirstChar: |
|||
lda (z_HL),y |
|||
beq notNumeric ;a null string is not a valid number! |
|||
cmp #'-' |
|||
beq isNegative_OK |
|||
cmp #'.' |
|||
beq isFloat_OK |
|||
and #$30 |
|||
cmp #$30 |
|||
beq isNumeral_OK |
|||
;else, is not numeric |
|||
notNumeric: |
|||
popY |
|||
jsr PrintString_TextScreen ;prints what's already in z_HL |
|||
jsr NewLine |
|||
loadpair z_HL,isStringNumeric_Fail |
|||
jsr PrintString_TextScreen |
|||
jsr NewLine |
|||
jmp NewLine |
|||
;rts |
|||
isNegative_OK: |
|||
isNumeral_OK: |
|||
iny |
|||
jmp loop_isStringNumeric |
|||
isFloat_OK: |
|||
iny |
|||
inc z_B |
|||
loop_isStringNumeric: |
|||
lda (z_HL),y |
|||
beq Terminated_isStringNumeric |
|||
cmp #'.' |
|||
beq CheckIfDecimalAlreadyOccurred |
|||
and #$30 |
|||
cmp #$30 |
|||
bne notNumeric |
|||
loop_overhead_isStringNumeric: |
|||
iny |
|||
jmp loop_isStringNumeric |
|||
CheckIfDecimalAlreadyOccurred: |
|||
lda z_B |
|||
bne notNumeric |
|||
inc z_B |
|||
jmp loop_overhead_isStringNumeric |
|||
Terminated_isStringNumeric: |
|||
;if we got this far the string is numeric. |
|||
popY |
|||
jsr PrintString_TextScreen ;prints what's already in z_HL |
|||
jsr NewLine |
|||
loadpair z_HL,isStringNumeric_Pass |
|||
jsr PrintString_TextScreen |
|||
jsr NewLine |
|||
jmp NewLine |
|||
;rts |
|||
isStringNumeric_Pass: |
|||
db "IS NUMERIC",0 |
|||
isStringNumeric_Fail: |
|||
db "IS NOT NUMERIC",0 |
|||
TestString0: |
|||
db 0 |
|||
TestString1: |
|||
db "123",0 |
|||
TestString2: |
|||
db "-30",0 |
|||
TestString3: |
|||
db "123.45",0 |
|||
TestString4: |
|||
db "-123.45",0 |
|||
TestString5: |
|||
db "ABCDE",0 |
|||
TestString6: |
|||
db "-34-5",0 |
|||
TestString7: |
|||
db "1.000.000",0 |
|||
TestString8: |
|||
db ".23456",0</lang> |
|||
{{out}} |
|||
<pre> |
|||
ready. |
|||
load"*",8,1: |
|||
searching for * |
|||
loading |
|||
ready. |
|||
run |
|||
IS NOT NUMERIC |
|||
123 |
|||
IS NUMERIC |
|||
-30 |
|||
IS NUMERIC |
|||
123.45 |
|||
IS NUMERIC |
|||
-123.45 |
|||
IS NUMERIC |
|||
ABCDE |
|||
IS NOT NUMERIC |
|||
-34-5 |
|||
IS NOT NUMERIC |
|||
1.000.000 |
|||
IS NOT NUMERIC |
|||
.23456 |
|||
IS NUMERIC |
|||
ready. |
|||
</pre> |
|||
=={{header|8th}}== |
=={{header|8th}}== |
||
<lang Forth>: number? >n >kind ns:n n:= ;</lang> |
<lang Forth>: number? >n >kind ns:n n:= ;</lang> |