I need to call a function at address 0xDD2:
// foo.h
void foo(void) __at(0xDD2);
// foo.c
#include "foo.h"
void foo(void)
{
// some code
}
This code works:
#include "foo.h"
void main(void)
{
void (*a)(void) = &foo;
a();
}
However, this one doesn't:
#include "foo.h"
void main(void)
{
void (*a)(void) = (void (*)(void))(0x0DD2);
a();
}
The compiler (XC8) says: main.c:5:: warning: (759) expression generates no code
and debugger passes these lines while debugging.
I need second one (call function just by its address). Why compiler optimizes it out? Is there any mistake in pointer assignment? Changing optimization level of compiler didn't help.
Based on what I read in XC8 manual and some discussions at another forum, it's not a good idea to put objects at a fixed address to use them in another project because of compiled stack and some other reasons.
However, if you just need to prevent compiler from optimizing out pointer to function when address is assigned manually, Use code in this comment or this answer.