Reducing Basic Program Size

bbc micro/electron/atom/risc os coding queries and routines
User avatar
Richard Russell
Posts: 1697
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: Reducing Basic Program Size

Post by Richard Russell » Wed Feb 12, 2020 2:05 pm

Rich Talbot-Watkins wrote:
Wed Feb 12, 2020 12:57 pm
Do you remember if the BBC BASIC specification required this to work, or whether it's just an undefined consequence of how BBC BASIC implements variable assignment (allocating a new variable and defaulting it to zero prior to evaluating the RHS)?
The 'specification' didn't go into that much detail, but it did imply the behaviour by calling for a good degree of compatibility with the Microsoft BASICs of the day. In those BASICs it was legitimate to write:

Code: Select all

      A = A + 1
(where A wasn't previously defined) because all numeric variables initially 'exist', with a value of zero. There is nothing comparable to a 'No such variable' error; the very first line of a program could be:

Code: Select all

      PRINT A
which would print zero, not raise an error as it would in BBC BASIC. This behaviour has survived to the present day in languages like Liberty BASIC in which all variables - even arrays with up to 10 elements - initially 'exist' but with values of zero or an empty string as appropriate.

In practice it's extremely useful to have a way of forcing a variable to exist if it doesn't, but not changing its value if it does (for example when releasing resources in a 'cleanup' routine). I tend to use the compound += operator, introduced in BBC BASIC V, for this:

Code: Select all

      var% += 0
      IF var% THEN PROCrelease(var%)

User avatar
Rich Talbot-Watkins
Posts: 1679
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca
Contact:

Re: Reducing Basic Program Size

Post by Rich Talbot-Watkins » Wed Feb 12, 2020 6:18 pm

I guess I have the C++ orthodoxy of "declare all variables before using them" too ingrained!

User avatar
LordVaderUK
Posts: 208
Joined: Thu Jan 31, 2019 12:26 am
Location: Hampshire
Contact:

Re: Reducing Basic Program Size

Post by LordVaderUK » Mon Jun 01, 2020 1:07 pm

One way programs can use space unnecessarily is having two copies of things in memory: one copy in a program statement defining it, and another copy in its intended location in memory.
As you mentioned, the classic example is DATA statements - the data exists twice, in the DATA statements themselves, and in the variable heap once they've been read. A lot of DATA use can be rewitten to avoid this duplication, for example the classic "day of week" function can be written:
DEF FNmon(mon%)=MID$("JanFebMarAprMayJunJulAugSepOctNovDec",mon%*3-2,3)

See http://beebwiki.mdfs.net/Data_without_DATA
Wow this is a GREAT tip. I just managed to get rid of a 6 element string array with this little tip :-) Struggling to ensure my code fits into a BBC B. Thanks!
Loving my BBC Master 128

joachim
Posts: 181
Joined: Wed Jun 21, 2006 2:20 am
Location: Germany
Contact:

Re: Reducing Basic Program Size

Post by joachim » Mon Jun 01, 2020 3:47 pm

jgharston wrote:
Sun Dec 15, 2019 10:03 pm
julie_m wrote:
Sun Dec 15, 2019 6:59 pm
One way programs can use space unnecessarily is having two copies of things in memory: one copy in a program statement defining it, and another copy in its intended location in memory.
As you mentioned, the classic example is DATA statements - the data exists twice, in the DATA statements themselves, and in the variable heap once they've been read. A lot of DATA use can be rewitten to avoid this duplication, for example the classic "day of week" function can be written:
DEF FNmon(mon%)=MID$("JanFebMarAprMayJunJulAugSepOctNovDec",mon%*3-2,3)
Sphinx Adventure (IIRC) solves the problem in the opposite way: it stores the room descriptions in DATA statements but doesn't make an in-memory copy. How? Well, when you're in room L% and need a description, it does what amounts to:

Code: Select all

RESTORE L%: READ LOC$: PROCprint(LOC$)
That's right, the data for location number L% is stored in line L% (or the next valid data line after it).

cmorley
Posts: 1347
Joined: Sat Jul 30, 2016 8:11 pm
Location: Oxford
Contact:

Re: Reducing Basic Program Size

Post by cmorley » Mon Jun 01, 2020 4:44 pm

I use DATA & RESTORE without copying to other memory in RTool for the menu.

Code: Select all

REPEAT
PRINT"? ";:K$=GET$:IF K$>"`":K$=CHR$(ASCK$-32)
F$="unrecognised":A$=""
RESTORE
REPEAT:READ key$,func$,arg$,help$:IF key$=K$ THEN F$=func$:A$=arg$
UNTIL key$=""
PRINT K$;" ";
IF A$<>"" THEN INPUT LINE ""A$:A%=VAL(A$):IF A$=""THENF$="argument"
PRINT EVAL("FN"+F$)
UNTIL FALSE

DATA I,romlist,,List ROMs
DATA *,oscli,<cmd>,Issue user OS Command
DATA .,cat,,Catalogue filesystem (*.)
DATA D,drive,<drive>,Change drive
DATA L,load,<afsp>,Load ROM image from file
DATA S,save,<afsp>,Save ROM image to file
DATA R,read,<bank>,Read ROM image
DATA P,program,<bank>,Write ROM image
DATA V,verify,<bank>,Verify ROM image
DATA E,erase,<bank>,Erase complete 64K EEPROM
DATA K,lock,<bank>,Lock (write protect) SRAM
DATA U,unlock,<bank>,Unlock (write enable) SRAM
DATA Q,quit,,Quit
DATA B,reset,,Reboot computer
DATA ?,help,,Display this help
DATA ,,,

Post Reply

Return to “programming”