variable declaration in multi-slot BS2pe program
davejames
Posts: 4,047
Hi All,
It seems common (even recommended) to declare variables in all 8 slots of a BS2pe program even if they are not specifically used in a particular slot.
Meaning, if variable XYZ is declared and only used in slot 1, it must be declared in slots 2 through 8 also.
Does anybody know why this is the case?
Thanks.
It seems common (even recommended) to declare variables in all 8 slots of a BS2pe program even if they are not specifically used in a particular slot.
Meaning, if variable XYZ is declared and only used in slot 1, it must be declared in slots 2 through 8 also.
Does anybody know why this is the case?
Thanks.
Comments
Here's an example: Say you have two word variables and a byte variable but in the next slot you only declare the byte. It will now occupy part of the original word variable and corrupt that for when you return.
...ahhh - got it!
Thanks.
Essential variables that really need to be used often in multiple banks can be allocated first in every slot. Memory is always allotted to words first in the order they are defined in the program, then all the bytes, then the nibs, then the bits.
If there are bytes, nibs and bits that need to be conserved between slots, give them an alias name as part of one of the initial words. E.g.,
myBestByte VAR myGoodWord.byte0
myBestNib VAR myGoodWord.nib3
myBestBit VAR myGoodWord.bit12
Hi Mr. Allen - yes, I just discovered this morning how variables are allocated (word, then byte, then nib, then bit); thank you.
The tip for "shuttling" variable values will probably come in handy as I re-think the construction of an 8-slot program. Up to this point, I've been lucky that cross-slot variables haven't clashed.
DJ
The trick in the way you do things is to keep track of any variables that may no longer be properly initialized after you've switched banks. I've seen many people get caught by that when declaring things differently.
Out of the 208 bits (13 X 16) available, I'm using 197 in combinations of words, bytes, nibs, nib arrays, and a single bit. Doing this even clarifies "shadowing" nib arrays (0) to other nibs.
And it adds to the documentation that I should have done at the beginning...shame on me :frown:
Hi Tom - how would you envision that looking and operating?
Note that the first half-dozen or words of the variable area needs to be identical for all modules. It would be nice to move that to a separate file, say IDECLARE.bpx which is called by each module (including this one). Then to make a change, one would merely make it in IDECLARE. Just saves a little bookkeeping. But I think that Parallax would have done this about ten years ago if it was gonna happen.
OK - I understand what you're saying. Any, yeah, it would have been done initially if done at all.
Later,
DJ
More questions concerning variables - this time the order in which they are placed in the variable space.
Given:
Are the variables inserted into the variable register(s) as shown below?
Thanks much.
Yup - I was aware of the register display in the IDE, just not sure if the example "j" and "k" were inserted in the order specified by the code ("j" declared first, then "k").
I could tell from the IDE display the order in which word/byte/nib/bit variables were allocated but didn't know if arrays were inserted starting with index zero.
Thanks again.
...roger that.