Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top