GCC内联汇编:跳转到块外标签

时间:2021-09-08 03:10:40

When using inline assembly under MSVC, one is allowed to jump outside of the assembly block by referencing a label in the C/C++ code, as explained in this MSDN article.

在MSVC下使用内联汇编时,允许通过引用C / C ++代码中的标签跳出汇编块之外,如本MSDN文章中所述。

Can such thing be done when using inline assembly under GCC?

在GCC下使用内联汇编时可以这样做吗?

Here's an example of what I'm trying to accomplish:

这是我要完成的一个例子:

__asm__ __volatile__ (
"   /* assembly code */ "
"   jz external_label;  "
);

/* some C code */

external_label:
/* C code coninues... */

The compiler, however, complains about "external_label" not being defined.

然而,编译器抱怨没有定义“external_label”。

2 个解决方案

#1


10  

What if you define the label with the assembler?

如果使用汇编程序定义标签怎么办?

asm("external_label:");

Update: this code seems to work:

更新:此代码似乎有效:

#include <stdio.h>

int
main(void)
{
  asm("jmp label");
  puts("You should not see this.");
  asm("label:");

  return 0;
}

#2


1  

As of GCC 4.5, you can also use asm goto. The following example jumps to a C label:

从GCC 4.5开始,您也可以使用asm goto。以下示例跳转到C标签:

#include <stdio.h>

int main(void) {
    asm goto (
        "jmp %l[done]"  // %l == lowercase L
        :
        :
        :
        : done          // specify c label(s) here
    );
    printf("Should not see this\n");

done:
    printf("Exiting\n");
    return 0;
}

#1


10  

What if you define the label with the assembler?

如果使用汇编程序定义标签怎么办?

asm("external_label:");

Update: this code seems to work:

更新:此代码似乎有效:

#include <stdio.h>

int
main(void)
{
  asm("jmp label");
  puts("You should not see this.");
  asm("label:");

  return 0;
}

#2


1  

As of GCC 4.5, you can also use asm goto. The following example jumps to a C label:

从GCC 4.5开始,您也可以使用asm goto。以下示例跳转到C标签:

#include <stdio.h>

int main(void) {
    asm goto (
        "jmp %l[done]"  // %l == lowercase L
        :
        :
        :
        : done          // specify c label(s) here
    );
    printf("Should not see this\n");

done:
    printf("Exiting\n");
    return 0;
}