简体   繁体   English

将程序计数器(AKA指令指针)重置为0

[英]Reset the program-counter (AKA instruction-pointer) to 0

I am trying to reset the program-counter (AKA instruction-pointer) to 0. 我正在尝试将程序计数器(AKA指令指针)重置为0。

I had expected the following C code to work (but it didn't): 我曾期望以下C代码能正常工作(但没有):

typedef void(*func)();
func reset = NULL;
reset();

Here is the dis-assembly when using VS2013 compiler: 这是使用VS2013编译器时的反汇编:

mov  dword ptr[reset],0
mov  esi,esp
call dword ptr[reset]

I realize that this issue is not dictated by the C-language standard, but is rather a matter of specific compiler implementation. 我意识到这个问题不是C语言标准所决定的,而是具体的编译器实现问题。 Nevertheless, I would expect it to work pretty much on every decent compiler. 不过,我希望它在每个不错的编译器上都能正常工作。

What could a function-call be compiled into, besides setting the PC/IP to the address of that function? 除了将PC / IP设置为该功能的地址外,功能调用还可以编译成什么?

Thanks 谢谢

It really depends on the hardware you're targeting, but it will probably compile to the same thing as any other function pointer call. 它实际上取决于您要定位的硬件,但它可能会与任何其他函数指针调用一样编译为同一事物。 It's also possible for the compiler to recognize the constant value given to reset , and optimize it as such. 编译器也有可能识别给reset的常数值,并对其进行优化。 If nothing else, you could always do: 如果没有别的,您可以随时执行以下操作:

((void (*)())NULL)();

which basically casts NULL to a parameterless function of type void . 基本上将NULL转换为void类型的无参数函数。


Whether or not the call succeeds is an entirely different matter: on most platforms using virtual memory, the kernel purposefully leaves the NULL address + some amount of space unmapped (maybe a few KB, maybe a few MB). 调用是否成功是完全不同的事情:在大多数使用虚拟内存的平台上,内核有目的地留下NULL地址+一定数量的未映射空间(可能是几KB,也许是几MB)。 Your instruction pointer will probably still go to 0, but as soon as the CPU tries to fetch an instruction from that address, KABOOM . 您的指令指针可能仍会变为0,但是,一旦CPU尝试从该地址KABOOM中获取指令,该指令指针就会变为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM