Author Topic: BASIC array memory  (Read 516 times)

0 Members and 1 Guest are viewing this topic.

Offline ivavilagu

  • CPC464
  • **
  • Posts: 10
  • Country: es
    • Awards
BASIC array memory
« on: 12:11, 18 November 21 »
Code: [Select]
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?
like
0
No reactions

Offline Sykobee (Briggsy)

  • 6128 Plus
  • ******
  • Posts: 850
  • Country: gb
    • Awards
Re: BASIC array memory
« Reply #1 on: 13:38, 18 November 21 »
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.
like
0
No reactions

Offline ivavilagu

  • CPC464
  • **
  • Posts: 10
  • Country: es
    • Awards
Re: BASIC array memory
« Reply #2 on: 14:13, 18 November 21 »
Ok, but only moves memory with arrays.


It seems is better to declare all variables at beginning.
like
0
No reactions

Offline abalore

  • CPC6128
  • ****
  • Posts: 211
  • Country: es
    • Awards
Re: BASIC array memory
« Reply #3 on: 15:22, 18 November 21 »
You are not checking the address of the array, but the address of the first element
like
0
No reactions

Offline eto

  • Supporter
  • 6128 Plus
  • *
  • Posts: 574
  • Country: de
    • Awards
Re: BASIC array memory
« Reply #4 on: 15:43, 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.
like
0
No reactions

Offline abalore

  • CPC6128
  • ****
  • Posts: 211
  • Country: es
    • Awards
Re: BASIC array memory
« Reply #5 on: 15:46, 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.
« Last Edit: 16:03, 18 November 21 by abalore »
like
0
No reactions

Offline eto

  • Supporter
  • 6128 Plus
  • *
  • Posts: 574
  • Country: de
    • Awards
Re: BASIC array memory
« Reply #6 on: 16:27, 18 November 21 »
No, 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.

like
0
No reactions

Offline Bread80

  • CPC464
  • **
  • Posts: 19
  • Country: england
    • Bread80.com
    • Awards
Re: BASIC array memory
« Reply #7 on: 17:53, 20 November 21 »
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.
like
2
Members reacted like:
Urusergi,Animalgril987,

Offline Bread80

  • CPC464
  • **
  • Posts: 19
  • Country: england
    • Bread80.com
    • Awards
Re: BASIC array memory
« Reply #8 on: 00:48, 21 November 21 »
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.
like
1
Members reacted like:
Sykobee (Briggsy),