Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

read back the complete program of main

Status
Not open for further replies.

electronicsman

Full Member level 5
Full Member level 5
Joined
May 4, 2012
Messages
291
Helped
11
Reputation
22
Reaction score
12
Trophy points
1,298
Activity points
3,737
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.
 

simply capturing instructions means nothing.

for your case if you want to jump from one memory to other and execute from there you can uses something like this
Code:
void (*fptr)(void);
fptr = (void (*)(void))YOUR_VIRTUAL_ADDRESS;
fptr();

If you want to capture main(), then you can pointer to catch but it is not clear until you say what purpose you want to do that.

EDIT
For kind information your code may through a wild pointer exception.
 

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.
 

Hi,

with a Von-Neumann architecture this is possible (PC)
With harvard architekture this is not possible. (many microcontrollers)

Klaus
 

Hi,

with a Von-Neumann architecture this is possible (PC)
With harvard architekture this is not possible. (many microcontrollers)

Klaus

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.
 

But in most of the boot loader applications they are executing the flash algorithms out of ram. How they are doing?
 

Hi,

But in most of the boot loader applications they are executing the flash algorithms out of ram. How they are doing?
You give general statements.
--> Give an example of those "most"?

********
With my answer in post#6 I was referring to this:
The other thing is i want to copy a function say f1() into ram and execute that function from RAM.
And the OP is referring to PIC in post#1.

--> 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.

Am I wrong with this?

Klaus
 

But in most of the boot loader applications they are executing the flash algorithms out of ram. How they are doing?

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.
 

O.K., explains the confusion. PIC has no problems to rewrite flash from flash based boot loader.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top