Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assembly question
#2
slenkar Wrote:In assembly how do you know where to put data?

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
This is a... hard question... :| but here we go:
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
Will generate the following ASM:
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 real size of this array is DIM myarray(0..2, 0..3) => 3 x 4 = 12 cells. Each cell is a byte, so 12 bytes.
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.
You can access this data either calling the array subroutine ZX Basic uses (it's already linked with your program if you use any array), or calculate an array cell address on your own, and this is easy:
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
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)