It was thus said that the Great Ron Hudson once stated:
Can you create self modifying code in any high level language, the kind of
code where the application program actually changes it's own instructions?
Any high level language? Depends upon the language. Something like
Haskel or ML, not at all.
I know in C it is possible to pass an address of a
function to a function,
that's not really what I mean.
It violates ANSI C, and it certainly isn't portable, but yes, it can be
done. Maybe. And when you get it working (I didn't, but I'm not going to
waste much time on this), make sure you remember how you compiled it,
because any changes in compiler options will set you back to square one.
-spc (Below is my first attempt ... probably have the wrong location)
#include <stdlib.h>
#define UP_OPCODE 0x77
#define DOWN_OPCODE 0x72
void sort(int *array,size_t size)
{
size_t i;
size_t j;
int t;
for (i = 0 ; i < size ; i++)
{
for (j = i + 1 ; j < size ; j++)
{
if (array[i] > array[j])
{
t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
}
void sort_ascending(int *array,size_t size)
{
unsigned char *f;
f = (unsigned char *)sort;
f[0x16] = UP_OPCODE;
sort(array,size);
}
void sort_descending(int *array,size_t size)
{
unsigned char *f;
f = (unsigned char *)sort;
f[0x16] = DOWN_OPCODE;
sort(array,size);
}
int main(void)
{
int data[256];
sort_ascending(data,256);
sort_descending(data,256);
return(0);
}