【问题标题】:How to get asm to jump to a variable address in c?如何让asm跳转到c中的变量地址?
【发布时间】:2013-11-07 03:46:00
【问题描述】:

现在,我用c中的内联汇编调用跳转指令如下:

int register eax asm("eax") = addr; // addr is a memory address
asm("jmp *%eax");

我想这样做而不必设置任何其他寄存器值(例如,我想做的是这样的):

asm("jmp *(addr)");

这样做的正确方法是什么?也就是说,如何将 c 变量“插入”到 asm 调用中?

Ubuntu 12.04 64 位,Intel x86 64 位处理器,gcc 版本 4.7.4。

【问题讨论】:

    标签: c linux assembly x86 inline-assembly


    【解决方案1】:

    "Assembler Instructions with C Expression Operands"

    asm volatile("jmp *%0" : : "r" (addr));
    

    【讨论】:

    • 这给出了以下错误:my.c:450:2: error: output operand constraint lacks '=' my.c:450:2: error: output operand constraint lacks '=' my.c:450:2: error: invalid lvalue in asm output 0
    • @Sefu:对,我的错。
    • 请注意,在没有 asm goto (GCC manual) 的内联 asm 语句中使用 jmp 通常是不安全的。或者至少在 asm 语句之后有一个 __builtin_unreachable() 让编译器知道执行不会从另一边出来,并且它不能安全地将存储或其他东西推迟到 asm 之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多