Using an Array Element as an Array Index
![JonnyMac](https://forums.parallax.com/uploads/userpics/922/n74EUX0AORWZS.jpg)
Given this kind of declaration:
... couldn't SX/B allow tmpIdx to be used as an index into tmpArray?
... where X is evaluated and moved into __PARAM1 -- this code seems to work:
My suggestion is that if the an array element can be used as an index into an array that it's part of -- knowing nothing about how the compiler validates and generates code I don't know if this is a reasonable request or not.
tmpArray VAR Byte (9) tmpIdx VAR tmpArray(8)
... couldn't SX/B allow tmpIdx to be used as an index into tmpArray?
tmpArray(tmpIdx) = X
... where X is evaluated and moved into __PARAM1 -- this code seems to work:
MOV FSR, #tmpArray ADD FSR, tmpIdx MOV IND, __PARAM1 MOV FSR, #__DEFAULT
My suggestion is that if the an array element can be used as an index into an array that it's part of -- knowing nothing about how the compiler validates and generates code I don't know if this is a reasonable request or not.
Comments
In this case idx2 is an element of another array.
I had simular thoughts expressed in the beta forum.
Here is what I came up with then:
Hi,
I know it is not supported to use an array variable as index into an array variable.
Example:
throws error INVALID NUMBER OF PARAMETERS.
But I think I figured out a way to make it happen. In fact, the solution is equal
to the way large arrays on the sx28 are implemented.
Here is the code that implements the above formula on the sx28:
Resume:
To get the address of an array element whose index·is also an array element
(in pseudo code: fsr = @Arr(index) )
It looks to me the restriction that an array index must be a normal variable
is easy to resolve (without using any __PARAMx variable).
Jonnymac,
Your last example could be done without using __param4 by doing
mov fsr,#idx2
mov fsr,ind ;your __param4 value
add fsr,#tmpArray
mov ind,__param1
The key point is to calculate fsr without using any temporary variables.
regards peter
·mov fsr,#idx
·mov w,#arr1
·add w,ind
·mov fsr,w
it is a little more exciting which the index of the nested array is variable to arr1[noparse]/noparse]arr2[noparse][[/noparse]i = 42 but still not impossible:
As in Peter's post if Arr1 or Arr2 span banks it is a little uglier ....
David
·
In theory you can expand to any level of indirection,
but that requires a change to the way sxb evaluates expressions.
Currently, you can have only one operator per line as in
; Attempting arr1[noparse]/noparse]arr2[noparse][[/noparse]i = val - arr1[noparse]/noparse]arr2[noparse][[/noparse]2
More complex expressions must be split over several lines.
What jonnymac proposed, and me too, is·to allow
an index at a fixed location, either a normal variable or an aliased
array·variable. Then all that needs to change is the calculation
of fsr. It requires no change on how expressions are evaluated.
If the index is a normal variable, use·the current way·of calculating fsr,
if the index is an array variable, use the proposed way·of calculating fsr.
The remaining generated code would remain the same.
regards peter
·
arr1[noparse]/noparse]arr2[i = x
and of course you are right - this is something not presently handled. I understand what you are proposing and I agree with your code and offered my simpler version for the easy case.
Knowing just how high-level you want a high-level language to be is always hard....
David [/i]