EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

How to get a function address in c++

Started by crandel_z October 9, 2009
Hi, I new with MSP430. I want to get the address of one of my program function. In assembler I can do:

MOV #MyFunctionName,R4

in c++ ?

thx

Crandel

Beginning Microcontrollers with the MSP430

crandel_z wrote:
> Hi, I new with MSP430. I want to get the address of one of my program function. In assembler I can do:
>
> MOV #MyFunctionName,R4
>
> in c++ ?
>

Without knowing your compiler and compliance...


Best, Dan.

Thank you, was lack me to make the cast

WORD ptMF = (WORD)&MyFunctionName;

Crandel
--- In m..., Dan Bloomquist wrote:
>
> crandel_z wrote:
> > Hi, I new with MSP430. I want to get the address of one of my program function. In assembler I can do:
> >
> > MOV #MyFunctionName,R4
> >
> > in c++ ?
> >
>
> Without knowing your compiler and compliance...
> Best, Dan.
>

crandel_z wrote:

> Hi, I new with MSP430. I want to get the address of one of my program
> function. In assembler I can do:
>
> MOV #MyFunctionName,R4
>
> in c++ ?

Hi!

Basically, there is no difference in C and C++, well, not for normal
functions that is.

Basically, writing the name of the function "foo" yields a pointer to
that function. What is tricky is getting the syntax right for the type
of the pointer. The best way to do this, in my opinion, is to define a
typedef representing a function type, then using this as the base for
the pointer type.

In the example below, "func_t" is a function type. "func_t *" is a
function pointer type. The program simply returns the address of "my_func".

---------------
typedef void func_t(void);
void my_func(void);
func_t * test()
{
return my_func;
}
---------------

On a plain MSP430, the following compiles into:

MOV.W #my_func, R12
RET

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

Yoy may define a pointer to function type. And then, define all your
"pointed" functions using a macro.
When you need the address of the funtion is just like a variable.
funtion_pointer_type *var;

var= &function_name;
On Thu, Oct 8, 2009 at 5:13 PM, crandel_z wrote:

> Hi, I new with MSP430. I want to get the address of one of my program
> function. In assembler I can do:
>
> MOV #MyFunctionName,R4
>
> in c++ ?
>
> thx
>
> Crandel
>




int func(int x); /* Declare a function */

int (*f) (int x); /* Declare a function pointer */

f=func; /* get the function add */















Franco Bucafusco

ʱ䣺 2009-10-20 07:54:46

msp430



Re: [msp430] How to get a function address in c++



Yoy may define a pointer to function type. And then, define all your

"pointed" functions using a macro.

When you need the address of the funtion is just like a variable.

funtion_pointer_type *var;



var= &function_name;



On Thu, Oct 8, 2009 at 5:13 PM, crandel_z wrote:



>

>

> Hi, I new with MSP430. I want to get the address of one of my program

> function. In assembler I can do:

>

> MOV #MyFunctionName,R4

>

> in c++ ?

>

> thx

>

> Crandel

>

>

>














The 2024 Embedded Online Conference