如何将程序执行跳转到C中的特定地址? [英] How to jump the program execution to a specific address in C?
问题描述
我希望程序跳到内存中的特定地址并从该地址继续执行。我曾考虑过使用 goto
,但是我没有标签,而只是内存中的地址。
I want the program to jump to a specific address in memory and continue execution from that address. I thought about using goto
but I don't have a label rather just an address in memory.
无需担心从跳转地址返回。
There is no need to worry about return back from the jump address.
编辑:使用GCC编译器
edit: using GCC compiler
推荐答案
内联汇编可能是最简单,最优雅的解决方案,尽管这样做非常不寻常,除非您正在编写调试器
Inline assembly might be the easiest and most "elegant" solution, although doing this is highly unusual, unless you are writing a debugger or some specialized introspective system.
另一个选择可能是声明一个指向void函数的指针( void(* foo)(void)
),然后将指针设置为包含您的地址,然后调用它:
Another option might be to declare a pointer to a void function (void (*foo)(void)
), then set the pointer to contain your address, and then invoke it:
void (*foo)(void) = (void (*)())0x12345678;
foo();
由于编译器认为您正在执行子例程调用,因此将某些东西压入堆栈您不在乎返回,这可能有用。
There will be things pushed on the stack since the compiler thinks you are doing a subroutine call, but since you don't care about returning, this might work.
这篇关于如何将程序执行跳转到C中的特定地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!