05-12-2013, 06:26 PM
slenkar Wrote:In assembly how do you know where to put data?This is a... hard question... :| but here we go:
like say I wanted some arrays how would I know which address in RAM to place the variables?
Is it possible to access arrays created in zxbasic from assembly? how would I do that
Arrays are just labels. Create a simple program with a DIM array and compile it with --asm and check for the result. After that label, some meta-data follows it, and finally, array cells. The metadata is the number of cells per dimension followed by a Cell Size. For example:
Code:
DIM myarray(2, 3) as Ubyte
Code:
ZXBASIC_USER_DATA: ; User declared global variables start here
_myarray: ; Array LABEL
DEFW 0001h ; Number of dimensions minus 1, in this case 2 dimensions => 1
DEFW 0004h ; 2nd dimension size, that is 0, 1, 2, 3 => size 4
DEFB 01h ; Size of each cell. "As uByte " = 1
DEFB 00h ; Cell data (initialized to 0)
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
DEFB 00h
The first dimension is not taken into account. So, for the compiler, the important information is the 2nd dimension and above.
- The 1st number (Uinteger) is 0001h, and counts the total number of dimensions minus 1. So, in this case, myarray has 2 dimensions, so this number is 2 - 1 = 1.
- The 2nd number is the 2nd dimension "range size", that is from 0 TO 3 = size 4.
Array data starts at: ARRAY_LABEL + 3 + 2 * (n - 1), where n = Number of dimensions.
So in this case, the array data starts at:
_myarray + 3 + 2 * (2 - 1) = _myarray + 5