here is the code i wrote, moves y to x using JIT compiling.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef void(*func)(); //type def for a function pointer so we can call the genrated function
inline void GetMov(unsigned char** code,int* x,int* y) {
*code = malloc(11);
unsigned char temp[] = {0xA1,((unsigned int)y),((unsigned int)y)>>8,((unsigned int)y)>>16,((unsigned int)y)>>24, //MOV EAX,[y]
0xA3,((unsigned int)x),((unsigned int)x)>>8,((unsigned int)x)>>16,((unsigned int)x)>>24, //MOV [x],EAX
0xC3
};
memcpy(*code,temp,11);
}
void Run() {
unsigned char* code;
int x,y=22;
GetMov(&code,&x,&y);
((func)code)();
printf("%i",x); //should print 22 to the console
}
int main() {
//this code must assume that pointers are 4 bytes, int's are 4 bytes, and char's are 1 byte,
assert(sizeof(unsigned char)==1);
assert(sizeof(unsigned int)==4);
assert(sizeof(unsigned char*)==4);
assert(sizeof(int*)==4);
Run();
}

New Topic/Question
Reply






MultiQuote




|