Recently, I have wonder how to execute machine code instruction from memory in C/C++. I am aware of data execution protection. Anyway, I have this piece of code:
int main()
{
// allocate 2 bytes for storing machine code
char* mc_add = (char*)malloc(sizeof(char)*2);
int reg_eax; // for storing register EAX
_asm mov eax, 0x10; // assign EAX = 16
_asm mov ecx, 0x01; // assign ECX = 1
// ADD EAX, ECX == 0x01C1
*mc_add = 0x01;
*(mc_add+1) = 0xC1;
// * I want to execute machine code instructuion from mc_add here *
_asm mov reg_eax, eax; // get register EAX
printf("Register EAX is%d", reg_eax); // print EAX
free(mc_add); // free machine code
return 0;
}
Using: Microsoft Visual Studio 2008