In general for micro controllers let us say my .text section starts at 0x100 that is main. Now i want to define a pointer to this starting location 0x100 and read this data in the main itself. For example
Code:
int main(void)
{
int *p; /* I want to point this to the start of main or 0x100 location */
int data;
data = *p; /* i want to capture the instructions at 0x100 into data variable*/
}
Is the above thing possible? This is for PIC controllers.
A PIC (presume you are talking about 8 Bit PIC) can't access program memory as data without using special TBLRD instructions. Constant data in flash or function pointers as mentioned by kanni1303 are supported by PIC compilers, but the required C syntax varies. Not all compilers allow to dereference a function pointer into a data read. Review your compiler's user manual.
The suggest C construct in post #1 is useless in any case. You need to point to the address of main () in some way. A data object inside main() has no relations to the instruction address.
The address of main is - astonishingly enough - just main.
You can legally assign this value to any other function pointer:
Code:
int main(int argc, char *argv[])
{
int (*fp)(int, char **) = main;
printf("main is at %p\n", fp);
}
What you can't do (well, at least with some compilers, you can, but it will invoke undefined behaviour according to the language standard) is to cast this function pointer to anything else than another function pointer.
This might work on some platforms with some compilers, but it's illegal from the language definition (so better don't do it):
Code:
int (*fp)(int, char **) = main;
char *data;
int i;
data = (char *) fp; /* THIS IS ILLEGAL */
for (i = 0; i < 10; i++)
printf("%02x \n", data[i]);
Thanks for all the replies. After reading i got some doubts like how does the compiler know whether it is constant data in program memory or instructions? The other thing is i want to copy a function say f1() into ram and execute that function from RAM. Lot of times i heard this thing of executing out of RAM so practically i want to see. Will the architecture allows it? Please help.
Even in a VN-architecture this is difficult since on most modern architectures there are data and instruction caches involved (not to mention MMU/memory protection) that would needed to be disabled/flushed which you usually can't do in plain C anyway.
--> With a PIC it is impossible to store any program into RAM and execute it from RAM.
But with a PC it is done all the time. All the program (except BIOS?) is stored into RAM and the executed from RAM.
You mentioned "PIC" processor without further specification (8-Bit/16-Bit/32-Bit PIC ?). The latest question suggests that you don't yet know or understand the differences in processor architecture. 8- and 16-Bit PIC can't execute code out of RAM. Nevertheless it can read flash content, C syntax varies.
Why don't you specify the processor, and if you are interested in a functional C code, the used compiler?
Sorry for the incomplete information. I have seen nxp/freescale s12x controllers with the execution from ram. Now i migrated to pic i thought it will be the same.