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

Function Pointer - Look Up Table

Hello,

I'm using a 80C51 with the MX - extension in my design.
I want to build up a look up table (ROM) that contains the addresses of several functions. The address of each function must be split into high, middle and low - byte so that i can use it later as a "jump - table" in assembler.

For example:
/* some functions. */
void fA();
void fB();
void fC();

/* Look up table: */
static code LUT [3][3] = {H(fA), M(fA), L(fA), {H(fB), M(fB), L(fC),...

I've already tried some macros which i found in other posts like:

#define L_B(v) (*(((unsigned char *) (&v) + 0)))

This macro doesn't work with function - addresses but works well on common pointers.

Best regards,
Patrick

  • To do numeric manipulation on a pointer address, you first have to typecast the pointer to an unsigned integer of suitable size. Then you may use shift iperations or div/modulo to extract information.

    However, all architectures does not guarantee that you can get all information from the pointer converted to an integer.

  • function pointers in the '51 are 'architecturally impossible'

    YES, I mean 'impossible' that Keil has, by "sheer magic", managed to create that functionality should not make you jump into using it.

    There are a legion of problems using finction pointers in the (Keil) '51 overlaying will most likely fail, the code will be slow ....

    again The '51 ain't no PC

    Erik

    PS YES, it is possible to make 'something' while totally ignoring the underlying arachitecture, but working that way the 'something' will be grossly inefficient and, most likely, eventually, develop timing problems.

  • So that i can use it later as a "jump - table" in assembler.

    Then you had better construct the table in assembler, too. There's only so much you can usefully do in C, and this is at least right on the boundary, if not beyond it.

    And prepare for a deluge of secondary problems caused by doing this. Yes, arrays of function pointers are a clever idea in theory. In practice on 8051-style devices, they tend to cause more trouble than they're worth. It's generally better to use switch() statements, and leave it to the compiler to decide whether to turn them into jump tables or not.

  • The below instruction generates LCALL instruction and jumps to the location "address". It may helps you.

    ((void( code * )( void )) address ) ();

  • Be sure to read the Application Notes and Knowledgebase articles and ensure that you fully understand all the issues with function pointers in C51 before doing this...