Shop OBEX P1 Docs P2 Docs Learn Events
passing array` — Parallax Forums

passing array`

Rob v.d. bergRob v.d. berg Posts: 81
edited 2008-12-13 08:50 in Propeller 1
Hi,

Can someone help me with this, how·can to pass an array from·one object to an other.·here a test example in one object.
The mess array has to be·the same as the message array

Rob

CON
· _CLKMODE····· = XTAL1 + PLL16X··
· _XINFREQ····· = 5_000_000
·
· pcDebugTX···· = 30
· pcDebugRX···· = 31
· pcDebugBaud·· = 115200
··
VAR
· long message[noparse][[/noparse]4]

OBJ
· debug········ : "Debug_PC"
···
PUB Main
· debug.startx(pcDebugRX,pcDebugTX,pcDebugBaud)
·
· message[noparse][[/noparse]0] := false '0
· message[noparse][[/noparse]1] := true· '-1
· message[noparse][[/noparse]2] := false '0···
· message[noparse][[/noparse]3] := false '0

· ReceiveArray(@message)

'can be placed·in an other object·
PUB ReceiveArray(mess) | i
·
· i := 0
· repeat 4
··· debug.str(string("Array mess[noparse][[/noparse]"))
··· debug.dec(i)
··· debug.str(string("]: "))
··· debug.dec(mess[noparse][[/noparse]i])
··· debug.putc(13)
··· i++

·

Comments

  • mcstarmcstar Posts: 144
    edited 2008-12-12 12:47
    In your code, you are not really "passing an array", you are really passing the memory address to the array with your call (which is correct)

    ReceiveArray(@message)
    



    So, in your other method you want read from the locations pointed to by the address you passed in. Mess is an address, not an object. Read the prop manual about the methods Byte[noparse]/noparse Word[noparse]/noparse and Long[noparse]/noparse which let you access any element in memory if you know its base address and an index.

    So to read from the memory address pointed by @message you passed into your second object, try this...

    'can be placed in an other object  
    PUB ReceiveArray(mess) | i
      
      i := 0
      repeat 4
        debug.str(string("Array mess[noparse][[/noparse]"))
        debug.dec(i)
        debug.str(string("]: "))
        debug.dec( long[noparse][[/noparse]mess+i])  ' <- change here.  
         '(FYI, there is another syntax that also works, 
         '   but will not post to the forum due to the square brackets, so see the manual for its syntax)
        debug.putc(13)
        i++
    
    
    




    Just remember that once you use the @ symbol, you're no longer dealing with a variable in local memory, just a pointer to memory address. It becomes more clear when you read about how the assembly operations work.

    Post Edited (mcstar) : 12/12/2008 12:54:37 PM GMT
  • Rob v.d. bergRob v.d. berg Posts: 81
    edited 2008-12-12 16:25
    I tested, but it·was not working well:

    debug.dec(·long[noparse][[/noparse]mess+i])··'·<-·change·here.

    after reading the manual, i tray this:·debug.dec(long[noparse][[/noparse]mess][noparse][[/noparse]i]) and is was working

    thanks.

    Rob

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-12 17:47
    If you use this: "long[noparse][[/noparse]mess][noparse][[/noparse] i ]", the compiler multiplies the "i" by 4 and adds "mess" before using the result as an address

    If you use this: "long[noparse][[/noparse]mess+i]", the compiler adds "mess" and "i". This only works when "i" is a multiple of 4 since addresses in Spin are byte addresses and all longs have to be aligned on a 4 byte boundary. Words have to be aligned on a 2 byte boundary.
  • Rob v.d. bergRob v.d. berg Posts: 81
    edited 2008-12-12 18:31
    Hi, Mike

    clear thanks.

    Rob.
  • mcstarmcstar Posts: 144
    edited 2008-12-12 22:02
    Yeah, that's what I was trying to show too, but the forum's text editor was interpreting the "[noparse][[/noparse] i ]" as an italics and hiding it in the code.
  • Rob v.d. bergRob v.d. berg Posts: 81
    edited 2008-12-13 08:50
    mcstar,

    I·understand, thanks for your input

    rob
Sign In or Register to comment.