Newbie Question - VAR vs. DAT
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
·
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
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
you will notice that
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
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.
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
(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
you will notice that
This is very basic misunderstanding, but also an excellent question
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
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...)
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?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
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...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
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
Post Edited (deSilva) : 8/24/2007 2:06:50 PM GMT