ARM Technical Support Knowledge Articles

LOCATING FUNCTION TABLES IN CODE MEMORY

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I have defined a structure that contains an array of function pointers. I then use this to declare a variable. I'm trying to locate the function pointers in code memory, however, I get syntax errors. What am I doing wrong?

typedef struct {
  const code void (*dsfHardwareFunc[2])(long x);
} HARDWARE_TASKLIST;

HARDWARE_TASKLIST functable;

ANSWER

The following code defines a type based on a structure, the same as your example. Because the typedef is simply defining a type, we do not need to specify where the structure goes in memory. It is used as a 'template' for subsequent variable declarations.

The variable functable is then defined using the typedef. This is a variable declaration, so we specify that it is 'const code'. This ensures that the function pointers are located in code memory.

We also have to initialize the table right away because it cannot be initialized later.

The rest of the code simply calls two functions indirectly to prove all the code is correct and functional.

void foo(long x);
void bar(long x);

typedef struct {
  void (*dsfHardwareFunc[2])(long x);
} HARDWARE_TASKLIST;

HARDWARE_TASKLIST const code functable = {foo, bar};

void foo(long x)
{
  x++;
}

void bar(long x)
{
  x++;
}

void main(void)
{
  long x = 5;
  functable.dsfHardwareFunc[0](x);
  functable.dsfHardwareFunc[1](x);
  while(1);
}

If you now look at the Symbol Table of Module in the generated .m51 map file, you see that the functions foo and bar are located at the following locations:

foo: 50H bar: 70H

You also see that functable is located at 9CH in code memory.

If you now load dScope, choose 8051.DLL, load in the object file and type

g, main

into the command line, you can double-check that the function pointers really are stored in code memory.

Type

D C:0x9C

into the dScope command line to display code memory from location 9CH onwards. You will see the following data:

9CH: FF  00  50  FF  00  70

These are the function pointers. The first byte of each pointer is FFH, which means that the pointer points to code memory. The remaining two bytes are the location of the function. You can see that 0050H and 0070H are the locations of the functions foo() and bar().

Now that you can see that the structure is located in code memory, the function pointers are also located in code memory.

If you look through the .m51 file, you may not be able to find the structure name. That is because it was never located in memory. It was simply used as a 'template' for the functable variable.

Using these methods, it is easy to double-check where your function pointers are being located, whether they are declared on their own or as part of a structure.

MORE INFORMATION

Article last edited on: 2005-07-15 12:52:49

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential