[英]Can I modify input operands in gcc inline assembly
We know, if I set %eax
as the input, then I cannot include it into list of clobbered registers. 我们知道,如果我将
%eax
设置为输入,则无法将其包含在受破坏的寄存器列表中。 So my question is that, is that legal to modify the value of %eax
in the assembly code without any declaration in clobbered list? 所以我的问题是,在汇编列表中不声明任何内容而修改汇编代码中
%eax
的值是否合法?
__asm__ __volatile__("inc %0" :: "a"(num) : "%eax"); // illegal
__asm__ __volatile__("inc %0" :: "a"(num)); // Can I modify %eax?
No, if the assembly code changes an input register, it must also be listed as an output register, or as an in-out register, by using "+" instead of "=" in the constraint. 否,如果汇编代码更改了输入寄存器,则还必须通过在约束中使用“ +”而不是“ =”将其列为输出寄存器或输入输出寄存器。
Examples: 例子:
__asm__ __volatile__("..." : "+r"(num));
__asm__ __volatile__("..." : "=a"(dummy) : "a"(num));
__asm__ __volatile__("..." : "=r"(dummy) : "0"(num));
The first example specifies num
as both an input and an output. 第一个示例将
num
指定为输入和输出。 This will overwrite the prior value of num
, which may be undesirable if the operand is clobbered rather being set to a useful value. 这将覆盖
num
的先前值,如果操作数被破坏而被设置为有用的值,则这可能是不希望的。
The second example explicitly uses eax for both input and output. 第二个示例将eax明确用于输入和输出。 It directs the output to a
dummy
variable so as to not corrupt the actual input variable num
. 它将输出定向到
dummy
变量,以免破坏实际的输入变量num
。
The third example uses a dummy output, to avoid modifying num
, and also avoids specifying eax explicitly, allowing the compiler to choose which register to use. 第三个示例使用伪输出,以避免修改
num
,还避免显式指定eax,从而允许编译器选择要使用的寄存器。 The "0"
constraint tells the compiler to use the same register as operand 0 for this input operand. "0"
约束条件告诉编译器对此输入操作数使用与操作数0相同的寄存器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.