Keil™, An ARM® Company

Discussion Forum

Local variables (even if designated as xdata) are in code space

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Chris Lake
Posted
1-Jul-2008 16:00
Toolset
C51
New! Local variables (even if designated as xdata) are in code space

Example:
void kfunc(unsigned char * m)
{ unsigned char xdata * k;

REG = *k;
}

If I hover over k, UVision says k = C:0x0000. If I define k as a global no such issues.

Am I missing something here?

Read-Only
Author
Hans-Bernhard Broeker
Posted
1-Jul-2008 16:53
Toolset
C51
New! RE: Local variables (even if designated as xdata) are in code space

I think you're mixing up where the pointer is (most likely in data space) with where it points to (xdata).

The pointer at hand is uninitalized, and has automatic storage, so its value is random garbage. It's impossible to derive any useful conclusion from where it might seem to point.

Read-Only
Author
Chris Lake
Posted
1-Jul-2008 17:06
Toolset
C51
New! RE: Local variables (even if designated as xdata) are in code space

Thanks Hans,
I don't believe it's an issue with misunderstanding the location of the pointer versus the target of the pointer. I've verified that I actually get incorrect behavior (pulling 0s rather than the expected data) when I declare the pointer within a function rather than as a global.

Thanks,
Chris

Read-Only
Author
Andy Neil
Posted
1-Jul-2008 18:08
Toolset
C51
New! RE: I've verified that I actually get incorrect (sic) behavior

But the pointer you've shown is, as already noted, uninitialised!
Therefore its behaviour is undefined, which means that anything goes - there is no such thing as "incorrect" here.

Read-Only
Author
Chris Lake
Posted
2-Jul-2008 13:29
Toolset
C51
New! RE: I've verified that I actually get incorrect (sic) behavior

I didn't post the actual code, which does initialize the pointer. I still don't know why the compiler would place the location of the actual pointer variable (NOT the target) in code space. As I've mentioned, using a global produces the correct behavior while a local does not. Here is, essentially, the actual code:

#define BASE 0x1000

void blah (unsigned char Offset)
{ unsigned char xdata *Address; Address = (BASE + Offset);

DATA1 = *(Address+3); DATA2 = *(Address+2); DATA3 = *(Address+1);
}

Read-Only
Author
Andy Neil
Posted
2-Jul-2008 14:03
Toolset
C51
New! RE: Here is, essentially, the actual code:

"essentially" is meaningless!

Unless it's the real code, copied & pasted from a genuine, compilable source, who knows what apparently "insignificant" changes you might have introduced in the translation...?!

Please note the instructions on how to post source code:
http://www.danlhenry.com/caps/keil_code.png
and remember to check it in the 'Preview'

Read-Only
Author
Chris Lake
Posted
2-Jul-2008 14:21
Toolset
C51
New! RE: Here is, essentially, the actual code:

I verified that the code I posted reproduces the issue. It's not the code from my program, as I can't post that code here. Reposting with the proper formatting...

#define BASE 0x1000

void blah (unsigned char Offset)
{
        unsigned char xdata *Address;
        Address = (BASE + Offset);
        SPXB_MSTR_DATA1 = *(Address+3);
        SPXB_MSTR_DATA2 = *(Address+2);
        SPXB_MSTR_DATA3 = *(Address+1);
}
Read-Only
Author
erik malund
Posted
3-Jul-2008 05:33
Toolset
C51
New! what does the assembly look like?

.

Read-Only
Author
Cpt. Vince
Posted
3-Jul-2008 05:59
Toolset
C51
New! RE: what does the assembly look like?

There you go again erik. Looking into that assembly.

Read-Only
Author
erik malund
Posted
3-Jul-2008 06:03
Toolset
C51
New! how else will you tell what the compiler did ?

.

Read-Only
Author
Cpt. Vince
Posted
3-Jul-2008 06:19
Toolset
C51
New! RE: how else will you tell what the compiler did ?

(I was being sarcastic... remember? from a thread where some ppl didn't think looking into the underlying assembly was 'needed')

Read-Only
Author
erik malund
Posted
3-Jul-2008 07:11
Toolset
C51
New! RE: how else will you tell what the compiler did ?

I knew that, but the eaters of fried pigeons that flew in through the window would not

Erik

Read-Only
Author
Chris Lake
Posted
3-Jul-2008 10:03
Toolset
C51
New! RE: how else will you tell what the compiler did ?

It appears to be an issue with pointer + fixed offset. If I actually modify the pointer instead, I get the correct behavior. Though I still can't inspect the pointer or its target. I think that's due to optimizations, which is fairly reasonable. I'll respond again if I find out more.

Thanks all.

Read-Only
Author
Per Westermark
Posted
3-Jul-2008 10:42
Toolset
C51
New! RE: how else will you tell what the compiler did ?

So, have you looked at the generated assembler code?

A lot of compilers would ignore your pointer variable, and just evaluate the real address for the individual memory accesses - all depending on how good the processor is at doing pointer manipulation.

In this case, it is porbably as cheap - or cheaper - for the compiler to join (BASE plus a constant offset) in the individual accesses (or as an decremented temp variable) and then add the Offset parameter before making the memory access.

Next Thread | Thread List | Previous Thread Start a Thread | Settings