Shop OBEX P1 Docs P2 Docs Learn Events
? about passing variables between PASM cogs — Parallax Forums

? about passing variables between PASM cogs

AGCBAGCB Posts: 327
edited 2014-12-31 05:29 in Propeller 1
My continuing quest to learn PASM.

Can I use the same hub variable and pass it between 2 or more PASM cogs? Back and forth. Similar to this
VAR
  one, two, three
PUB
  cognew(@cog1,@one)
  cognew(@cog2,@one)
DAT    org
cog1   mov  copy,  par
           add  copy,  #4
           rdlong  item1,  copy     'this to put hub var "two" into item1
           org
cog2   mov  copy1,  par
           add  copy1,  #8,  
           rdlong  item2,  copy1     'this to put hub var "three" into item2
           wrlong  item2,  par       'this now puts item2 into hub var "one" 

Thanks,
Aaron

Comments

  • ElectrodudeElectrodude Posts: 1,658
    edited 2014-12-30 13:54
    Yes, that method should work fine.

    Note that if you mix longs, words, and bytes in an object, the final order (within that object) is first all of the longs, then all of the words, and then all of the bytes, each size grouped together and ordered in the order you defined them.
    This:
    VAR
      word a,b
      byte c
      long d,e
      byte f
      word g
    
    will end up in the following order: (at least according to BST)
    VAR
      long d,e
      word a,b,g
      byte c,f
    
  • tomcrawfordtomcrawford Posts: 1,126
    edited 2014-12-30 14:02
    The short answer is "yes". I usually use par to point to a list of addresses and constants, rather than the actual argument. The bottom line is, a given address in hub memory refers to the same location for all hubs.
    Edit: Electrodude said it better.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-30 14:06
    will end up in the following order: (at least according to BST)

    This is also the case with the Propeller Tool.

    Variables in DAT sections don't get switched around like they do in the VAR section.

    I personally always list my variables in long, word then byte order just so I'm not surprised by anything being switched by the compiler.

    Remember to always pass a long address with par. The last two bits of the address are lost when launching a PASM cog so you can only count on a long address as being passed correctly.
  • JDatJDat Posts: 103
    edited 2014-12-30 14:23
    Duane Degn wrote: »
    Remember to always pass a long address with par. The last two bits of the address are lost when launching a PASM cog so you can only count on a long address as being passed correctly.
    Thank you! This is important information.
  • AGCBAGCB Posts: 327
    edited 2014-12-30 14:40
    Thanks.

    OK so I know how to get the 2nd or 3rd var into the cog, but how do I write back to a var other that the 1st?
  • Cluso99Cluso99 Posts: 18,069
    edited 2014-12-30 14:58
    A few things to note...
    You really have two PASM programs here within the one DAT section.
    It is normal to separate these with their own DAT sections and then each will have its own private variables at the end (ie copy1, item1, etc).
    I always use "ORG 0" just to be sure (ie add the 0 or $0 parameter to the ORG)

    Your question in post #6..
    Just use
    wrlong anyvar,anyptr
    where anyptr can be copy1 or any variable which is typically set to par + offset.

    We typically use ptrxxx or xxxptr to be a variable containing a pointer to hub (in your case this is copy1 or copy2)

    BTW I believe there are good pasm learning info around - maybe someone would like to chime in where they are.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-30 15:01
    AGCB wrote: »
    Thanks.

    OK so I know how to get the 2nd or 3rd var into the cog, but how do I write back to a var other that the 1st?

    You can write the same way you read.

    Use "wrlong item1, copy" instead of "rdlong item1, copy" to write the value of item1 to address "copy". It's common to maintain multiple addresses (if different variables) in the cog for this purpose.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-30 15:01
    Cluso99 wrote: »
    BTW I believe there are good pasm learning info around - maybe someone would like to chime in where they are.

    Post #3 of my index.
  • AGCBAGCB Posts: 327
    edited 2014-12-30 15:42
    From the bottom part of my original post
    cog2   mov  copy1,  par
               add  copy1,  #8,  
               rdlong  item2,  copy1     'this to put hub var "three" into item2
               wrlong  item2,  par       'this now puts item2 into hub var "one
    

    To instead write item2 to the second hub var, what do I change
  • AGCBAGCB Posts: 327
    edited 2014-12-30 16:00
    cog2   mov  copy1,  par
               add  copy1,  #8,  
               rdlong  item2,  copy1     'this to put hub var "three" into item2
               mov  copy1,  par            'get par address into copy1           
               add  copy1,  #4
               wrlong  item2,  copy1       'this now puts item2 into hub  2nd var?????
    

    Is this it?
  • tonyp12tonyp12 Posts: 1,951
    edited 2014-12-30 19:09
    Save a long as you know what value copy1 has.
    yes this code will read the 3rd long and write it back to the 2nd long, the 1st long (location 0) is the value PAR have.
    cog2       mov     copy1,  par      'get read-only PAR value (value was set when cognew was initiated)
               add     copy1,  #8 
               rdlong  item2,  copy1     
               sub     copy1,  #4
               wrlong  item2,  copy1   ' wrlong is quirky in how source(hub address)->dest (actual value), just use it in the same layout as rdlong
    



    declaring values (4,8,12) does not waste hub space as 'add copy1, #4' is no longer in the code that initially is also stored in hub so it's a wash, but cog space is saved that is important,
    and the readability is way better.
    CON       hub_one = $1F0    ' $1F0=PAR, just to keep the mailbox names the same without wasting any longs (CON are only used by Compiler)
    
    DAT
      add     hub_two,   PAR    ' set up mailbox
      add     hub_three, PAR
      add     hub_four,  PAR
    loop
      rdlong  par, hub_three    ' use destination-side par (shadow) as a free long
      wrlong  par, hub_one
      ...
    
    hub_two   long 4
    hub_three long 8
    hub_four  long 12
    
  • frank freedmanfrank freedman Posts: 1,983
    edited 2014-12-30 20:12
    Cluso99 wrote: »
    A few things to note...
    BTW I believe there are good pasm learning info around - maybe someone would like to chime in where they are.

    see propeller assembly for the beginner thread. multiple really good examples by kuroneko, jazzed, and others on this and other topics. Filter heavily as needed. Also, Propeller Tricks and Traps by Phil Pilgrim, The tutorial written by potatohead , and Da Silva's tutorial are all good resources to get started.
  • edited 2014-12-30 20:58
    The information published by potatohead and deSilva is attached. Both provide excellent reference material for PASM.

    They are freely available on this forum if you know where to look and what to look for.

    Sandy
  • AGCBAGCB Posts: 327
    edited 2014-12-31 05:29
    YOU DID IT AGAIN!

    You taught me more than I asked to know. What a great forum because of great members!

    Tonyp12
    I was thinking that I could somehow make some shortcuts similar to that. I'll study yours.

    Alexander (Sandy) Hapgood
    I've gone through the DeSilva one several times. Will look at the other.

    Duane Degn
    I'll have to search in your index some more. I see there are probably some I haven't looked at yet.

    Cluso99
    Thanks for those "few things to note". A lot of what I do has been learned by studying other peoples code and not all are done the same. I guess it just takes time to learn the perfect way even if a slightly different way works also (at least so far)

    Thanks to the others as well. I appreciate the replies and time taken of everyone.

    Aaron
Sign In or Register to comment.