Tricks in BASIC

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Tricks in BASIC

Post by stefano »

Ahh.. PI and "NOT PI".. that's the way to optimize!
The Spectrum has also "BIN" meaning zero as well, but it adds the whole mantissa, thus it is equivalent to '0'. Yet NOT PI is the only choice
AyreGuitar
Posts: 3
Joined: Sun Aug 18, 2019 4:34 pm

Re: Tricks in BASIC

Post by AyreGuitar »

Shaun_B wrote: Tue Jan 07, 2020 10:21 pm Here's a new version of the maze generator simulator in 5 BASIC lines :-)
InfiMaze2.png
Enjoy!

Regards,

Shaun.
It's possible to squeeze the maze generator into just one line using some USR calls in a PRINT statement like this:

Code: Select all

10 PRINT "@80@00@01@02@87@04@05@83@03@85@81@82@07@84@06@86"(RND*15+NOT PEEK USR (3085+(ABS(PEEK 16441-17)=16))*USR 3663)
USR 3085 does nothing, just returns, but...
USR 3086 does SCROLL, which is triggered when 16441 (S_POSN_X) = 33 (at start of program) or 1 (when about to run out of screen space)
USR 3663 is used to GOTO 0

Enjoy!
User avatar
BarryN
Posts: 151
Joined: Thu Nov 09, 2017 11:34 pm

Re: Tricks in BASIC

Post by BarryN »

AyreGuitar wrote: Wed Sep 22, 2021 8:06 pm
Shaun_B wrote: Tue Jan 07, 2020 10:21 pm Here's a new version of the maze generator simulator in 5 BASIC lines :-)

InfiMaze2.png

Enjoy!

Regards,

Shaun.
It's possible to squeeze the maze generator into just one line using some USR calls in a PRINT statement like this:

Code: Select all

10 PRINT "@80@00@01@02@87@04@05@83@03@85@81@82@07@84@06@86"(RND*15+NOT PEEK USR (3085+(ABS(PEEK 16441-17)=16))*USR 3663)
USR 3085 does nothing, just returns, but...
USR 3086 does SCROLL, which is triggered when 16441 (S_POSN_X) = 33 (at start of program) or 1 (when about to run out of screen space)
USR 3663 is used to GOTO 0

Enjoy!
How the %$^%#%$@^%#$@ do you type that in? How do you type an @?
AyreGuitar
Posts: 3
Joined: Sun Aug 18, 2019 4:34 pm

Re: Tricks in BASIC

Post by AyreGuitar »

Sorry, I was using the same convention I'd seen on this thread for using hexadecimal numbers for graphics characters, so "@80" is CHR$(128)
Here's a screenshot running on JSZeddy emulator for the actual listing:
MazeOneliner.png
The graphics string is the same as the original multi line version earlier in this thread.
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Tricks in BASIC

Post by dr beep »

Code: Select all

10 GOTO 10-8*(INKEY$="N")
Saves a line, this code does the same as

Code: Select all

10 IF INKEY$<>"N" THEN GOTO 10
11 GOTO 2
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Tricks in BASIC

Post by XavSnap »

Send datas from Basic to an ASM routine: (from Greg's AY sound driver)
Sound Cmd.jpg
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Tricks in BASIC

Post by XavSnap »

Retrieve the "BC", "B" and "C" value in BASIC from an USR function:

Code: Select all

1 REM [ASM= \
ORG 16514|\
LD BC,$0100|\
INC BC|\
INC BC|\
RET|\
]
10 PRINT AT 1,1;"BC= ";USR 16514
15 REM COMPUTE THE VALUE
20 LET R=USR 16514
30 LET B= INT (R/256)
40 LET C= R-(B*256)
50 PRINT AT 3,1;"B= ";B,"C= ";C
55 REM OR...
60 RAND USR 16514
70 PRINT AT 5,1;"BC= ";PEEK 16435*256+PEEK 16434
80 PRINT AT 7,1;"B= ";PEEK 16435,"C= ";PEEK 16434
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Tricks in BASIC

Post by XavSnap »

How do you save space?

"How can one devise ways of making the most economical use of memory"

MOST VERSIONS of Basic store keywords such
as If or Goto in the same way - as one-byte
codes, usually known as tokens. But the way
in which variables, line numbers and floating
point numbers are stored varies from machine
to machine. If you understand how your Basic
stores its program lines and variables you can
often find more economical ways of doing things.

Sinclair Basic, in particular, uses a rather
complicated method for storing floating point
numbers and considerable space savings can
sometimes be made by using character strings
instead. For example, on the ZX-81, the line
10 LET X =4
consumes 15 bytes while the alternative
10 LET X= VAL "4"
only takes up 12 bytes.

Similarly, on the Spectrum, numeric variables
consume less memory than numeric literals.
If you are short of space it is a good idea
to assign 0 and 1 to single letter variables
at the beginning of the program and substitute
these variables for the numbers 0 and I
wherever they occur alone in the program.

In a long program this technique can save
several hundred bytes, at least.

For example:
FOR n= L to L + L

where L= 1 saves 7 bytes over FOR n=1 to 2

These techniques are specific to Sinclair
Basic but it is easy enough to discover how
to exploit the quirks of other Basic
interpreters.

You can investigate how mach memory a line
consumes by using the memory-free function -
FRE (0) on the Vic and Oric, MEM on the Dragon.

Dragon and Vic owners, for example, will
discover that renumbering a program in steps
of one reduces space substantially - low
line numbers obviously consume less memory
than high ones. But on the BBC line numbers
both at the beginning of a line and after
Gotos or Gosubs take up the same amount of
memory whether they are large or small.
Yet another saving is to remove spaces
between keywords. The Sinclair Basic
interpreter does not register spaces although
it inserts them in the screen listing.

Other Basics, however, will allow almost all
the text on a program line to be packed together.

Finally, you may find that you are not short
of memory after all. Try running this two
line program:
10 GOSUB 20
20 GOSUB 10
You should get an Out of memory message.
The problem here is that when the interpreter
performs a Gosub or a Proc instruction it
stores the address it must return to on the
Basic stack - in RAM. When the program
returns from a subroutine the address is
removed from the stack. If your program
contains an endless loop of subroutine calls
without returning you will find the stack
gobbles up RAM very rapidly.

46 YOUR COMPUTER, AUGUST 1983
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
1024MAK
Posts: 5101
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Tricks in BASIC

Post by 1024MAK »

After running

Code: Select all

10 GOSUB 20
20 GOSUB 10 
it all gets rather fun!

The fun way to get back to normal, is not a power down/up, reset or NEW, but to keep typing RETURN [New Line] until you get a 0:0 report (the poor Zeddy may not even have enough free RAM to print more than ‘R’ :lol: .

Then enter

Code: Select all

10 RETURN
20 RETURN
and then enter RETURN [New Line]

:D

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
1024MAK
Posts: 5101
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Tricks in BASIC

Post by 1024MAK »

Given that programs often use the numbers 0 and 1 rather often, i’m a bit surprised that you don’t see this more often:

Code: Select all

1 LET O=NOT PI
2 LET I=NOT O
10 PRINT O,I
With the rest of the program using O and I instead of 0 and 1…

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
Post Reply