Keil Logo

C166: Casting a Variable on the Stack to a Code Pointer


Information in this article applies to:

  • C166 All Versions

SYMPTOMS

I have the following function declared:

void testcode (void)
{
unsigned int buf [100];
void (far *func) (void);

xmemcpy (mycode, buf, 100);
func = (void *) buf;
(*func) ();
}

I want to get a far function pointer to buf and then to call it. The following code doesn't convert the variable address to a function pointer correctly. It appears that the lower 14 bits of the address are loaded into the lower word of the address and the contents of the DPP register are loaded into the upper 16 bits.

CAUSE

That is exactly correct. The reason is because far pointers to variables are treated differently than far pointers to program code.

RESOLUTION

Change the assignment of buf to func to the following:

(unsigned long) func = (unsigned long) buf;

and the above program will work correctly.

MORE INFORMATION


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.