Tricks in BASIC
Re: Tricks in BASIC
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
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
-
- Posts: 3
- Joined: Sun Aug 18, 2019 4:34 pm
Re: Tricks in BASIC
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 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!
Re: Tricks in BASIC
How the %$^%#%$@^%#$@ do you type that in? How do you type an @?AyreGuitar wrote: ↑Wed Sep 22, 2021 8:06 pmIt's possible to squeeze the maze generator into just one line using some USR calls in a PRINT statement like this:USR 3085 does nothing, just returns, but...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 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!
-
- Posts: 3
- Joined: Sun Aug 18, 2019 4:34 pm
Re: Tricks in BASIC
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: The graphics string is the same as the original multi line version earlier in this thread.
Here's a screenshot running on JSZeddy emulator for the actual listing: The graphics string is the same as the original multi line version earlier in this thread.
Re: Tricks in BASIC
Code: Select all
10 GOTO 10-8*(INKEY$="N")
Code: Select all
10 IF INKEY$<>"N" THEN GOTO 10
11 GOTO 2
Re: Tricks in BASIC
Send datas from Basic to an ASM routine: (from Greg's AY sound driver)
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Re: Tricks in BASIC
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)
Re: Tricks in BASIC
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)
- 1024MAK
- Posts: 5526
- Joined: Mon Sep 26, 2011 10:56 am
- Location: Looking forward to summer in Somerset, UK...
- Contact:
Re: Tricks in BASIC
After running
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’
.
Then enter
and then enter RETURN [New Line]
Mark
Code: Select all
10 GOSUB 20
20 GOSUB 10
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’

Then enter
Code: Select all
10 RETURN
20 RETURN

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Amp
Standby alert 
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Spring approaching...
ZX81 Chip Pin-outs
ZX81 Video Transistor Amp


There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb

Spring approaching...
- 1024MAK
- Posts: 5526
- Joined: Mon Sep 26, 2011 10:56 am
- Location: Looking forward to summer in Somerset, UK...
- Contact:
Re: Tricks in BASIC
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:
With the rest of the program using O and I instead of 0 and 1…
Mark
Code: Select all
1 LET O=NOT PI
2 LET I=NOT O
10 PRINT O,I
Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Amp
Standby alert 
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb
Spring approaching...
ZX81 Chip Pin-outs
ZX81 Video Transistor Amp


There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb

Spring approaching...