C/C++ inline assembler with instructions in string variables

烂漫一生 提交于 2020-01-19 18:07:37

问题


So as you know in C and C++ if using Visual-C you can have in line assembly instructions such as:

int main() {
    printf("Hello\n");
    __asm int 3
    printf("this will not be printed.\n");
    return 0;
}

Which will make a breakpoint inside of the executable. So my question is, is there somekind of function I can use to call __asm using a variable such as a char array. I was thinking something like this:

char instruction[100] = "int 3";
__asm instruction

But that doesn't seem to really work since it gives 'Invalid OP code'. So can you help with this or it isn't possible at all.


回答1:


Neither C nor C++ are interpreted languages, the compiler generates the int 3 machine instruction at compile time. The compiled program will not recognise the string as an instruction at run-time - unless your program is itself an interpreter.

You can of course use a macro:

#define BREAKPOINT __asm int 3

int main() 
{
    printf("Hello\n");

    BREAKPOINT ;

    printf("this will not be printed.\n");
    return 0;
}



回答2:


The code of your program is created by the compiler, during compilation.
You are trying to feed the compiler the input for that at run time - when the program is already executing. If you want ‘on-the-fly-compilation’, you will have to program an incremental compiler and linker that can modify the code while executing it.

Note that even if you would be successful, many OS would block such execution, as it violates security. It would be a great way to build viruses, and is therefore typically blocked.



来源:https://stackoverflow.com/questions/47113280/c-c-inline-assembler-with-instructions-in-string-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!