Tokenisations in BASIC

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
Fahnn
Microbot
Posts: 135
Joined: Sun Jan 27, 2019 7:56 pm
Location: Redcar, UK
Contact:

Tokenisations in BASIC

Post by Fahnn »

Back in the 1980s, like everyone else, I messed around with BASIC programming. After realising that you couldn't really write a decent arcade game in BASIC, I moved onto writing poor quality management simulations, where speed isn't really important.

Anyway, I got a bit obsessed with saving memory wherever possible, mainly by assigning variables to common numbers. My basics (pun intended) were always z=NOT PI for 0, o=SGN PI for 1 and t=o+o for 2. I didn't bother with 3 as INT PI could be used, but then I'd generally go on to define everything from 4 to 10 (fo, fi, si, se, ei, ni, te) and a few others (20 as tw=te*t, 25 as tf=fi^t, 100 as hu=te^t, etc.). Pretty standard stuff.

Now, I'm sure there are better and more efficient setup methods, but I wonder if there's stuff that I never knew about? One thing I hadn't thought of back then was using my existing variables to define values with PEEK (255 is PEEK 0, 175 is PEEK 1, 17 is PEEK 2 etc.). Although there's limited value in these, they can still help save a byte or two here and there. And given that I hadn't thought of that back then, I wonder if there are more that I missed? I'd be very interested to hear about any others that anyone might have.
User avatar
Stefan
Manic Miner
Posts: 822
Joined: Mon Nov 13, 2017 9:51 pm
Location: Belgium
Contact:

Re: Tokenisations in BASIC

Post by Stefan »

The reason these tokenisations are saving memory is that they are not having to store the integer with it's five byte number storage tagged on. If you are using PEEK, you also have the address that you are peeking, which then also has it's five byte number storage added.

And you really need to start with fee fi fo fum... :lol:
User avatar
djnzx48
Manic Miner
Posts: 731
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Tokenisations in BASIC

Post by djnzx48 »

I think he means using e.g. PEEK z, where z is already equal to zero, so the memory overhead is avoided.

If you haven't seen these already, there's a whole load of them listed in here. They're for the ZX81 though so some of the values may be different. One particular 'optimisation' that I thought was quite cool was using BIN on its own to represent zero, until I discovered that it still stores the five-byte value even when there are no digits! But I don't know of too many others besides the ones listed there.
User avatar
Fahnn
Microbot
Posts: 135
Joined: Sun Jan 27, 2019 7:56 pm
Location: Redcar, UK
Contact:

Re: Tokenisations in BASIC

Post by Fahnn »

djnzx48 wrote: Sun Mar 31, 2019 10:14 pm I think he means using e.g. PEEK z, where z is already equal to zero, so the memory overhead is avoided.
Yes, that's it exactly.
djnzx48 wrote: Sun Mar 31, 2019 10:14 pmIf you haven't seen these already, there's a whole load of them listed in here. They're for the ZX81 though so some of the values may be different. One particular 'optimisation' that I thought was quite cool was using BIN on its own to represent zero, until I discovered that it still stores the five-byte value even when there are no digits! But I don't know of too many others besides the ones listed there.
Ah, excellent, thanks for this, interesting reading! I have a dim recollection of using CODE " " for 32, come to think of it, but I should really see if the rest of the character set can be similarly used. It's only going to save a byte over e.g. VAL "32" etc. but still, it all helps. Plus there's some really interesting stuff in that article about using the trigonometry functions, which I'd never considered before. Going to try out some of these tonight!
Post Reply