I am playing with macros for getting some 2D arrays working, and i got something wrong when usin element 0 of array.
the code is this:
Great Cow BASIC (0.94 2015-08-05)
#chip mega328p, 16
create2Darray( my2Da, 8, 4 ) ' Create 2x4 Array called my2Da
set2Ddata( my2Da, 2, 4, 24 ) ' Set element 2,4 in array to 24
myvar = 0
get2Ddata( my2Da, 2, 4, myVar ) ' Get element 2,4 and put in myVar
do
loop
Macro create2Darray( name, size, dimY )
Dim name( size )
name(0) = dimY
End Macro
Macro set2Ddata( name, ordx, ordy, value )
name( (ordx-1)*name(0)+ordy ) = value
End Macro
Macro get2Ddata( name, ordx, ordy, var )
var = name( (ordx-1)*name(0)+ordy )
End Macro
I se that gcbasic creates an alias for element 0 and sets its value properly:
;Alias variables
MY2DA0 is not in my code and was generated by gcbasic and used in place of SYSMY2DA_0 only in the get macro.
Finally i got my code working properly this way:
Macro set2Ddata( name, ordx, ordy, value )
index = (ordx-1)*name(0)+ordy
name( index ) = value
End Macro
Macro get2Ddata( name, ordx, ordy, var )
index = (ordx-1)*name(0)+ordy
var = name( index )
End Macro
This way the variable MY2DA0 is not created and the alias SYSMY2DA_0 is properly used in both macros.
I know my code is a bit tricky but may be there is something in the compiler.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am playing with macros for getting some 2D arrays working, and i got something wrong when usin element 0 of array.
the code is this:
Great Cow BASIC (0.94 2015-08-05)
I se that gcbasic creates an alias for element 0 and sets its value properly:
;Alias variables
And uses it in the macro set2Ddata, for example the line in file.gcb:
generates this code in file.asm:
In the macro get2Ddata i use exactly the same calculations.
but the line in file.gcb:
gerenates this asm:
Then looking in the memory location of variables i see:
MY2DA0 is not in my code and was generated by gcbasic and used in place of SYSMY2DA_0 only in the get macro.
Finally i got my code working properly this way:
This way the variable MY2DA0 is not created and the alias SYSMY2DA_0 is properly used in both macros.
I know my code is a bit tricky but may be there is something in the compiler.
In GCBasic arrays are indexed from 1, there is no element zero.
The First element, zero, of the array stores the length of the array.
Cheers
Chris