This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Incorrect compiler behaviour for local HCONST arrays

Keil C51 v7.20, 16MB contiguous mode for DS80C400.

Why doesn't the following work?

unsigned char LookUp(unsigned char x)
{
  const unsigned char far lookups[]=
  {
    1,2,3,4, ...etc... , 125,126,127,128 // 128 bytes
  };
  return lookups[x];
}

The compiler/linker, as far as I can tell, doesn't place the 128-byte lookup table anywhere in memory. The code which accesses the lookup table actually believes the table starts at address 0x00000000.

It does however work if I specify the size of the array:

unsigned char LookUp(unsigned char x)
{
  const unsigned char far lookups[128]= // *** SIZE SPECIFIED
  {
    1,2,3,4, ...etc... , 125,126,127,128
  };
  return lookups[x];
}

It also works if I make the array global:

const unsigned char far lookups[]= // *** ARRAY NOW GLOBAL
{
  1,2,3,4, ...etc... , 125,126,127,128
};

unsigned char LookUp(unsigned char x)
{
  return lookups[x];
}

In both cases where it works the array is correctly assigned an address somewhere within an HCONST segment and contains the lookup table data.

When it doesn't work, no HCONST segment exists and I believe the lookup table data is nowhere to be found in memory because the SRC/LST files seem to indicate that the compiler has just completely ignored the fact that the array even exists. Oddly enough the SRC refers to the array by name for some MOV/ADDC statements but the name is undefined yet no error is produced - the lines just assemble with zero bytes for the LOW/HIGH/MBYTE parts of the address and these remain at zero even after linking.

0