I would like to write a function that will have some optional code that will be executed or not depend on user settings. This function is intense cpu, and ifs in it will be slow, as the branch predictor is not so good.
My idea is to create a copy in memory of this function and replace the NOPs with a jump when I don't want to execute any code. My working example is as follows:
int Test()
{
int x = 2;
for (int i=0 ; i<10 ; i++)
{
x *= 2;
__asm {NOP};
__asm {NOP};
x *= 2;
x *= 2;
}
return x;
}
In my main test copy, I copy this function to the newly allocated executable memory and replace the NOP with JMP 2 so that the next x * = 2 is not executed. JMP 2 really "skips the next 2 bytes."
, JMP , , , .
, , :
__asm {NOP};
__asm {NOP};
goto dont_do_it;
x *= 2;
dont_do_it:
x *= 2;
goto, . , , goto x * = 2 , .
, .
VStudio 2008.