Tricks in BASIC

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

Re: Tricks in BASIC

Post by iturbez » Tue Jul 20, 2010 10:52 am

XavSnap wrote:Step One ... the Basic.
Step Tow ... The ASM.
Step Tree ... Save the World !
I agree! :lol:

User avatar
XavSnap
Posts: 461
Joined: Sat May 10, 2008 3:23 pm

Re: Tricks in BASIC

Post by XavSnap » Thu Jul 22, 2010 1:01 pm

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 65 times

Shaun_B
Posts: 312
Joined: Wed Apr 22, 2009 9:22 am

Re: Tricks in BASIC

Post by Shaun_B » Thu Jan 02, 2014 7:23 pm

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 12:47 pm

Re: Tricks in BASIC

Post by AndrewH7 » Thu Oct 15, 2015 3:20 pm

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 12:47 pm

Re: Tricks in BASIC

Post by AndrewH7 » Sun Nov 22, 2015 9:58 pm

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: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Re: Tricks in BASIC

Post by stefano » Thu Nov 26, 2015 1:38 pm

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: 1045
Joined: Thu Jun 16, 2011 7:35 am
Location: Boxmeer

Re: Tricks in BASIC

Post by dr beep » Thu Nov 26, 2015 2:00 pm

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: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Re: Tricks in BASIC

Post by stefano » Thu Nov 26, 2015 2:57 pm

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: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Re: Tricks in BASIC

Post by stefano » Thu Nov 26, 2015 3:07 pm

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: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Re: Tricks in BASIC

Post by stefano » Thu Nov 26, 2015 3:51 pm

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests