ARM Technical Support Knowledge Articles

CASTING A VARIABLE ON THE STACK TO A CODE POINTER

Applies to: C166 C Compiler

Answer


Information in this article applies to:


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

Article last edited on: 2005-07-15 14:07:28

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