Page 1 of 1

When does Basic variables move?

Posted: Tue Feb 25, 2025 4:28 pm
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"?

Re: When does Basic variables move?

Posted: Tue Feb 25, 2025 4:43 pm
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

Re: When does Basic variables move?

Posted: Tue Feb 25, 2025 5:06 pm
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". :)

Re: When does Basic variables move?

Posted: Tue Feb 25, 2025 5:09 pm
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

Re: When does Basic variables move?

Posted: Tue Feb 25, 2025 7:12 pm
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!).

Re: When does Basic variables move?

Posted: Wed Feb 26, 2025 8:55 pm
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