Passing data arrays - simple question
Hi Guys,
I just need to figure out how you pass arrays or pointers into an subroutine.
Greg
I just need to figure out how you pass arrays or pointers into an subroutine.
Greg
Comments
You represent the address of some array X with the notation @X. You can pass this to a subroutine. You can use the BYTE, WORD, or LONG syntax to get or set the value at a particular address. See the Propeller Manual for more details and examples.
I don't know why but I have the hardest time finding simple information for spin.· It's like having such a messy garage that it's easier to borrow your neihbors tools.
I tried SendData(@Data,n) to pass an address compiler didn't like the @ sign.· How do you get the data at that address once you pass it?
Thanks,
Greg
In your example, "Data" has to be the name of an array (or even a single variable). Probably that's why the compiler complained.
surely, it's not simple to find out, how it works, but then it turns out to be quite straight forward.
It took some time to see, that the address is a byte address, in spite of having a real 32-bit processor.
destination := type[noparse][[/noparse]source_address]
That means: you store the content of the memory pointed to by source_address to destination. Remember: source_address is a byte-address!!!
the "type" determines the type of data transfered.
the same is true if the destination is determined by a pointer
When transfering an element of an array, you do it this way:
You see, the offset is created by having the forth element of a long (destination) or by adding 16 to the first element of a LONG as source.
So there are many ways to have access, the most simple way is, to work always with absolute addresses. But that is not what is done today as the programming languages always try to hide, what's going on behind the scene. On a first glance, that makes programming easier, but in the end, as it seems so simple, we create over and over new code.
Hope, there is no error in this description. warranty void if used
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
cmapspublic3.ihmc.us:80/servlet/SBReadResourceServlet?rid=1181572927203_421963583_5511&partName=htmltext
Hello Rest Of The World
Hello Debris
Install a propeller and blow them away
Nice explanation.·
Greg
I have a list of variables in my main program dat section:
DAT
··· Var1·· Long 0
····Var2·· Long 0
···· ...
····Var20· Long 0
I send the base address pointer in a call to another object:
OBJ
· A : "anotherobject"
PUB Main
· A.dosomething(@Var1)
Now in my "anotherobject" I want to modify the 20 variables:
'anotherobject code
VAR
· BYTE· Var2_ptr
· ...
··BYTE· Var20_ptr
PUB dosomething(Var1_ptr)
· 'setup pointers to all variables in Main based on baseaddress provided
· Var2_ptr :=· Var1_ptr· +· 'some offset
· ...
· Var20_ptr := Var20_ptr + 'some offset
· 'Then I take these global variables in this object and use them in other routines to modify different combinations of these variables
So my question is:
The "some offset" value is a multiple of 1 if I had Byte values declared in the DAT section in Main, multiple of 2 if Word, and multiple of 4 if Long???
I know that if I weren't modifying the pointer, but was modifying the original variable directly it would be this:
newVar1 := Long[noparse][[/noparse]Var1_ptr][noparse][[/noparse]0] 'index of 0 off baseaddress
newVar2 := Long[noparse][[/noparse]Var1_ptr][noparse][[/noparse]1] 'index of 1 long off baseaddress
...
newVar20 := Long[noparse][[/noparse]Var1_ptr][noparse][[/noparse]19] 'index of 19 longs off baseaddress
And this index value is automatically incremented in the right multiples if I used BYTE,WORD, or LONG???
I know that I could use this method directly, but I wrote alot of code not realizing my limit of passing 15 variables to a subroutine call, so I needed to implement a list of variables using a base address.· If I use global variables in the 'other object' that is called, and these variables have the same pointer names I had before, I can just initialize these pointer variables at the begining of the subroutine call, all my remaining code would stay the same.· It comes down to understanding that using the Long[noparse][[/noparse]ptr][noparse][[/noparse]index] vs. byte[noparse][[/noparse]ptr][noparse][[/noparse]index] increments the index by 1 as you move through the list, but the·address of these variables are incremented by a multiple that corresponds to the data type.· Am I right here in my thinking, I couldn't find this spelled out in the manual?
Anyway, if you pass the address of the array or the address of the first variable (@variable1) to the "inner object" and have that value there as "address", then you can indeed refer to the variables as long[noparse][[/noparse] address ] or long[noparse][[/noparse] address + offset ] or long[noparse][[/noparse] address ][noparse][[/noparse] index ] where "offset" is a multiple of 4 (because they're longs ... 4 bytes each) and "index" is an integer value (the index'th variable). If you're going to do a lot of referring to these variables this way, you may be better of copying the variable values to an array or similar list of variables inside the object, referring to them directly, then copying them back when done. You'd use LONGMOVE for the copy ... it's very fast.
The tradeoff is that the local copy takes space and the copies take a little bit of time, but the indirect references (long[noparse][[/noparse] ... ]) take more space than direct variable references and they're slower as well.
Steve