Shop OBEX P1 Docs P2 Docs Learn Events
Passing data arrays - simple question — Parallax Forums

Passing data arrays - simple question

steprogsteprog Posts: 227
edited 2010-07-23 03:17 in Propeller 1
Hi Guys,
I just need to figure out how you pass arrays or pointers into an subroutine.
Greg

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-10 05:02
    A pointer is just a number that happens to be an address. You pass it the way you'd pass any other numeric value.

    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.
  • steprogsteprog Posts: 227
    edited 2010-07-10 05:06
    Hi Mike,

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-10 05:24
    Look in the Propeller Manual under BYTE, WORD, and LONG. If P is the address of a long variable, you'd use LONG[noparse][[/noparse] P ] to access that variable.

    In your example, "Data" has to be the name of an array (or even a single variable). Probably that's why the compiler complained.
  • ErNaErNa Posts: 1,795
    edited 2010-07-10 17:26
    Hi steprog,
    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.

    destination := BYTE[noparse][[/noparse]source_address] transfers a single byte. So source_address can have every value.
    destination := WORD[noparse][[/noparse]source_address] transfers two bytes. So source_address has to be even
    destination := LONG[noparse][[/noparse]source_address] transfers four bytes. So source_address has to be a multiple of 4
    
    



    the same is true if the destination is determined by a pointer
    BYTE[noparse][[/noparse]destination_address] := BYTE[noparse][[/noparse]source_address] transfers a single byte. So source_address can have every value.
    WORD[noparse][[/noparse]destination_address] := WORD[noparse][[/noparse]source_address] transfers two bytes. So source_address has to be even
    LONG[noparse][[/noparse]destination_address] := LONG[noparse][[/noparse]source_address] transfers for bytes. So source_address has to be a multiple of 4
    
    



    When transfering an element of an array, you do it this way:
    LONG[noparse][[/noparse]destination_address] := LONG[noparse][[/noparse]source_address+4*4] transfers for bytes. So source_address has to be a multiple of 4
    
    


    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. wink.gif

    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 wink.gif
  • steprogsteprog Posts: 227
    edited 2010-07-13 18:34
    Thanks Erna,

    Nice explanation.·

    Greg
  • electricsmithelectricsmith Posts: 20
    edited 2010-07-22 11:48
    Okay,· I have another question related to this thread and passing data arrays.



    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?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-22 13:28
    You've confused me a bit. I don't understand what you're trying to accomplish overall. It looks like you've got a list of variables and you're trying to use them as if they're an array and you want to index into them somehow.

    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.
  • electricsmithelectricsmith Posts: 20
    edited 2010-07-22 16:23
    Thank you Mike very much. I am really glad we have experts like you that always respond, you have helped me tremendously. I do tend to confuse people. Sorry about that.
  • w8anw8an Posts: 176
    edited 2010-07-23 03:17
    Remember, in the Propeller Tool, you can click the Help menu then Propeller Help... to get to a Spin Programming Tutorial

    Steve
Sign In or Register to comment.