Useful Snippets
Posted: Mon 05 Jan, 2009 11:33 pm
This thread is (hopefully) going to contain useful snippets of BBC BASIC code. At the moment they'll appear cryptic as there's nothing to compare them to, but hopefully that will change when the BBC BASIC test version gets released.
I'll start the ball rolling with a pair of functions to change the case of strings as BBC BASIC does not have the UCASE$() or LCASE$() functions you may be familiar with from other dialects of BASIC.
String case conversion
Usage
The easiest way to use the code is to call either the FN_UCASE$() or FN_LCASE$() function.
You can convert multiple strings at a time by calling the assembly routine CS directly. Both movable and fixed strings are supported. The strings passed to the routine are overwritten.
The exact operation carried out (converting to uppercase or lowercase) depends on whether FN_UCASE$() or FN_LCASE$() was most recently called. To set the conversion call either of these BASIC functions first:
Remarks
You must not modify the variables CS, CS_MIN, CS_MAX or CS_OFF. Doing so is likely to crash the calculator.
I'll start the ball rolling with a pair of functions to change the case of strings as BBC BASIC does not have the UCASE$() or LCASE$() functions you may be familiar with from other dialects of BASIC.
String case conversion
Code: Select all
DIM CS 67:FOR Q%=0TO1:P%=CS
[OPT Q%*2
LD A,(IX+0):OR A:RET Z:LD C,A
.CS_LP LD L,(IX+2):LD H,(IX+3):LD A,(IX+1):CP 128:JR NZ,CS_NFIX:LD B,0:LD E,L:LD D,H
.CS_LEN LD A,(DE):CP 13:JR Z,CS_GO:INC B:INC DE:JR CS_LEN
.CS_NFIX LD B,(HL):INC HL:INC HL:LD E,(HL):INC HL:LD D,(HL):EX DE,HL:CP 129:JR NZ,CS_SKIP
.CS_GO LD A,(HL):.CS_MIN CP 97:JR C,CS_NX:.CS_MAX CP 123:JR NC,CS_NX:.CS_OFF SUB 32:LD (HL),A
.CS_NX INC HL:DJNZ CS_GO
.CS_SKIP INC IX:INC IX:INC IX:DEC C:RET Z:JR CS_LP:]
NEXT
DEFFN_UCASE$(s$)CS_MIN?1=97:CS_MAX?1=123:CS_OFF?1=32:CALLCS,s$:=s$
DEFFN_LCASE$(s$)CS_MIN?1=65:CS_MAX?1=91:CS_OFF?1=-32:CALLCS,s$:=s$
The easiest way to use the code is to call either the FN_UCASE$() or FN_LCASE$() function.
Code: Select all
PRINT FN_UCASE$("Hello, World!") : REM Prints "HELLO, WORLD!"
PRINT FN_LCASE$("BBC BASIC Z80") : REM Prints "bbc basic z80"
Code: Select all
A$="The"
B$="Quick"
DIM C 100:$C="Brown"
DIM D 100:$D="Fox"
CALL CS,A$,B$,$C,$D
Code: Select all
A$=FN_LCASE$(A$):CALL CS,B$,$C,$D
You must not modify the variables CS, CS_MIN, CS_MAX or CS_OFF. Doing so is likely to crash the calculator.