简体   繁体   English

汇编:英特尔x86-64汇编中的CMOVB指令

[英]Assembly: CMOVB instruction in Intel x86-64 assembly

I'm a little confused about what "cmovb" does in this assembly code 我对“ cmovb”在此汇编代码中的作用有些困惑

leal   (%rsi, %rsi), %eax  // %eax <- %rsi + %rsi
cmpl   %esi, %edi          // compare %edi and %esi
cmovb  %edi, %eax
ret

and the C code for this is: C代码是:

int foo(unsigned int a, unsigned int b)
{
    if(a < b) 
        return a;
    else
        return 2*b;
}

Can anyone help me understand how cmovb works here? 谁能帮助我了解cmovb在这里的工作方式?

C MOV B = Conditional MOVe if Below (Carry Flag Set). C MOV B =如果低于(有载标志设置),则为有条件的运动。 It literally does what it says, if the condition is met then move. 如果符合条件,则按字面意思进行操作。 The condition is a<b and the value moved is 2*b 条件是a<b ,移动的值是2*b

The ABI stores the return value in %edi , so it first stores a and then conditionally overwrites it with 2*b . ABI将返回值存储在%edi ,因此它首先存储a ,然后有条件地用2*b覆盖它。

Like Jester commented to the question, the cmov* family of instructions are conditional moves, paired via the flags register with a previous (comparison) operation. 就像杰斯特(Jester)对问题的评论一样, cmov*系列指令是有条件的移动,通过标志寄存器与先前的(比较)操作配对。

You can use for example the Intel documentation as a reference for the x86-64/AMD64 instruction set. 您可以使用例如Intel文档作为x86-64 / AMD64指令集的参考。 The conditional move instructions are shown on page 172 of the combined volume. 有条件的移动指令显示在合并卷的第172页上。

cmovb , cmovnae , and cmovc all perform the same way: If the carry flag is set, they move the source operand to the destination operand. cmovbcmovnaecmovc都以相同的方式执行:如果设置了进位标志,它们cmovc源操作数移动到目标操作数。 Otherwise they do nothing. 否则,他们什么也不做。

If we then look at the preceding instructions that affect flags, we'll see that the cmp instruction (the l suffix is part of AT&T syntax, and means the arguments are "longs") changes the set of flags depending on the difference between the two arguments. 然后,如果我们看一下前面的影响标志的指令,我们将看到cmp指令(后缀l是AT&T语法的一部分,并且意味着参数为“ longs”)会根据标志之间的差异来更改标志集。两个论点。 In particular, if the second is smaller than the first (in AT&T syntax), the carry flag is set, otherwise the carry flag is cleared; 特别是,如果第二个小于第一个(在AT&T语法中),则设置进位标志,否则清除进位标志;否则,将进位标志清零。 just as if a subtraction was performed without storing the result anywhere. 就像执行减法而不将结果存储在任何地方一样。 (The cmp instruction affects other flags as well, but they are ignored by the code.) cmp指令也会影响其他标志,但是代码会忽略它们。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM