GCC 内联汇编中的标签

Labels in GCC inline assembly(GCC 内联汇编中的标签)
本文介绍了GCC 内联汇编中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我对 GCC 内联汇编进行的持续实验中,我遇到了一个关于标签和内联代码的新问题.

In my ongoing experimentation with GCC inline assembly, I've run into a new problem regarding labels and inlined code.

考虑下面的简单跳转:

__asm__
(
    "jmp out;"
    "out:;"
    :
    :
);

这除了跳转到 out 标签之外什么都不做.按原样,这段代码编译得很好.但是如果你把它放在一个函数中,然后用优化标志编译,编译器会抱怨:错误:符号‘out’已经定义".

This does nothing except jump to the out label. As is, this code compiles fine. But if you place it inside a function, and then compile with optimization flags, the compiler complains: "Error: symbol 'out' is already defined".

似乎正在发生的事情是编译器每次内联函数时都会重复此汇编代码.这会导致标签 out 被复制,从而导致多个 out 标签.

What seems to be happening is that the compiler is repeating this assembly code every time it inlines the function. This causes the label out to get duplicated, leading to multiple out labels.

那么,我该如何解决这个问题?真的不能在内联汇编中使用标签吗?这个GCC 内联汇编教程 提到:

So, how do I work around this? Is it really not possible to use labels in inline assembly? This tutorial on GCC inline assembly mentions that:

因此,你可以把你的程序集到 CPP 宏和内联 C功能,因此任何人都可以将其用作任何 C 函数/宏.内联函数非常类似于宏,但是有时使用更清洁.当心在所有这些情况下,代码将是重复,所以只有本地标签(1: style) 应该定义为汇编代码.

Thus, you can make put your assembly into CPP macros, and inline C functions, so anyone can use it in as any C function/macro. Inline functions resemble macros very much, but are sometimes cleaner to use. Beware that in all those cases, code will be duplicated, so only local labels (of 1: style) should be defined in that asm code.

我试图找到有关这些本地标签"的更多信息,但似乎找不到与内联汇编相关的任何信息.看起来教程说本地标签是一个数字后跟一个冒号,(如 1:),所以我尝试使用这样的标签.有趣的是,代码已编译,但在运行时它只是触发了一个分段错误.嗯...

I tried to find more information about these "local labels", but can't seem to find anything relating to inline assembly. It looks like the tutorial is saying that a local label is a number followed by a colon, (like 1:), so I tried using a label like that. Interestingly, the code compiled, but at run time it simply triggered a Segmentation Fault. Hmm...

所以有什么建议、提示、答案......?

So any suggestions, hints, answers...?

推荐答案

本地标签的声明实际上是一个数字后跟一个冒号.但是对本地标签的引用需要fb的后缀,这取决于你是向前看还是向后看——即1f 指的是前向的下一个 1: 标签.

A declaration of a local label is indeed a number followed by a colon. But a reference to a local label needs a suffix of f or b, depending on whether you want to look forwards or backwards - i.e. 1f refers to the next 1: label in the forwards direction.

所以将标签声明为 1: 是正确的;但是要引用它,您需要说jmp 1f(因为在这种情况下您正在向前跳跃).

So declaring the label as 1: is correct; but to reference it, you need to say jmp 1f (because you are jumping forwards in this case).

这篇关于GCC 内联汇编中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)