PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Strange assembler ldr "problem"...

(This turned into a bit of a ramble - I'll post it anyway :P).

Part I

I declare a value at the top of the file. At the start of the code, I store the stack into it, like this:

Code:
stmfd r13!,{r4-r11,r14}
str r13,|$L_StackDump|

If I then have this code, a little while after (call this A):

Code:
ldr r13,|$L_StackDump|
ldmfd r13!,{r4-r11,pc}


all works just fine.

If I copy the code to close to the end of the file, like this (call this B):

Code:
|$THE_END$|
ldr r13,|$L_StackDump|
ldmfd r13!,{r4-r11,pc}
ENDP


And replace the A code with:

Code:
B |$THE_END$|


... it crashes, or infinite loops or something unpleasant.

So, I disassembled the code and inspected it, and it all seems to be fine.

So, I poked it a bit more. If I replace the ldr in B with:
Code:
adrl r13,|$L_StackDump|
ldr r13,[r13]


It works... :cry:

When compiled, the ldr is:

Code:
ldr r13,[pc,#-0x9e4]
ldmfd r13!,{r4-r11,pc}
, which accounting for the PC being 8 ahead, should be correct.

The adrl + ldr combo compiles to:
Code:
sub r13, pc, #0x800
sub r13, r13, #0x1E4
ldr r13, [r13]
ldmfd r13!,{r4-r11,pc}
, which is synthesizing the offset.

I just can't work out what on earth is going wrong; the offsets are the same, and the offset fits within the 12-bit range for the instruction. The instructions aren't misaligned either.

Has anybody seen this kind of strangeness before, or know of anything that might be causing this?! :?

Part II

I figured that as the code was effectively the same (at last to my mind), the only thing that could be screwing it up was the branch instruction. :S

So, I had a look at it.

Its address is 0x0000014C, and the instruction is:

0xea000226

Which equates to branching to the instruction at PC + 226 :
0x226<<2 + 0x14c + 8 (PC is ahead by 8 in the pipeline)
= 0x898 + 0x154
= 0x9eC
Which is indeed the address of the instruction I want to branch to.

So now I just have no clue what the problem is.

I'll be making a better test function later today, and trying with a later assembler. :S

Any ideas appreciated...

Addendum:

It seems that now the adrl isn't working either. Nor is ldr r13,=|$L_StackDump|

So, to summarize, this works:

Code:
stmfd r13!,{r4-r11,r14}
str r13,|$L_StackDump|
... more code ... (no stores)
ldr r13,|$L_StackDump| ;
ldmfd r13!,{r4-r11,pc} ;


And this doesn't:
Code:
stmfd r13!,{r4-r11,r14}
str r13,|$L_StackDump|
... more code ... (no stores)
b |$THE_END$|
... more code ...

|$THE_END$|
ldr r13,|$L_StackDump| ;
ldmfd r13!,{r4-r11,pc} ;

(And it doesn't matter whether the code at |$THE_END$| uses adrl lrd r13,= or just ldr).

Not having fun. :cry:
I'm thinking two things. Pet hate of data near code which 'C' does but surely sometimes causes a cache issue.

As someone who learnt ARM in 1988 and I'm sure it's just my stoneage thinking. But I LDM back into r14 then mov pc,r14 because of that stupid ^ hat user mode nonsense that wouldn't do the flags right. I'm sure they got rid of it in about 1995-96 with StrongARM.

I'd go with cache anyway keep Data away from Code as they have different caches. 256 aligned bytes should do it.
Code:
stupid ^ hat user mode nonsense that wouldn't do the flags right.
Naah, ISTR that was only a problem in 26-bit mode where the processor flags were in the top 6 bits of the PC. The iPAQ is in 32-bit mode, so it shouldn't be that.

As for the cache though, you have a good point. I wonder if it's loading into both the instruction and data caches, and the instruction cache flushing before the data cache and buggering up the value I stored. Mmh, testing time.

Thanks for the ideas, I'll report back soonish. :) (Seems we learnt ARM code at around the same time, BTW ;))
refractor wrote:Thanks for the ideas, I'll report back soonish. :) (Seems we learnt ARM code at around the same time, BTW ;))


All the extra power since 8MHz ARM2 seems to of done so little difference.
There will always be a part of me thinking SWI, I should make this as a module. I'll be glad to get my Yakumo Omicron with an ARM9 maybe I'll get one of the old "Intel Outside" stickers the Acorn boys were so fond of.
I seem to have "fixed" it.

Here's what I tried.


  1. I made sure that the data was aligned well away from the code. No change.

  2. I commented everything out to find that I could kill it just by doing mov r13,#0xff and b |$THE_END$|

  3. I modified all of the ldr r13,|$L_StackDump| to:
    ldr rXX,=|$L_StackDump|
    ldr r13,[rXX]


#3 fixed the problem, it seems. However, it all seems a bit like "voodoo code" because the ldr r13,|$L_StackDump| works fine in all my other object files :S

<Sigh>

All the extra power since 8MHz ARM2 seems to of done so little difference.
How do you mean?
refractor wrote:
All the extra power since 8MHz ARM2 seems to of done so little difference.
How do you mean?


Great that you fixed it. I thought you looked at what code it produced and it was fine. I'll stick to passing data block registers in 'C', my only labels are Branches. As this doesn't seem to be making sense.

I mean Saloon Cars Deluxe * All the power since then.
= RC Racers ???

Just expected more in 15 years.
I thought you looked at what code it produced and it was fine.
I did - that's the voodoo part :? - it seemed absolutely fine to me - I even disassembled the opcodes and checked they were what they should've been. All I can think is that something was breaking things when it was all loaded into memory.
I'll stick to passing data block registers in 'C',
That's not an option for me right now.

What's annoying me most is that I use the same code/technique in 5 other files, and it works just fine in them... (he says, wondering if they're going to screw up and break at an inopportune time).

Oh well, tomorrow maybe I can test it all properly, and fix the "real" bugs, if there are any (there will be, of course :)).

I have to say that I still miss a lot of the old Acorn games - Chocks Away was great, even on an ARM2, and StarFighter was fun.
It sounds like you have tested it to death. Advantage of using 'C' is you can throw up support code and look for memory leaks without loosing all the day. I remember I'd written a *2 as a <<2, kept reading the thing as correct and saying to myself nothing wrong there. You can end up reading r13. as r13! because r13, makes no bloody sense in a stack. Anyway all I'm saying is sleep then read it.

Whatever good luck!

I'm sure we'll both do the platform justice, its a huge advantage knowing what can be done, instead of guessing something might be possible.
Advantage of using 'C' is you can throw up support code and look for memory leaks without losing all the day.
Yes indeed. You don't get that option when the device goes <splat> when you get something "wrong".

I remember I'd written a *2 as a <<2
Been there, done that. :) Spent a few hours working on that problem before. ;)

Whatever good luck!

Thanks, you've been helpful looking at thing from a different angle. If you're ever stuck or whatever, I'd be happy to return the favour.
As for the cache though, you have a good point. I wonder if it's loading into both the instruction and data caches, and the instruction cache flushing before the data cache and buggering up the value I stored. Mmh, testing time.


I dont quite understand the point here. There should not be any problems with a value loading into both intruction and data cache (besides selfmodifying code, where you have to invalidate the instruction cache). The instruction cache is read only and will never contain dirty data and thus never needs to be cleaned.
I know you are right I just never liked it, as I said 'C' generates code like that all the time. I spend a lot of time reading that assembler trying to work out if its so bad I should hand code it. Most of it saying things like use a TST don't waste a bloody register on an AND then you wouldn't store the screen address in the middle of the critical loop. :roll:

Its an odd error and coming up with anything that would produce it wasn't easy. My last suggestion was TYPO , words like desperate spring to mind. :)
Note to self: don't bother posting conjecture any more, ever.
Most of it saying things like use a TST don't waste a bloody register on an AND then you wouldn't store the screen address in the middle of the critical loop.
;) I've seen far worse from the compiler.

words like desperate spring to mind
I still think it's likely that I screwed something up along the line, but I can't figure out where (as you suggested, maybe a night of good sleep will help with that). I'm actually hoping that somebody will show me where I screwed up, then at least I can bang my head against the wall knowing what the issue was.