Shop OBEX P1 Docs P2 Docs Learn Events
Newbie Question - VAR vs. DAT — Parallax Forums

Newbie Question - VAR vs. DAT

Chuck DavisChuck Davis Posts: 23
edited 2007-08-24 14:01 in Propeller 1
I've read the manual several times, done the tutorials, and worked through most of the sample programs, but I'm still a little unclear about one question:·

When is it appropriate to use VAR section to create some data in a object and when should I use a DAT section?

I think I'm clear (correct me if I'm wrong) that the VAR section will be duplicated for each instance of an object while the DAT section will not.· Other than that (and assuming I'm not writing any assembly code for the moment), is their some reason to use one over the other?· I seem to see examples showing both being used in what appear to be similar circumstances.

Any guidance would be appreciated!

Chuck

·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-21 23:42
    You're basically correct. One other distinction is that the VAR section allows for arrays while the DAT section doesn't really support that. You can still have arrays, but you have to give a value to each element ... the notation isn't convenient. In other words, you simply define a series of byte, word, or long constants and you can access them as an array using the label of the first cell.
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-23 19:24
    @Chuck. O.k. You are right, Mike is right.

    Now coming to the differences:
    (1) Variables in the first 500 (I think) bytes of the VAR space are accessed in an optimized way, i.e. when you use those variables your code will run about 10% faster.
    (2) When you need instant specific variables, there is no choice!
    (3) As you can preset DAT variables, I see no drawback using them in all cases where (1) or (2) is of no concern....
    (4) Using "local" variables in routines is a third option: They can also be declared as a vector, but mind the stack size. They are always LONG and cannot be preset.

    When starting COGs with assembly routines, there are more things to be considered..


    Edit: added somewhat later:
    (5) As there is no presetting inside the VAR area, this is utilized for an "optimization" in some cases:
    - loading from PC is much faster
    - Size of .binary files is much smaller
    Although there is otherwise ittle difference between
    LONG varvar[noparse][[/noparse] 8000 ]
    and
    datvat LONG 0[noparse][[/noparse] 8000 ]
    



    you will notice that smile.gif


    Edit 2: The list grows..
    (6) You can use an address of a DAT vaiable (@datv) in a constant context (however not in the CON or VAR section). This is a nice trick to help indexed access to e.g. strings, as explained in the emanual. However it has severe drawbacks, and requires ths skillful use of the @@ operator in SPIN.
    when 'end' and 'start' are dat labels.

    Post Edited (deSilva) : 8/24/2007 10:51:14 AM GMT
  • CJCJ Posts: 470
    edited 2007-08-23 19:29
    DAT arrays are possible, use something like

    label long $ffff[noparse][[/noparse]_32_] (minus the underscores)

    quite visible in the compiler output

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-23 19:50
    Mike said: ".. the notation isn't convenient ..." You did your best to support that smile.gif
  • CJCJ Posts: 470
    edited 2007-08-23 19:57
    it is the same array notation used for VAR arrays, to make it just like a VAR array, use 0 as the value

    I felt Mike was refering to using a comma separated list of values

    label long 0[noparse][[/noparse]_28_]

    is the same as

    label long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.

    Post Edited (CJ) : 8/23/2007 8:02:24 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-23 20:05
    @CJ: I never thought smile.gif But yes, you may be right.. And note this is one of the NOT DOCUMENTED features, though everybody uses it (as with vectors within local variables)

    Edit: I just noticed this is in fact documented in the supplement - never leave the house without it!
    www.parallax.com/dl/docs/prod/prop/PMv1.0Supplement-v1.1.pdf

    Post Edited (deSilva) : 8/25/2007 1:27:30 PM GMT
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 02:58
    Gosh...Perhaps I could put my graphic bitmap in DAT instead of VAR. Then I can have several instances of Graphic object without worrying about duplicating the bitmap! Hmmmm..... rolleyes.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-24 07:18
    I noticed another effect I added to my posting above as "Difference #5":

    (5) As there is no presetting inside the VAR area, this is utilized for an "optimization" in some cases:
    - loading from PC is much faster
    - Size of .binary files is much smaller
    Although there is otherwise ittle difference between
    LONG varvar[noparse][[/noparse] 8000 ]
    and
    datvat LONG 0[noparse][[/noparse] 8000 ]
    



    you will notice that smile.gif
  • big_markbig_mark Posts: 49
    edited 2007-08-24 08:27
    I thought that anything defined in the VAR section is held in hub RAM, and anything in the DAT section is held in cog RAM and will therefore be faster to access?
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-24 08:49
    No.
    This is very basic misunderstanding, but also an excellent question smile.gif

    The COGs are loaded by COGNEW instructions, generally with the code of th SPIN interpreter; when doing SPIN, there is nothing but 8 SPIN interpreters in the COGs!
    You can also load your own machine code into a COG. As assembly code is allowed in the DAT section only, you then load a part of the DAT section into the COG, exactly 496 longs, whatever is allocated there, be it machine code, variables, or just nonsense.

    There has always to be an "archetype" (or "template") in HUB RAM (or ROM) for this loading.

    Utilizing the DAT section for other things but loading COGs is not "just tricky" but a neccessity, as there is no other way to use "global" variables (VAR is not global but object specific ("instant variables"))

    There is also no other way to do some static presetting

    Post Edited (deSilva) : 8/24/2007 10:30:45 AM GMT
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 10:05
    So if I interpret this correctly, I should be able to modify the Graphics object so that I can use it in several different objects at the same time, and they all use the same set of global variables. I'm trying to design an application with a rather complex GUI and I was wondering how I was going to do that...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 10:07
    WOW....perhaps I can use that space to set up a cool splash graphic for startup as well!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-24 10:33
    Ken Peterson said...
    .... I should be able to modify the Graphics object so that I can use it in several different objects at the same time, and they all use the same set of global variables. I'm trying to design an application with a rather complex GUI and I was wondering how I was going to do that...

    Indeed. This is a workaround as you cannot avoid SPIN ALWAYS instantiating an Object when you write something into the OBJ section.

    I changed the FloatString and Float32 accordingly, as I wanted FloatString to use Float32 rather than FloathMath.
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 11:02
    I wonder....Why, in the Graphics Demo, are all the tv params put in the DAT section and then copied into the VAR section with LONGMOVE() before the TV driver is started? I assumed this was being demonstrated as best practice, but is it? Can't I just start the TV driver with a pointer to the DAT section? I only need one instance of the TV object, and I can't imagine many applications where more than one are needed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-24 12:11
    Yes you can - I changed that for by comming video driver tutorial, as it confuses totally.

    The reason may be, that this is the best way to have MULTIPLE monitors with different parameters! The only other way would have been to multiply the DAT sections which cannot be done automatically (unless you have a good macro assembler or precompiler...)
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 13:17
    Coming from an object-oriented programming background, it has been a little bit difficult to get my mind around having access to the same object instance from different levels in a program. This helps tremendously!

    I wish we had a means to define object instances and pass them into other objects as parameters. That would be a lot more object-oriented. ...or can you do that?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 13:21
    I was beginning to think that we stole this thead and went off on a tangent, but I guess in a way we're still on topic. Apologies to Chuck if this is not relevant to his question anymore!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-24 13:37
    Ken Peterson said...
    I wish we had a means to define object instances and pass them into other objects as parameters
    This is the most fundamental requirement for any OO-approach. The rest can be accomlished by ad-hoc programming, as can be seen in languages as Perl (5) and PHP (4) where OO is brought to in very tricky way, but it works somehow; they did it right only in PHP (5) and Perl (6?).

    There is a much more fundamental requirement for serious programming, which is: How to pass the addresses of routines and so have indirect procedure calls (which is in fact connected to your request). As long as this does not work, a language is severely handicapped!

    The problem is to transfer relevant "context" in addition to the simple pointer....

    I remember Peter Jackacki did some "research" here, but his results are not really simple to use...
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 13:53
    Not to mention the fact that most OO languages require dynamic memory allocation, which does not promote deterministic behavior and may not be necessarily appropriate for the types of applications the Propeller is best suited for. I think if your program is so complex that you can only solve it using a strictly OO approach, it may be too big a job for the little Prop with 32K of RAM.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-08-24 13:53
    OK...now I think we're getting a bit off topic...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-24 14:01
    Only a little bit smile.gif You see: evertyhing is connected to everything (old Buddhistic wisdom, also known to all students of any esotheric orientation, as computer science e.g.)

    Take recursive procedure calls e.g. - poppycock, as everybody knows!
    SPIN has it, as there is no other way to implement re-entrant procedures, needed for simple parallel processing smile.gif

    Post Edited (deSilva) : 8/24/2007 2:06:50 PM GMT
Sign In or Register to comment.