BASIC 2 string arrays

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
User avatar
Lardo Boffin
Posts: 1308
Joined: Thu Aug 06, 2015 6:47 am
Contact:

BASIC 2 string arrays

Post by Lardo Boffin » Wed Mar 28, 2018 8:26 pm

Hello all

First, I assume I am using BASIC 2 - it is on an issue 7 model B!

Has anyone mapped out how string arrays are held in memory?
A quick test suggests that an empty string array holds 5 bytes per element. This was done by using DIM A%-1: PRINT A%-LOMEM on various sizes of array with nothing in them.
I assume that one byte will be the length of the string and two bytes will be a pointer to the next array element (just guessing at this stage) but no idea about the other two bytes unless it is the address of the previous element?

Also how do you determine the location of the array in memory? I may have need of being able to address it directly (or indirectly :lol:) at some point.

Thanks!
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Viglen twin 40/80 5.25" discs, Acorn 6502 coproc
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc
BBC Master, Datacentre + HDD, pi co-proc

crj
Posts: 834
Joined: Thu May 02, 2013 4:58 pm
Contact:

Re: BASIC 2 string arrays

Post by crj » Wed Mar 28, 2018 9:17 pm

Well, you can extract the address of individual elements of an array by using CALL with arguments. Take a look at page 186 of the User Guide.

Writing a trivial program:

Code: Select all

DIM C% 100
P%=C%
[
LDA &601: STA &70
LDA &602: STA &71
LDA &603: STA &74
RTS
]
!&70=0
...will then let you do stuff like:

Code: Select all

DIM X$(2)
CALL C%,X$(0) : P. ?&74,~!&70,~!!&70
X$(0)="Hello"
CALL C%,X$(0) : P. ?&74,~!&70,~!!&70
...to see what's going on.

It looks like a string array is stored as an array of what the User Guide refers to as "string information blocks". 4 bytes: address, length, allocated space.

Provided you didn't want to do anything advanced like make strings longer, that trick would let you mess about with an array of strings. Extremely hackily. Do you really need to do that? :-p

User avatar
jgharston
Posts: 3244
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: BASIC 2 string arrays

Post by jgharston » Wed Mar 28, 2018 10:25 pm

Lardo Boffin wrote:Has anyone mapped out how string arrays are held in memory?
A string array is just the same as any other array:
EQUW link ; link to next variable or 0
EQUS varname$ ; variable name without first character
EQUB 0 ; terminator of variable name
.here
EQUB arraydata-here ; offset to start of array data = 2*number_of_dimensions+1
EQUW dimension1
EQUW dimension2
;etc.
.arraydata
; array data follows from here
; array data is stored in the order of the rightmost dimension increasing fastest, eg:
; array(0,0)
; array(0,1)
; array(0,2)
; array(0,3)
; array(1,0)
; array(1,1)
; array(1,2)
; etc.
;
; string data is multiple entries of:
EQUW stringdata
EQUB allocation
EQUB length
;
with the string data in the heap as normal
(Edit: note, if none of the string elements have been set to anything, the string data block will be EQUW 0:EQUB 0:EQUB 0)
Lardo Boffin wrote:Also how do you determine the location of the array in memory? I may have need of being able to address it directly (or indirectly :lol:) at some point.
Use the AddressOfAVariable function:

Code: Select all

 DEFFNAddrOf_65:DIM A% 31:P%=A%
 [OPT2:LDA &601:STA &70:LDA &602:STA &71:LDY #0
 LDA &604:STA (&70),Y:INY:LDA &605:STA (&70),Y:INY
 LDA #0:STA (&70),Y:INY:STA (&70),Y:RTS:]:=A%
Set up with addrof%=FNAddrOf_65 and then CALL addrof%,variable,result% will set result% to the address of the variable variable. Eg:

Code: Select all

CALL addrof%,result%,num   	result% -> 5-byte floating point value
CALL addrof%,result%,num%   	result% -> 4-byte integer value
CALL addrof%,result%,str$   	result% -> string information block
CALL addrof%,result%,num%(0)   	result% -> 4-byte integer value of num%(0)
CALL addrof%,result%,str$(0)   	result% -> string information block of str$(0)
6502 BASIC doesn't understand the concept of array() so you can't get the address of the array information block, only the address of individual array elements. If you know how many dimensions your array has you can calculate the address of the array information block with addrof array(zeroth_element) minus (numberofdimensions*2+1), eg:
CALL addrof%,result%,str$(0):result%=result%-3:REM subtract (1 dim)*2+1 = 3
CALL addrof%,result%,str$(0,0):result%=result%-5:REM subtract (2 dims)*2+1 = 5

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
Lardo Boffin
Posts: 1308
Joined: Thu Aug 06, 2015 6:47 am
Contact:

Re: BASIC 2 string arrays

Post by Lardo Boffin » Thu Mar 29, 2018 8:20 am

Thanks.

I am not trying to do anything too complex here. Basically a number of data lists are loaded into arrays and then used as a static lookup list - find the matching string and then return its ID number (based on its position).

What I may want to do is use machine code to do the lookup as it can be fairly slow in BASIC. If it was going to be impossible to access the data in the array directly then I would have to build my own data structures. I suspect these would be even slower in BASIC meaning I would have to go straight to machine code and I am no where near that stage yet.
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Viglen twin 40/80 5.25" discs, Acorn 6502 coproc
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc
BBC Master, Datacentre + HDD, pi co-proc

User avatar
sweh
Posts: 1967
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: BASIC 2 string arrays

Post by sweh » Fri Mar 30, 2018 1:34 am

FWIW, the "BASIC ROM user guide" (OCR'd here viewtopic.php?t=13861) and "Advanced BASIC ROM user guide" ( viewtopic.php?f=42&t=13860 ) are invaluable resources on how BASIC works. It's been decades (literally) since I last read them so I don't recall if they answer this exact question, but if they don't then they should provide enough information to let you work out the answer.
Rgds
Stephen

Post Reply