Tricks in BASIC

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
User avatar
iturbez
Posts: 154
Joined: Fri Dec 25, 2009 1:06 pm
Location: Spain

Re: Tricks in BASIC

Post by iturbez »

XavSnap wrote:Step One ... the Basic.
Step Tow ... The ASM.
Step Tree ... Save the World !
I agree! :lol:
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 »

Hi,

Ther's a mysterious ASM Code !

Basic:

Code: Select all

# DISPLAY EXEMPLE
# ><AVSNAP
# ®®®®®®®®®®®®®®®®®
# FOUND IN ORDI-5 n2
# PAGE 29
# AUTHOR :
# ERIC WEINSTEIN.
# ®®®®®®®®®®®®®®®®®

     1  REM [HEX:\
0E,21,3E,06,C6,02,47,CD,\
18,09,CD,9B,09,12,FD,34,\
3A,2A,0C,40,23,16,06,ED,\
B1,15,20,FB,54,5D,ED,B1,\
C3,5D,0A ]

     7 POKE 16536,6
     8 POKE 16517,6
    10 LET I=1
    50 DIM N$(50,16)
    55 FOR A=1 TO 50
    60 LET N$(A)=VAL A + "HELLO"+ VAL A
    70 NEXT A
    90 FOR I=1 TO 50
    96 RAND USR 16514
    98 PRINT N$(I);"(";I;")"
   100 NEXT I
  8999 STOP 
  9000 SAVE "AUTO-SCROLL"
ASM:

Code: Select all

#define ORG  .org       ; TASM cross-assembler definitions
ORG $4082
	LD C,$21 
	LD A,$06 
	ADD A,$02 
	LD B,A 
	CALL $0918 ; [LOC-ADDR]
	CALL $099B ; [ONE-SPACE]
	LD (DE),A 
	INC (IY+58) 
	LD HL,($400C) ; GET D-FILE
	INC HL 
	LD D,$06 
Lb4099:
	CPIR 
	DEC D 
	JR NZ, Lb4099 ; [$4099:16537]
	LD D,H 
	LD E,L 
	CPIR 
	JP $0A5D ; [RECLAIM-1]

.end
:?:
Attachments
Display.p
ASM display codes [auto-scroll] (ZX81)
(1.31 KiB) Downloaded 799 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: Tricks in BASIC

Post by Shaun_B »

As you may have noticed, I've been messing about with Sinclair BASIC again and thanks to this thread I've found some excellent tips.

For instance, you can negate some of the problems of it not allowing multiple statements like:

Code: Select all

10 CLS: FOR I=0 TO 10
...
20 NEXT I
by calling the routine in the BASIC, like

Code: Select all

10 FOR I= USR VAL "2602" TO 10
...
20 NEXT I
As luck would have it, USR 2602 (CLS) returns zero so it's valid in the FOR statement. Very handy indeed :-)

Any more tips?

Regards,

Shaun.
AndrewH7
Posts: 6
Joined: Thu Oct 15, 2015 1:47 pm

Re: Tricks in BASIC

Post by AndrewH7 »

NOT PI =0
SGN PI =1
INT PI =3

SAVE maybe included in program, then after loading from tape it carries on, so if you'd typed LET A=0 before then it'll still =0.
AndrewH7
Posts: 6
Joined: Thu Oct 15, 2015 1:47 pm

Re: Tricks in BASIC

Post by AndrewH7 »

I think NOT PI should = faster than PI-PI
& maybe CODE "$" faster than VAL "35"
LET A = A + A faster than A * 2
IF NOT A THEN faster than IF A <>0 THEN
0 = NOT PI
1 = SGN PI
3 = INT PI
Text within " " may include full words, press key for THEN & then other keys & delete THEN if not needed.
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Tricks in BASIC

Post by stefano »

On Spectrum I used sometimes also expressions like PI*PI, PI+PI, etc.
Considering that numbers occupy the visualization bytes plus 5 hidden bytes for the internal number encoding (mantissa etc..), it can save lots of memory, even if many of such combinations slow down the code.
Sometimes the "INT" rounding can be omitted.

One more idea:
USR VAL "3893" (pause) will act also as keyboard scanner reporting different codes depending on the 5-key group being pressed.
dr beep
Posts: 2059
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Tricks in BASIC

Post by dr beep »

stefano wrote:On Spectrum I used sometimes also expressions like PI*PI, PI+PI, etc.
Considering that numbers occupy the visualization bytes plus 5 hidden bytes for the internal number encoding (mantissa etc..), it can save lots of memory, even if many of such combinations slow down the code.
Sometimes the "INT" rounding can be omitted.

One more idea:
USR VAL "3893" (pause) will act also as keyboard scanner reporting different codes depending on the 5-key group being pressed.
On a ZX Spectrum you don't really need to save memory in BASIC. You have at least 16K where on a ZX81 you would have 1K.
Only reason on a ZX Spectrum might be to stay in BASIC under 24064 to start machinecode on a 256-border.
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Tricks in BASIC

Post by stefano »

more interesting constants /hints:

-1 = COS PI
255 = PEEK PI
36.5 = PI**PI (approx.)
1.5 = LN PI**PI (approx)., but VAL "1.5" is just 2 bytes longer

Another interesting trick could be:
10 RAND PI
20 PRINT EXP EXP RND
30 GOTO 20
..and take benefit of the fixed sequence ;)
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Tricks in BASIC

Post by stefano »

Memory on a Spectrum: yes and no.. the real memory availability on the 16K model was much lower, I'd say 7/8K. The 16K rampack on a ZX81 had a way wider space for the BASIC programs.

The Supercode II tools for the Spectrum included compression routines able to remove comments and compress number encoding.. I don't remember them all but one of them was simply zeroing all the visible part of the number, thus preserving the program speed, obfuscating the program a little and gaining few bits of pragram size.
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Tricks in BASIC

Post by stefano »

A little more, perhaps useful in trigonometry, graphics, etc

360 (approx) = EXP EXP SQR PI
5.5 (approx) = LN PEEK PI
Post Reply