How to jump the program execution to a specific address in C?

前端 未结 8 712
感动是毒
感动是毒 2021-01-02 01:17

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

相关标签:
8条回答
  • 2021-01-02 02:02

    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.

    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.

    0 讨论(0)
  • 2021-01-02 02:02

    This is what I am using for my bootstrap loader(MSP430AFE253,Compiler = gcc,CodeCompeserStudio);

    #define API_RESET_VECT 0xFBFE
    #define JUMP_TO_APP()  {((void (*)()) (*(uint16_t*)API_RESET_VECT)) ();}
    
    0 讨论(0)
提交回复
热议问题