Continuing discussion on memory alignment
ags
Posts: 386
With clarity on the topic of memory alignment from a recent, similar thread, and implications on symbol definitions in VAR blocks, I now move my attention to DAT blocks. A few things now become apparent (long train of thought perhaps): 1) Because DAT blocks can contain PASM code, I presume they are not reordered like VAR blocks are, so a long/byte/long/byte/long/byte/long/byte sequence wastes space (total of 8 longs in the DAT block) whereas in a VAR block, symbols are reordered by size; in the case of a VAR block the memory would be allocated as four longs followed by four consecutive bytes, total of 5 longs. 1a) I also presume that all DAT blocks are effectively concatenated into one large block, in the same order in which they appear in the source. 2) Since the entry point provided as the first argument to coginit() is the location in the (concatenated) DAT block which will be loaded into cog RAM starting at 0x00, nothing before it exists in cog RAM. So, even though it will compile, if the PASM code after that entry point (with ORG or ORG 0 directive) refers to memory (a label in DAT block) that is before that entry point, that is not the symbol that will be used in the PASM code. IOW, if mySymbol is a label for the 5th long as it appears in the DAT block, and the PASM code that will be loaded into cog RAM occurs after that symbol and references it, the PASM code will actually be accessing the 5th long after the ORG directive, likely a collision with contents with a completely different purpose (an instruction or now aliased to a storage location with a different symbol/name. Do I have this right?
Comments
You mean like: I'm not really sure what that compiles to. I don't have a Spin tool at hand to find out. Cannot be good though.
I'd expect that mov to move the contents of COG location (Not DAT location) zero into the contents of cog location zero. I.e move the mov instruction itself back over itself.
So then "mov 0, e" would be moving the content of cog location -1 int o location zero.
We don't have negative addresses so if that compiles at all it must refer to the top most long in cog.
Probably not what you had in mind.
Yes I expect also this behavior. The Assembler will not go back and give a..e negative addresses.
But why do you find that "not good"?.
What would be good? If you refer to an address outside the range that lands in the cog, then it's your fault, and you don't know what you do. That's never good if you propgram in Assembly.
You can say it must give an error or warning, but I like Spin (and PASM) exactly because it does what I write. Other compilers try to be more intelligent than me, and I hate that. This loose checking allows a lot of PASM tricks.
Andy
The use of a DAT section with numerous ORG 0's will permit redefinition (aliases if you like) of things. Take a look at my overlay loader in OBEX. There you will find a few tricks about this and also the offsets in rdlong used to advantage. They are not all my own ideas/concepts, but a conglomeration of those into something useful.
re:
No. If you cognew(@start, 0), mov 0,e would load the contents of cog location 4 (relative to start) to location 0 (i.e. start), since e has a cog address of 4, relative to its own implied org of 0 -- even though its contents do not even exist in the started cog.
-Phil
Ah yes of course, it must be like that. The assembler assigns cog addresses 0, 1, 2, 3, 4 to a, b, c, d, e respectively before it hits the ORG.
On hitting the ORG it resets it's COG counter to zero and continues. So the COG address of e is already defined as 4.
No negative addresses involved.
Still. it's unlikely to be what you want to do.
If you do it with long 0 then it's a waste of hub memory, but with res 1 then it makes sense. I've seen this several times in Chip's code. It's a way to make aliases for the first few instructions if you later use them as registers: normally you define the aliases after the code, and you should use an ORG before the regs. You can't be sure that it is the first DAT.
Andy
"It's something you are quite likely to want to do".
Fiendishly clever.
What happens with this?:
Here's my thinking -
Since DAT blocks define initialized data, it exists in hub RAM (other than when using the RES directive). Each symbol has to resolve to a hub address as it can be accessed using @<symbol> syntax in SPIN. The long/word/byte keywords designate alignment (and/or size).
IF this DAT block is loaded into cog RAM and launched, it is directly copied as laid out in hub RAM, beginning with the entry point (in this case, at @sym0). In hub RAM sym3 and sym4 are byte aligned - so they occupy consecutive bytes in hub RAM. But in cog RAM, sym3 and sym4 contents are contained in a single (long) register - immediately following sym2. Yet the assembly pointer for sym4 is sym3+1 (or is it? If not, then both will resolve to the same cog location, and neither will result in the byte value that was most likely intended but a long with the two byte values in the low word). Moreover, every label following sym3 will be off-by-one. So even if the miscreant byte-aligned locations are not used, they will cause all following locations to be incorrect. (DAT block contents are not reordered as VAR blocks are).
I can't test this as I am on a Mac and can't run Propeller tool. What the heck happens in this case? Am I missing something?
If this accurately describe what will happen, in addition to "make sure RES directives are after all instructions and long aligned locations", there should be a "avoid byte/word aligned locations in any DAT blocks that will be launched in a cog" in the tips & tricks document. (or was it tricks & traps?)
.
No it won't. You mentioned "@". That thing means different thing depending on where you use it. For this reason there is "@@" which does not help and if I remember corectly BST has an "@@@". Give me strength.
Yes, I now understand when/why/how to use "@@". Didn't want to, but do now.
I shouldn't have spoken so loosely: "This won't resolve" really means "I keep coming up with things that could be landmines that I didn't know about". While I do enjoy mastering technical content, the driver behind all this thinking/fretting/posting is that I am battling an insidious hang in my current project and I've looked at every line of code over and over. It has symptoms of memory overrun, but is hard to reproduce and so far impossible to debug.
Sure wish I could solve my memory problem...
To clarify, if you want greater-than-byte-and-aligned elements in your table they have to be in a separate line (e.g. [post=1176542]#18[/post]).