GCC INLINE ASSEMBLY Won't Let Me Overwrite $esp

二次信任 提交于 2019-12-04 07:38:37

Okay so the problem is gcc is allocating input and output to the same register eax. You want to tell gcc that you are clobbering the output before using the input, aka. "earlyclobber".

asm __volatile__("movl %%esp, %0\n\t"
        "movl %1, %%esp"
        : "=&r"(old_stack_ptr) /* output */
        : "r"(new_stack_ptr) /* input */
        );

Notice the & sign for the output. This should fix your code.

Update: alternatively, you could force input and output to be the same register and use xchg, like so:

asm __volatile__("xchg %%esp, %0\n\t"
        : "=r"(old_stack_ptr) /* output */
        : "0"(new_stack_ptr) /* input */
        );

Notice the "0" that says "put this into the same register as argument 0".

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