Shop OBEX P1 Docs P2 Docs Learn Events
allignment of VARiables in memory — Parallax Forums

allignment of VARiables in memory

janbjanb Posts: 74
edited 2006-08-04 03:43 in Propeller 1
Hi,
I was expecting the memory allocated·in the VAR block matches seqence of definitions.

I'm puzzeld why
VAR
·· byte a[noparse][[/noparse]4]
·· word w1,w2
·· byte x···
results with
@x-@w2= 6

x·follows w2 and the difference in adresses should be equal to the size of word=2 ?

If I change dimention of a[noparse][[/noparse]4]-- >a[noparse][[/noparse]8] I'd get
@x-@w2= $A· which is 2+8

If I move decalartion of a[noparse]/noparse below declaration of x· I'd get 2 as difference - as expected.

The example code is attached.
Could you point where·my logic breaks?
Thanks
Jan

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-08-04 01:17
    I suspect that variables are allocated from large to small. So longs would be allocated first, then words, then bytes. This helps to minimize vacant bytes left between differently-aligned variables.

    -Phil
  • janbjanb Posts: 74
    edited 2006-08-04 02:50
    Hi,

    I see.

    In the fullDuplexSerial code there is a bunch of variables allocated in SPIN and the actual data transmision is done in assembler, assuming the order of variables as in SPIN VAR-block. Does it mean it works correctly only because all variables·were declared as long?

    In other words, if I want to use some set of variables as a FORTRAN-style common block and pass only address of the first one I'd better decalre all of them as long to avoid surprises?

    Thanks

    Jan
  • Mike GreenMike Green Posts: 23,101
    edited 2006-08-04 03:43
    Yes, you can assume that a block of same-size variables will be allocated in order, but not mixed size.· Local variables in a method are always longs so they are always allocated in order.· It would be nice to have this clearly stated in the documentation.· Maybe next iteration when there are "advanced" examples included.
Sign In or Register to comment.