ZX81 strings

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
SuperGlob
Posts: 5
Joined: Wed Dec 14, 2022 4:05 am

ZX81 strings

Post by SuperGlob »

Hi everyone!

I have a, perhaps somewhat technical question about how the zx81 handles strings. In particular, how does it deal with memory allocation? For example, something like:

LET A$ = "Hello "
LET B$ = "ZX81"
LET A$ = A$ + "World!"

If you look at the variables in memory, you'd first see "Hello" defined, followed by zx81. When you redefined A$ to be 'Hello World" it moves the everything up, so "ZX81" becomes the first variable followed by "Hello World".

So, does that mean that every time a variable is redefined the *entire* table is rebuilt?

Thanks!
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: ZX81 strings

Post by XavSnap »

Hi SuperGlob,

No, only in cas of this kind of string variable.

In fact, there's two kinds of string variables...
1) DIM A$(10) , is a value with a fix length of 10 spaces. A$="xx" >>> ( LEN(A$)=10 ) (static VARS)
2) LET A$="xxx" , is a variable string length. A$="xx" >>> ( LEN(A$)=2 ) (updated VARS)

If you use the first-string case, the VARS memory won't be moved.
The second case, will change the string length, and will be updated in the top of VARS.

If you had to target the VARS memory, you had to keep the DEST system value:

Code: Select all

5 REM xx
10 LET A$="HELLO"
20 GOSUB 1000
30 LET B$=" WORLD"
40 GOSUB 1000
50 LET A$=A$+B$
55 LET A$(1)=A$(1)
60 GOSUB 1000
500 STOP

1000 POKE 16514,PEEK 16402
1010 POKE 16515,PEEK 16403
1020 LET A= PEEK 16514+256*PEEK 16515
1030 PRINT A
1040 RETURN
Never use "LET A=PEEK 16402+256*PEEK 16403" !
The DEST value will point to the "A" numeric value in the VARS.
POKE will freeze the last pointed variable.

LET A$(1)=A$(1) is use to point to the first character, where A$(1)="H"="H"
LET A$(5)=A$(5) >>> "O" memory offset.

If DIM is used to get a fix string, DIM A$(10) LET A$(1)="HELLO" >>> A$="H........."
To fill the value, use LET A$(1 TO)="HELLO">>> A$="HELLO....." LEN 10 characters. ("HELLO"+5 spaces)

LET A$(1 TO)="HELLO" ("HELLO"+5 spaces)
Or
LET A$="HELLO" >>> will trim the value to 10 characters too! ("HELLO"+5 spaces)

LET A$(3 TO)="HELLO" (2 space+"HELLO"+3 spaces)

Have Fun.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
SuperGlob
Posts: 5
Joined: Wed Dec 14, 2022 4:05 am

Re: ZX81 strings

Post by SuperGlob »

Thanks for the answer!

Second follow-up question. Do lines store pointers to variables or the actual name? If it's the former, you'd have to rebuilt all the pointers if you move / resize a string. If it's the latter, both space and time increase. A 'space-time' continuum if you will :-)

FWIW, I'm contemplating rewriting ZX81 basic in C to make it easier to read / port to other systems. Most of it is pretty straight-forward and you'd save (some) space by getting rid of the display file stuff.

If anyone's done this before, I'd love to have a look at it btw!
User avatar
mrtinb
Posts: 1911
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: ZX81 strings

Post by mrtinb »

It’s not a pointer. It stores the variable name. Read chapter 27 in the manual.
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: ZX81 strings

Post by XavSnap »

Yes, Martin's true.

The variable pointer isn't enclosed in the Basic line.
Only the numerics values are encoded in the ZXBASIC.
The BASIC monitor (BIOS) retrieves the variable name and scan the VARS to find the same variable name by variables jumps.

:roll:

1) Can't help in "C" language, just in BASIC.
2) Rewrite the holy BASIC ROM will lose the previous programs compatibility...
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
SuperGlob
Posts: 5
Joined: Wed Dec 14, 2022 4:05 am

Re: ZX81 strings

Post by SuperGlob »

Yeah, I figured as much. While caching variables in a list wouldn't be that hard, it'd kinda get hairy once you start shuffling around and you need to fix all of them up. Hence my initial question :-)

I loved / love the fact that it's really hard to mess up and that lines are checked before you enter them. This early form of Intellisense made it great for people to at least get the syntax right. You'd still be able to mess up but not really on syntax.

Thanks for the clarification!
Post Reply