Tricks in BASIC

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

Re: Tricks in BASIC

Post by XavSnap »

To set or retrieve a binary value in memory:

To retrieve the D_file:
10 LET A=PEEK 16396+256*PEEK 16397

Code: Select all

1 REM [HEX: 2A,32,40,4E,23,46,C9 ]
10 RAND 16396
20 LET A=USR 16514

;------- TASM ASM mnemonics. -------
;------- ROM and RAM Symbols -------
RAM_SEED equ $4032

ORG 16514
Start:
	LD HL,(RAM_SEED) ; GET SEED
	LD C,(HL) 
	INC HL 
	LD B,(HL) 
	RET ; ==========================
.end
To POKE a decimal value:
10 POKE 16507,A-INT(A/256)*256
20 POKE 16508,INT((A/256)

Code: Select all

10 RAND A
20 POKE 16507,PEEK 16434
30 POKE 16508,PEEK 16435

Have fun.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

How to get a character code with a PRINT AT ?

Code: Select all

10 PRINT AT Y,X;
20 IF PEEK (PEEK 16398+256*PEEK 16399)<>0 THEN GOTO xxxx
This code peeks up the next character after the X,Y coordinates.

In ASM:

RAM_DF_CC equ $400E

ORG $4082 ; [@16514/@h4082]
Lb4082:
LD HL,(RAM_DF_CC) ; GET DF-CC (NEXT CHARACTER PRINTED LOCATION)
LD C,$00
INC HL
LD B,(HL)
RET ; ==========================

Code: Select all

     0 REM [HEX: 2A,0E,40,0E,00,23,46,C9 ]
     10 PRINT AT Y,X;
     20  IF USR 16514<>0 THEN GOTO xxxx
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

Booleans in BASIC:

Code: Select all

  5 LET FALSE=0
  7 LET TRUE=1
  9 LET B=0
  10 LET A=TRUE
  20 IF A>0 THEN LET B=B+1
  30 LET B=B+1*(A>0)
  35 LET B=B+(A>0)
  40 LET B=B+NOT NOT A
  45 PRINT "A IS ";("TRUE") AND A;("FALSE") AND NOT A,,,,
  50 PRINT "BOOL SUM=";B
  
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

"ON N GOTO 1250,2000,4500,6001,2000,4500,6001"

IF you can't use a GOTO N...

Example:
#Regular code:

Code: Select all

10 IF N=1 THEN GOTO 1250
20 IF N=2 THEN GOTO 2000
30 IF N=3 THEN GOTO 4500
40 IF N=4 THEN GOTO 6001
50 IF N=5 THEN GOTO 2000
60 IF N=6 THEN GOTO 4500
70 IF N=7 THEN GOTO 6001
80 STOP

Use this code:

Code: Select all

110 GOTO VAL"1250200045006001200045006001"(N*4-3 TO N*4)
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

Compess the D_file in 1,2 or 16k:
Set the RAM_TOP TO 1k or 2k:

ZX81: (1K)
POKE 16389,68
NEW

TS1000: (2K)
POKE 16389,72
NEW

Or in 16k BASIC (only 22 lines)

Code: Select all

1 PRINT "HELLO"
2 STOP
10 FOR A=1 TO 22
20 SCROLL
30 NEXT A
60 SAVE "P"
70 RUN
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

Hi,

In BASIC, after a FAST/SLOW process, an unsightly scrolling can occur when exiting the FAST mode.

To avoid this dropout of the screen frame, just add a PAUSE before the SLOW commande:

Code: Select all

10 FAST
(... your code..)
1000 PAUSE 0
1010 SLOW
Have fun.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
stroebeljc
Posts: 74
Joined: Thu Apr 23, 2020 6:02 am

Re: Tricks in BASIC

Post by stroebeljc »

XavSnap wrote: Sat Mar 30, 2024 4:18 am Hi,

In BASIC, after a FAST/SLOW process, an unsightly scrolling can occur when exiting the FAST mode.

To avoid this dropout of the screen frame, just add a PAUSE before the SLOW commande:

Code: Select all

10 FAST
(... your code..)
1000 PAUSE 0
1010 SLOW
Have fun.
...and don't forget to add this line if running on first editions ROMs:

Code: Select all

1005 POKE 16437,255
:D
John
stroebeljc
Posts: 74
Joined: Thu Apr 23, 2020 6:02 am

Re: Tricks in BASIC

Post by stroebeljc »

I love your suggestions, XavSnap.
You're such a trickster! :twisted:
John
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

10 LET E$="(32 blank chars)"
equal
10 DIM E$(32)

Don't set a string variable LET A$="ABCD" to use it one time...

Code: Select all

10  LET A$="ABCD"
(...)
100 FOR A=1 TO LEN A$
110 LET B=CODE A$(A)
120 NEXT A

Code: Select all

100 FOR A=1 TO 4
110 LET B=CODE "ABCD"(A)
120 NEXT A
Use integer flags in chars format to preserve memory:
A$="X(SPACE) (SPACE)X(SPACE)XXX"
(SPACE) = FALSE
"X" =TRUE
A$(1)=TRUE
A$(2)=FALSE

AND (BOOLEAN):
IF A$(1)+A$(2)="(SPACE)(SPACE)" THEN ... (=> IF A$(1)="(SPACE)" AND A$(2)="(SPACE)" THEN )
IF A$(1)+A$(8)="XX" THEN ...
IF CODE A$(1)*CODE A$(8) THEN ...

OR (BOOLEAN):
IF CODE A$(1) THEN GOSUB...
IF CODE A$(2) THEN GOSUB...


IF A$(1)>"(SPACE)" THEN PRINT "TRUE"
IF A$(1)="(SPACE)" THEN PRINT "FALSE"

Code: Select all

10 LET A$="X"
20 PRINT ("TRUE" AND CODE A$(1));("FALSE" AND NOT CODE A$(1))
Thanks John... ;)
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2062
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

Hi,

Many ways to read a value in a string:

Faster:

Code: Select all

100 LET A$="01234567890123456789"
110 PRINT AT 0,0;VAL A$(TO 2)
120 LET A$=A$(3 TO)
130 IF A$<>"" THEN GOTO 110
A$(n to n+1):

Code: Select all

100 LET A$="01234567890123456789"
110 FOR A=1 TO LEN A$ STEP 2
120 PRINT AT 0,0;VAL A$(A TO A+1)
130 NEXT A
A$(n to):

Code: Select all

100 LET A$="01234567890123456789"
110 FOR A=1 TO LEN A$ STEP 2
120 PRINT AT 0,0;VAL A$(A TO)(TO 2)
130 NEXT A
Get a fix string (length 2):

Code: Select all

50 DIM B$(2)
100 LET A$="01234567890123456789"
110 FOR A=1 TO LEN A$ STEP 2
115 LET B$=A$(A TO)
120 PRINT AT 0,0;VAL B$
130 NEXT A
Trimmed values:

Code: Select all

100 LET A$="01234567890123456789"
110 FOR A=1 TO LEN A$ STEP 2
120 PRINT AT 0,0;VAL (A$(A)+A$(A+1))
130 NEXT A
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Post Reply