简体   繁体   中英

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

I am trying to reset the program-counter (AKA instruction-pointer) to 0.

I had expected the following C code to work (but it didn't):

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

Here is the dis-assembly when using VS2013 compiler:

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. 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?

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. If nothing else, you could always do:

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

which basically casts NULL to a parameterless function of type 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). Your instruction pointer will probably still go to 0, but as soon as the CPU tries to fetch an instruction from that address, KABOOM .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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