I decided to start learning some inline assembly however I am left with a simple question for which I am unable to find a clear answer.
我决定开始学习一些内联汇编,但是我留下了一个简单的问题,我无法找到明确的答案。
Take the following simple example which I found in a tutorial which performs a simple addition as such:
以下是我在教程中找到的简单示例,该示例执行简单的添加:
int one,two,out;
one = 1;
two = 2;
__asm__ ( "add eax, ebx;" : "=a" (out) : "a" (one) , "b" (two) );
Note that for the sake of my own sanity after working with intel syntax for a while, I configured GCC to use intel syntax instead of AT&T.
请注意,在使用intel语法一段时间后,为了我自己的理智,我将GCC配置为使用intel语法而不是AT&T。
Now suppose I would want to leave out the "out" variable and just store the output in "one" instead (or "two" for all that matters).
现在假设我想省略“out”变量,只是将输出存储在“one”中(或者将“2”存储到所有重要的位置)。
How would I do this?
我该怎么办?
2 个解决方案
#1
1
__asm__ ( "add eax, ebx;"
: "+a" (one)
: "b" (two) );
The "+"
modifier indicates a 'read/write' operand (and must be used in the output constraint).
“+”修饰符表示“读/写”操作数(必须在输出约束中使用)。
#2
3
Use
__asm__ __volatile__(" add %0, %2": "=r" (one): "0"(one), "r" (two) )
By using "=r" and "r", you don't unnecessarily force the compiler to use a particular register, which helps register allocation. "0" means "use same as (output) argument 0".
通过使用“= r”和“r”,您不会不必要地强制编译器使用特定寄存器,这有助于寄存器分配。 “0”表示“与(输出)参数0一样使用”。
Edit3: The two
argument is in %2
, not in %1
, which is just a second copy of one
(same register as %0
). Also fixed double %
when single should be used.
Edit3:这两个参数在%2中,而不在%1中,它只是一个的第二个副本(与%0相同的寄存器)。当应该使用单个时,也固定双倍%。
Edit2: I also added __volatile__
to ensure that the compiler doesn't move or omit the assembler code, which can happen if the compiler does not think your code does anything useful [typically because it produces no output that the compiler uses later on]. With __volatile__
the compiler is guaranteed to NOT move or remove the assembler code.
Edit2:我还添加了__volatile__以确保编译器不移动或省略汇编代码,如果编译器认为你的代码没有做任何有用的事情,这可能会发生[通常因为它不会产生编译器稍后使用的输出]。使用__volatile__,编译器可以保证不移动或删除汇编代码。
Edit1: Fix up syntax.
Edit1:修复语法。
#1
1
__asm__ ( "add eax, ebx;"
: "+a" (one)
: "b" (two) );
The "+"
modifier indicates a 'read/write' operand (and must be used in the output constraint).
“+”修饰符表示“读/写”操作数(必须在输出约束中使用)。
#2
3
Use
__asm__ __volatile__(" add %0, %2": "=r" (one): "0"(one), "r" (two) )
By using "=r" and "r", you don't unnecessarily force the compiler to use a particular register, which helps register allocation. "0" means "use same as (output) argument 0".
通过使用“= r”和“r”,您不会不必要地强制编译器使用特定寄存器,这有助于寄存器分配。 “0”表示“与(输出)参数0一样使用”。
Edit3: The two
argument is in %2
, not in %1
, which is just a second copy of one
(same register as %0
). Also fixed double %
when single should be used.
Edit3:这两个参数在%2中,而不在%1中,它只是一个的第二个副本(与%0相同的寄存器)。当应该使用单个时,也固定双倍%。
Edit2: I also added __volatile__
to ensure that the compiler doesn't move or omit the assembler code, which can happen if the compiler does not think your code does anything useful [typically because it produces no output that the compiler uses later on]. With __volatile__
the compiler is guaranteed to NOT move or remove the assembler code.
Edit2:我还添加了__volatile__以确保编译器不移动或省略汇编代码,如果编译器认为你的代码没有做任何有用的事情,这可能会发生[通常因为它不会产生编译器稍后使用的输出]。使用__volatile__,编译器可以保证不移动或删除汇编代码。
Edit1: Fix up syntax.
Edit1:修复语法。