When does Basic variables move?

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
User avatar
mrtinb
Posts: 2004
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

When does Basic variables move?

Post by mrtinb »

Hi.

I just made a larger program i Basic, that saves machine code in the string variable C$.

I made the first line of my code:

Code: Select all

10 LET C$=" "
I assumed the variables were stored at memory location (VARS), in the order they were created in the program.

However after I later in my program do this:

Code: Select all

4444 LET C$=C$+CODE 45+CODE 85
Then the variable is no longer the first variable at location (VARS).

Can anybody explain when variables "stays" and when they "move"?
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
1024MAK
Posts: 5527
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: When does Basic variables move?

Post by 1024MAK »

I *think* when a string variable is added to, then a new string variable is created at the end of the variable space and then the earlier version is deleted with everything being shuffled along.

Changing the program (adding, editing or removing program lines) also shifts things in memory.

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
mrtinb
Posts: 2004
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: When does Basic variables move?

Post by mrtinb »

Maybe if I make a

Code: Select all

10 DIM C$(100)
instead, and then use another variable as pointer, to update the string, then the string will "stay in place". :)
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
siggi
Posts: 1043
Joined: Thu May 08, 2008 9:30 am
Location: Wetterau, Germany
Contact:

Re: When does Basic variables move?

Post by siggi »

Try as first line
DIM C$(number)

Then the total space is allocated and does not need to be moved.
But then you have to add characters by using an index. "+" will not work

Siggi
My ZX81 web-server: online since 2007, running since dec. 2020 using ZeddyNet hardware
http://zx81.ddns.net/ZxTeaM
User avatar
XavSnap
Posts: 2193
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: When does Basic variables move?

Post by XavSnap »

Hi,
Yes, the DIM command able to fixe the string variable location.
It's due to its length.
10 LET c$="xx"
20 LET c=100
30 LET c$="x" < the VARS will be move after the C value.
40 LET E=50 <will be add after C$
50 LET C$=C$+C$ <Will move C$ after E...

In case of DIM E$(4), 4 characters are reserved.
LET E$="xxxxxxxxxxxooooo" => LET E$="xxxx"

DIm E$(2)
LET A$="A"
LET B$="B"
LET E$=B$+A$="BA"
LET E$=A$+A$+A$+A$+A$+A$+B$="AA"

LET A$="ABC"
LET B$="BCD"
LET E$=A$ E$=="AB"
LET E$=A$(2 TO)="BC"
LET E$=A$(2)+B$(3)="BD"

If you don't change the string length, the BASIC program don't had to refresh and move the VARS and is faster.

In 1k, the vars move if the screen length is change (With the CLS or SCROLL command for example!).
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 2193
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.
Contact:

Re: When does Basic variables move?

Post by XavSnap »

Hi,

Hard to tracking a floating variable.
But it's done.


If you use an ASM routine, use the DEST and SEED BASIC variable:

Fill the SEED with DEST:

LD BC, DEST
RET

SEED=BC (RAND USR =BC)

To store the DEST value!
A LET will overwrite the DEST last pointer.

Code: Select all

1 REM [HEX:\
ED,4B,12,40,C9 ]

10 LET C$="HELLO"
15 LET D$="WORLD"
20 GOSUB 1000
30 LET C$=C$+C$
100 GOSUB 1000
110 LET C$=C$+D$
120 GOSUB 1000
130 LET C$="-THANKS"
140 GOSUB 1000
150 STOP

1000 LET C$=C$+CHR$ 128
1010 LET C$(1)=C$(1)
1020 RAND USR 16514
1030 LET A=PEEK 16434+PEEK (16435)*256
1040 FOR A=A TO 4E4
1050 LET B=PEEK A
1060 IF B<128 THEN PRINT CHR$(B);
1070 IF B<128 THEN NEXT A
1075 LET C$=C$( TO (LEN C$-1))
1080 PRINT
1100 RETURN
STRINGS.P
(1.33 KiB) Downloaded 110 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Post Reply