Shop OBEX P1 Docs P2 Docs Learn Events
Variable organization in Main RAM — Parallax Forums

Variable organization in Main RAM

Vega256Vega256 Posts: 197
edited 2012-08-02 15:55 in Propeller 1
Hey guys,

Lets suppose that I have code in a similar format as this,
var

  var0
  var1
  var3

var

  var4
  var5

where I have two var sections. Is the Prop going to set up these variables sequentially although they are in two separate var sections?

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-07-14 18:13
    All variable sections in an object are joined (sequentially) and treated as one big section (same with DAT). Out of this (big) section the longs are emitted first followed by words and bytes.

    So if your variables are of the same type they will appear in that order.
  • Heater.Heater. Posts: 21,230
    edited 2012-07-14 23:40
    BYTES, WORDS, and LONGS are only reorderd by size in VAR blocks.
    In DAT blocks they will remain in the order you declared them.
    BUT beware that gaps between byte and words and between words and longs may be introduced as the compiler puts words on even addresses and longs on addresses divisible by 4.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-15 05:26
    What about in cog ram?

    For example if you have
    variable1       long   0    ' this would be par
    variable2       long   4
    variable3       long   8
    variable4       long   12
    variable5       long   16
    variable6       long   20
    variable7       long   24
    
    

    and then you change it to

    variable1       long   0    ' this would be par
    variable2       long   4
    'variable3       long   8
    'variable4       long   12
    'variable5       long   16
    variable6       long   20
    variable7       long   24
    
    

    Is there now a wasted 'gap' in cog ram where variables 3/4/5 were at?

    Also, is there a more dynamic way to assign cog ram long location/numbering, then the way I am doing it?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-15 06:52
    In the 2nd case, you have
    variable1   long   0
    variable2   long   4
    variable6   long   20
    variable7   long   24
    
    There's no 'gap'. It's as if the 3 lines (variable3..variable5) were not there. That's true of any comment.

    'dynamic way' ... what do you mean? What are you trying to do? Is there some reason why you're labelling every line? Are you trying to use an array / table?

    If it's just a table, why not do
    table   long   0, 4, 8, 12, 16, 20, 24
    
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-15 07:05
    The reason I ask about a dynamic way is because if I want to comment out the three PASM variables that I do, I have to renumber everything consecutively from top to bottom when I remove a PASM variable from the instantiation portion of my PASM code because they are static in terms of the hub ram byte numeric placement. (0, 4, 8, 12, etc). I was hoping there was a dynamic way to assign that, that I had not figured out yet. I think I've seen something in the past where someone had +4, maybe in the beginning of the PASM code? add variable2 par +4 or something that appeared as if it could be more dynamic?


    So
    variable1       long   0    ' this would be par
    variable2       long   4
    variable3       long   8
    variable4       long   12
    variable5       long   16
    variable6       long   20
    variable7       long   24
    
    becomes
    variable1       long   0    ' this would be par
    variable2       long   4
    'variable3       long   8
    'variable4       long   12
    'variable5       long   16
    variable6       long   8
    variable7       long   12
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-15 07:44
    So ... we're talking about hub ram and particularly a parameter list addressed relative to PAR. Each time you access a parameter, you have to compute its offset from PAR like this:
    myCode   mov   temp,par
             add   temp,#variable1-base
             rdlong   local1,temp
             mov   temp,par
             add    temp,#variable7-base
             rdlong   local7,temp
    
    Your parameter table would start with
    base        long
    
    and this would be followed by your hub variables. You could rearrange the variables and this would still work. You could add variables. You couldn't delete variables that are already referenced in your PASM code without commenting out the code that references them, but that would always be true.

    This is not dynamic in the sense that you can change things at run-time, but it's set up so you can recompile and everything adjusts.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-15 11:52
    Hi Mike,

    Ok, and do you still have to assign numeric non zero values to variable1-variable7 (underneath of base), or can you just set them to

    variable1 long 0
    variable2 long 0

    etc, and use the myCode section with that?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-15 12:10
    variableX are placeholders. You can give them whatever initial value makes sense. Usually zero works.

    The 'myCode' stuff was just an example of how you'd reference the variables.
  • kuronekokuroneko Posts: 3,623
    edited 2012-07-15 17:25
    Mike Green wrote: »
    myCode   mov     temp,par
             add     temp,#variable1-base
             rdlong  local1,temp
             mov     temp,par
             [COLOR="orange"]add     temp,#variable7-base[/COLOR]
             rdlong  local7,temp
    
    Minor adjustment, since we are using cog offsets but need offsets for long addresses either use
    add     temp, #(variable7 - base)[COLOR="orange"]*4[/COLOR]
    
    or
    add     temp, #[COLOR="orange"]@[/COLOR]variable7 - [COLOR="orange"]@[/COLOR]base
    
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-15 18:36
    Thanks for the replies everyone. Just to make sure I understand, it should look like the top code block, if converting from the bottom code block?

    This way if I decide to add (insert) or remove a variable name, I wouldn't have to renumber them like I would if I decided to add/remove in the bottom code block example?

    DAT
    
                            org     0
    
    pasmstart
                          
    
                            mov    temp, par
                            add     temp, #@pstPtr1-@base 
                            add     temp, #@pstPtr2-@base 
                            add     temp, #@pstPtr3-@base 
                            add     temp, #@pstPtr4-@base
                            add     temp, #@pstPtr5-@base
                            add     temp, #@pstPtr6-@base
                            add     temp, #@pstPtr7-@base 
                            add     temp, #@pstPtr8-@base
                            add     temp, #@pstPtr9-@base 
                            add     temp, #@pstPtr10-@base
                            add     temp, #@pstPtr11-@base
    
    
    [ pasm main code here ]
    
    
    ' Variables to read from hub ram and write to hub ram, aka Ptrs
    
    base                    long    0
    cogPtr                  long    0 
    pstPtr1                 long    0
    pstPtr2                 long    0
    pstPtr3                 long    0
    pstPtr4                 long    0
    pstPtr5                 long    0
    pstPtr6                 long    0
    pstPtr7                 long    0
    pstPtr8                 long    0
    pstPtr9                 long    0
    pstPtr10                long    0
    pstPtr11                long    0
    
    

    DAT
    
                            org     0
    
    pasmstart
                          
    
                            add     cogPtr, par
                            add     pstPtr1, par 
                            add     pstPtr2, par 
                            add     pstPtr3, par  
                            add     pstPtr4, par
                            add     pstPtr5, par
                            add     pstPtr6, par 
                            add     pstPtr7, par 
                            add     pstPtr8, par 
                            add     pstPtr9, par 
                            add     pstPtr10, par
                            add     pstPtr11, par 
    
    
    [ pasm main code here ]
    
    
    ' Variables to read from hub ram and write to hub ram, aka Ptrs
    
    cogPtr                  long    0 
    pstPtr1                 long    4
    pstPtr2                 long    8
    pstPtr3                 long    12
    pstPtr4                 long    16
    pstPtr5                 long    20
    pstPtr6                 long    24
    pstPtr7                 long    28
    pstPtr8                 long    32
    pstPtr9                 long    36
    pstPtr10                long    40
    pstPtr11                long    44
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2012-07-15 19:26
    turbosupra wrote: »
    Just to make sure I understand, it should look like the top code block, if converting from the bottom code block?
    They are not equivalent. The bottom code prepares an array of hub addresses relative to par so you can later access the hub location without setup overhead by using e.g. rdlong v, pstPtr1. The top block simply adds offsets to temp and leaves the pointer array unchanged. What you want here is a loop which fills the pointer array with par+4n (correct me if I'm wrong).
    mov     temp, par
    loop            mov     eins, temp              ' table[n] := par + 4n
                    add     loop, dst1              ' next table entry
                    add     temp, #4
                    djnz    ecnt, #loop
    
    ' main code
    
    ecnt            long    __limit__ - __start__   ' table entries
    dst1            long    |< 9                    ' dst +/-= 1
    
    __start__       res     0
    eins            res     1
    zwei            res     1
    drei            res     1
    __limit__       res     0
    
    temp            res     1
    
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-15 20:08
    Hi Kuroneko,

    I'm looking for a more efficient way to do the bottom block, which stores hub ram addresses in cog ram so that I can read from or write to them. So yes, something that allows me to do the pointer array more efficiently. If I find that I have to add something in, I have to shift the numerical value by 4n (n being the number of cog ram variable pointers I am inserting in the 'middle') by hand, and then update all of the numeric values under the instantiation portion of my PASM code.

    There may not be a more efficient way, but my gut tells me there is. The way I am doing it is setting the byte shift by hand and by a static value (4, 8, 12, 16 etc) and it would be nice if I didn't have to hard code that static value, but I could instead put something dynamic, so that if I did have to insert something any other place but at the end of the list, I did not have to then compensate and hand reenter in the newly shifted values by 4 x the number of newly inserted longs.
  • kuronekokuroneko Posts: 3,623
    edited 2012-07-15 20:19
    See example in post #12. The table of pointers sits betwen __start__ and __limit__.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-15 20:29
    Thanks ... I just realized that was German :) and now I see how it works.

    I've known how to count to 10 or 20 in German since I was a kid, just never how to spell the words.
  • Vega256Vega256 Posts: 197
    edited 2012-08-02 07:46
    Sorry to bump; I didn't think I needed to start a new thread for the same topic.

    Suppose I have this variable section in which data types are intermingled.
    var
    
    long a
    long b
    long c
    byte i
    long d
    word e
    word f
    byte j
    word g
    word h
    byte k
    byte l
    

    After compilation, will the variables be sorted out by type and order such that they are arranged like this?
    long a
    long b
    long c
    long d
    word e
    word f
    word g
    word h
    byte i
    byte j
    byte k
    byte l
    

    Or does it just organize with respect to type without regard of order?
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-02 15:55
    The only sorting criteria is type (which is to cut down on alignment padding). Not sure about what you mean by order but if it means the order in which they are defined then yes, this is preserved.
Sign In or Register to comment.