Tricks in BASIC

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
User avatar
GCHarder
Posts: 477
Joined: Sat Dec 14, 2013 7:46 pm

Using USR

Post by GCHarder »

We're all familiar with using USR to RUN a machine code routine from BASIC.

The usual methods are...

10 RAND USR 16516
or
10 LET L=USR 16516

The first method messes up the RAND function, and unless your expecting a return value, the second
method wastes space by assigning L a value.

Also exactly what does that USR 16516 do?

Here's an alternate way to USR...

10 IF USR 16516 THEN REM SCROLL UP

Which calls the routine and lets you know what it does.

Also, always LD BC,0000 before returning from your MC routine. This can be a
very handy byte saving tip. For example instead of...

10 RAND USR 16516
20 LET X=10

USE

20 LET X=USR 16516+10

or

100 FOR N=USR 16516 TO 20
110 NEXT N

Regards;

Greg
dr beep
Posts: 2428
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Tricks in BASIC

Post by dr beep »

@Greg,

just use LIST USR nn and make BC larger than 10000 on return, LD B,40 or more is enough.
User avatar
XavSnap
Posts: 2193
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

https://archive.org/details/zx-appeal/Z ... 0/mode/2up
More POKES

Having been a ZX-81 computer enthusiast for a few years now. I have picked up many bits and bobs which have helped me on my journey through BASIC. I have compiled a list of some of these bits and bobs into the following list:

RAND USR 836
This is a loading function which loads your program and automatically breaks into it. To use the function, type in FAST and then RAND USR 836.

USR 3086
This function scrolls the screen and prints something at the same time. To use it in your program, type-in PRINT TAB USR 3086;"whatever the message is" or if you want to want it 5 spaces from the beginning of the line, PRINT TAB USR 836+5;"whatever the message is".

POKE 16389,68
If you have got a RAM-pack connected, and you wish to go into 1K Mode without disconnecting the RAM-pack, then you can lower RAM-TOP to 1K by typing in POKE 16389,68 and then NEW.

POKE 16389,128
If you are in 1K Mode, and you would like to get back to 16K Mode without losing your program, type in FAST and then POKE 16389,128. Now type in LIST and WAIT.

RAND USR 0
This function clears all memory including whatever is above RAMTOP. It is also a quick way of restoring RAMTOP to normal if you have lowered it.

POKE 16419,x
This function will LIST any line from 0 to 255. Just LIST the line that you want to view from le.g. LINE 17, and then type in POKE 16419,x where x is the line which you have just LISTed.

POKE 16418,0
This function will allow the use of the bottom two lines of the screen. Use the statement with a program, as it will not work after the program has been broken into or if it is not a program line or after the program has stopped. Do not INPUT or SCROLL in this mode, as the machine will crash. To get back into normal mode, type in: POKE 16418,2.

POKE 16510,0
If you have a machine code routine at line 1, and you do not wish it to be accidentally edited, type in POKE 16510,0 and line 1 will change to line 0. This line cannot be edited. If you want it changed back to line 1 again, type in: POKE 16510,1.
Last edited by XavSnap on Wed Oct 30, 2024 1:40 am, edited 1 time in total.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
Paul
Posts: 1602
Joined: Thu May 27, 2010 8:15 am
Location: Germanys west end

Re: Tricks in BASIC

Post by Paul »

RAND USR 0
Doesn't clear all RAM but just between 32768 and 16384. All RAM above the normal 16k RAM is ignored. Also all RAM in the 8-16K area is ignored.
In theory, there is no difference between theory and practice. But, in practice, there is.
David G
Posts: 632
Joined: Thu Jul 17, 2014 7:58 am
Location: 48 North

Re: Using USR

Post by David G »

GCHarder wrote: Sat Oct 12, 2024 8:17 pmHere's an alternate way to USR...

10 IF USR 16516 THEN REM SCROLL UP

Which calls the routine and lets you know what it does.
I like it!


How does it differ from
10 IF USR 16516 THEN REM

They appear to act the same when I try them
User avatar
1024MAK
Posts: 5526
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: Tricks in BASIC

Post by 1024MAK »

I think the point was that the programmer can write a comment on the same line. Nothing more. At least, that’s the way I read it...

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...
User avatar
GCHarder
Posts: 477
Joined: Sat Dec 14, 2013 7:46 pm

Menuing Routines

Post by GCHarder »

Menuing Routines

----------------------------------
Option 1: Beginner's version
----------------------------------

Code: Select all

    10 CLS
    20 PRINT "\
===================","\
   TEMPO OPTIONS","\
===================","\
   PLAY TUNE",,"\
   TEMPO CHANGE",,"\
   CHANNEL SELECT","\
   SAVE CHANGES",,"\
   RE-LOAD TUNE",,"\
   NEW TUNE",,"\
   QUIT",,"\
===================","\
 SELECT"
    30 LET I$=INKEY$
    40 IF I$="" THEN GOTO 30
    50 IF I$="P" THEN GOTO 100
    51 IF I$="T" THEN GOTO 200
    52 IF I$="C" THEN GOTO 700
    53 IF I$="S" THEN GOTO 300
    54 IF I$="R" THEN GOTO 400
    55 IF I$="N" THEN GOTO 500
    56 IF I$="Q" THEN GOTO 600
    60 GOTO 30
  1000 PRINT AT 16,5;"YOU SELECTED:";I$
  1010 PRINT AT 18,5;"    RUN SIZE:";PEEK 16404+256*PEEK 16405-16384
  1100 STOP
  
Run Size:1481 bytes

----------------------------------
Option 2: Calculated GOTO's
----------------------------------

Code: Select all

    10 CLS
    20 PRINT "\
===================","\
   TEMPO OPTIONS","\
===================","\
   PLAY TUNE",,"\
   TEMPO CHANGE",,"\
   CHANNEL SELECT","\
   SAVE CHANGES",,"\
   RE-LOAD TUNE",,"\
   NEW TUNE",,"\
   QUIT",,"\
===================","\
 SELECT"
    30 LET I$=INKEY$
    40 IF I$="" THEN GOTO 30
    50 GOTO 20+\
80*(I$="P")+\
180*(I$="T")+\
280*(I$="C")+\
380*(I$="S")+\
480*(I$="R")+\
580*(I$="N")+\
680*(I$="Q")
  1000 PRINT AT 16,5;"YOU SELECTED:";I$
  1010 PRINT AT 18,5;"    RUN SIZE:";PEEK 16404+256*PEEK 16405-16384
  1100 STOP
Run Size:1460 bytes

----------------------------------
Option 3: Uses M$ to convert INKEY$ to a GOTO value
----------------------------------

Key Press: ABCDEFGHIJKLMNOPQRSTUVWXYZ A=0, B=0, C=7, N=5, Q=6, etc

5 LET M$="00700000000005006432000000" For values>10 use A,B,C where A=10, B=11, C=12, etc

Code: Select all


     5 LET M$="00700000000005016432000000"
    10 CLS
    20 PRINT "\
===================","\
   TEMPO OPTIONS","\
===================","\
   PLAY TUNE",,"\
   TEMPO CHANGE",,"\
   CHANNEL SELECT","\
   SAVE CHANGES",,"\
   RE-LOAD TUNE",,"\
   NEW TUNE",,"\
   QUIT",,"\
===================","\
 SELECT"
    30 IF INKEY$="" THEN GOTO 30
    40 LET I=VAL M$(CODE INKEY$-37)
    50 IF NOT I THEN GOTO 30
    60 GOTO 100*I

  1000 PRINT AT 16,5;"YOU SELECTED:";I
  1010 PRINT AT 18,5;"    RUN SIZE:";PEEK 16404+256*PEEK 16405-16384
  1100 STOP

  
Run Size:1429 bytes

Also note that no matter how many options you add, up to 26, the routine will always
stay the same size.
==================================

Greg
User avatar
XavSnap
Posts: 2193
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

Hi Greg,
5 LET M$="00700000000005016432000000"
(...)
40 LET I=VAL M$(CODE INKEY$-37)
Just use:
40 LET I=VAL ("00700000000005016432000000"(CODE INKEY$-37))
Cap0000.jpeg
It avoid to set the M$ string and mirror it in the VARS. Less lines in BASIC is better !
(used one time in the listing!)

30 IF INKEY$="" THEN GOTO 30
30 GOTO 30+(INKEY$<>"")
Size: 1392... Not better.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
GCHarder
Posts: 477
Joined: Sat Dec 14, 2013 7:46 pm

Re: Tricks in BASIC

Post by GCHarder »

Thanks Xav, that's a neat trick. Now why didn't I think of that?

Regards;

Greg
User avatar
XavSnap
Posts: 2193
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: Tricks in BASIC

Post by XavSnap »

Sometimes I find programming tips for ZXBASIC, but it seems that the compilation of the text works, the program works, but it will be impossible to edit the line because when validating this line, BASIC gives an error.

That is why I am wary of programs assembled by this mode of reconstitution not in-line and without validation of edition:

30 IF INKEY$="" THEN GOTO 30
Is ok.

30 IF NOT (INKEY$) THEN GOTO 30
Working too !

But, it's an illegal function on the edition mode...

It's a good way to add a copy/edit protection, but it's impossible in real life, without using pokes.
Now why didn't I think of that?
The B/30 ERROR !
Many BASIC assume to generate a value 0 or "" to a new variable value.

The ZX81 had to find an initialized value in the VARS.

Only in case of A$=A$+"x", a A=A+1, A=NEWVAR or A$=N$
The variable is not set in the vars after the equal symbol !
In ZX BASIC programming, all values had to be set before use.
So it's due to a healthy programming habit!

:ugeek:
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Post Reply