0

I wrote a program using c language., In this program, I was able to be accessed by a pointer to a specific address and opcode can print it.I've used this method to write the self modifying code.I did this in 8086 architecture. I want it running on a microcontroller PIC18F2550.I am using a Mplab IDE 8.92 with Mplab C18 3.46.I've never done this before with microcontroller. Unlike 8086 Here I do not have access to the opcode!The pointer is the address of the opcode. However, the return value is 0X00!This is a code sample

#include <stdio.h>
#include <stdlib.h>
void fsub(void){
int a,b;
a=3;b=5;
a-=b;
printf("a-b=%d\n",a);   
}
void fadd(void)
{
int a,b;
 a=3;b=5;
 a+=b;
 printf("a+b=%d\n",a);
 }
 void retadd(void);
 void main()
 {
  int i;
   unsigned char *instSub,*instAdd;
  unsigned char instructionSub[2];
  void *retadd_addr=(void*)retadd;
  retadd();
  instSub=(unsigned char*)retadd_addr;
  instAdd=(unsigned char*)retadd_addr+2;
  printf("%x\n", *instSub);
  printf("%x\n", *instAdd);
  while(1);
  }
  void retadd(void)
  {
   fsub();
    fadd();
   }

![picture from mplab]this picture show Values1

1 Answers1

1

I don't think you can do this, for two reasons that I detail below:

The PIC controllers typically store their program in flash memory. You can re-program the flash at run-time, but it's not that easy and typically you cannot write a single word but rather must write a whole "page" (or "sector" or something, i.e. a collection of perhaps 64 to 512 bytes or so) at once. This makes it very impractical to modify code at the level you're trying.

Furthermore, the program memory is generally not directly addressable due to the controllers using a Harvard architecture. This means you can't even have a regular pointer pointing at an instruction, because data and code are in different address spaces.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • @user3161225 You can access opcodes through special function calls. You cannot get the address of a function via `void *retadd_addr=(void*)retadd. Review the "Harvard architecture" link or better yet: visit www.microchip.com and search for docs on PIC18F2550. – chux - Reinstate Monica Jan 30 '14 at 21:55
  • I got the address by void *retadd_addr=(void*)retadd (See this picture show Values); But the value of the return address is 0x00! – user3161225 Jan 31 '14 at 07:36
  • @user3161225 Did you read the Harvard page? It really seems like you should, it's kind of confusing. – unwind Jan 31 '14 at 08:33