Basic optimized : Keep it in memory !

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Basic optimized : Keep it in memory !

Post by XavSnap »

... Save zx81's memory ... it'had only 16kb...

If you had to code a big basic program, you had preserve all bytes you can.

In basic,

For example:
10 goto 1

This line Take
4 bytes : used by the line Header,
+ 1 byte : for the goto command.
+ 1 byte : for the numeric string : "1"
+ 5 bytes : use by the system to decode the numeric value.
= 11 bytes. (i don't add the <end of line> tag (128/76h)!)

To improve you code, you had to avoid numerics encoding.
Like this
10 goto PI/PI
4 : header (can't be changed : 2bytes for the line number, 2bytes for the line lenght.)
+ 1byte : for goto command.
+ 1byte : PI
+ 1byte : /
+ 1byte : PI
= 8 bytes. (3bytes saved!)

Now, If you use a constant:
5 LET I=PI/PI
10 goto I
4 : header (can't be changed : 2bytes for the line number, 2bytes for the line lenght.)
+ 1byte : for goto command.
+ 1byte : I
= 6 bytes. (5bytes saved! in the Goto line)

Constants examples:

A=0 =PI-PI
B=1 =PI/PI
C=2 =B+B
D=3 =C+B or int PI
E=4 =C*C

In case of numbers between 0 and 255,
you can use the "code" function, but most of chars values can't be typed on the keyboard!
67 to 127, and 230 to 255! (you had to have a "K" cursor in Basic!)

Juste type:
10 goto val"67"

In case of "1000" or "2000", you can use the exponant tag!
10 goto val"1E3" or val"2E3"

You can use the PEEK function in the rom PEEK PI=255 or PEEK+constant...

You can also save memory if you type a single line...

10 IF A>0 then PRINT"MORE" (16bytes+5bytes numerics)
20 IF A=0 then PRINT"EQUAL" (17bytes+5bytes numerics)+header*4+EOL*1
30 IF A<0 then PRINT"LESS" (16bytes+5bytes numerics) +header*4+EOL*1
= 64 bytes+8*header+2*EOL.
=> 74 bytes.

10 PRINT "MORE" AND A>0;"LESS" AND A<0;"EQUAL" AND A=0 (34bytes+3*5bytes numerics)
= 49 bytes.
**** 25 bytes saved ****

if, O=PI-PI
10 PRINT "MORE" AND A>O;"LESS" AND A<O;"EQUAL" AND A=O (34bytes)
=34Bytes.
**** 40 bytes saved ****
Juste for 1 line!

You can alsa try to use the XuR Basic optimizer, but it's hard to change you program with "CODE" funtions...
Your program must be finished to use it!
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
siggi
Posts: 990
Joined: Thu May 08, 2008 9:30 am
Location: Wetterau, Germany
Contact:

Re: Basic optimized : Keep it in memory !

Post by siggi »

AFAIK there is a "cruncher" program, which does some of these optimations.

Siggi
My ZX81 web-server: online since 2007, running since dec. 2020 using ZeddyNet hardware
http://zx81.ddns.net/ZxTeaM
User avatar
Factor6
Posts: 11
Joined: Wed Nov 11, 2009 9:27 pm
Location: Czech Republic

Re: Basic optimized : Keep it in memory !

Post by Factor6 »

Hi, these tricks are surely perfect, but when typing a prog on 1K machine you have to use regular numbers to get some memory for editing the prog. Also, using these tricks rapidly slows down the program's run. If typing something time depending (loops making screen effects or animations) it's better to use normal numbers.

F6
**
Sinclair ZX81/video out/ZX 16K RAM/LCD LG M1721A
Post Reply