News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

BASIC array memory

Started by ivavilagu, 11:11, 18 November 21

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ivavilagu

10 dim a(100)
20 print @a(0)
30 m=2
40 print @a(0)
50 s=4
60 print @a(0)
70 n=8
80 print @a(0)
90 print @a(0)
100 print @a(0)



Result:
497
506
515
524
524
524


Why memory direction of array changes every new variable declaration?

Sykobee (Briggsy)

It sounds like the memory allocation for new variables results in existing variables being moved around. There is probably something that explains the runtime allocation of variables somewhere but I can't find it at the moment.


That might be why a lot of basic programs predefine all the variables up front, as memory copying isn't cheap.


BASIC itself starts at &170, and when the program runs the variables seem to be placed just after the end of the basic program. As you add to the basic program, the variables get higher addresses.


DIM a%(128):poke @a%(0)+offset,value:b$=peek(@a%(0)+offset) - I think this lets you access a byte[] of size 256 in BASIC which doesn't have this natively - useful if you are running out of memory for, e.g., screen maps, etc.

ivavilagu

Ok, but only moves memory with arrays.


It seems is better to declare all variables at beginning.

abalore

You are not checking the address of the array, but the address of the first element

eto

Quote from: abalore on 14:22, 18 November 21
You are not checking the address of the array, but the address of the first element


the address of the first element of the array is equal to the address of the array.

abalore

#5
Quote from: eto on 14:43, 18 November 21

the address of the first element of the array is equal to the address of the array.
No, there is an array definition, which probably doesn't change, and then there is the array data, which changes.
In fact, the single types are just tokenized into the BASIC code, and that's the reason they are not rearranged. The array data is put after all the tokenized code.

BASIC is interpreted, hence it runs in a single pass. So the interpreter organizes the memory in sequence as the program is running.

eto

Quote from: abalore on 14:46, 18 November 21No, there is an array definition, which probably doesn't change, and then there is the array data, which changes.

Ok, so how do I get the address of the array? I haven't found any documentation, so this is just based on my own experience with Basic. In my understanding, arrays of numbers are always a fixed, linear memory space, where each element occupies 2 (integer) or 5 bytes (floating point). At least I haven't seen a counter example yet.


Bread80

I have a bunch of stuff written up about this and ready to go into the wiki. I just need to get my email system to allow the wiki confirmation emails through <sigh> so I can upload it.


BASIC allocates memory as follows:
Tokenised code => Variables => Arrays => Free memory
(from low memory to high)


As you allocate variables and arrays (or, rather, as BASIC comes across them in your code) it allocates space for them. To allocate a new variable it moves up the /all/ the arrays and their data to make space.


The variables themselves, and DEF FNs and arrays, are stored as bunch of linked lists. 26 for variables (one list for each possible first letter), one for DEF FNs, and for arrays, a list each for strings, integers and reals.


The @ operator gives you the address of the /data/ for the variable (or array element). The data 'header' is stored immediately before that address, but walking it backwards in not easy (or even possible). It includes the variable name/array (variable length) and a word for each dimension (giving it's size), which is also, of course, variable.


To get the address of an array's (or variables) header block you'll probably need to walk the relevant list and match the name.

Bread80

I've added full descriptions of the variables, DEF FNs and arrays storage areas to my site http://bread80.com/2021/11/20/variables-def-fn-definitions-and-arrays-storage-in-amstrad-cpc-locomotive-basic/
There are links from there to the BASIC source code repository and thus the example programs.

Powered by SMFPacks Menu Editor Mod