6

使用 AT&T 汇编语法,我试图了解如何testl在汇编代码中使用。具体来说:

testl  %edx, %edx
jle    .L3

我知道testl按位and对相同的值设置条件标志,但是如果不比较两个值,我如何解释“小于或等于时跳转”?

4

3 回答 3

12

以下是英特尔在测试中官方文档的摘录:

Operation
TEMP ← SRC1 AND SRC2;
SF ← MSB(TEMP);
IF TEMP = 0
    THEN ZF ← 1;
    ELSE ZF ← 0;
FI:
PF ← BitwiseXNOR(TEMP[0:7]);
CF ← 0;
OF ← 0;

在 jle 上也是如此:

Jump if less or equal (ZF=1 or SF≠OF)

So, the jump will be performed if edx is 0 (because edx AND edx = edx and that's 0 only when edx is 0, and because ZF is set to 1 when the result of AND is 0) or if the most significant bit of edx is 1 (because SF = most significant bit of edx AND edx (or, equivalently, of edx itself) and OF is always 0, which means SF ≠ OF is only true when SF ≠ 0).

IOW, the jump will be performed only if edx is ≤ 0 when interpreted as a signed integer or, equivalently, when edx is either 0 or greater or equal than 0x80000000 when interpreted as an unsigned integer.

于 2013-01-30T03:00:03.667 回答
4

TESTL with identical arguments (like edx and edx) sets the flags based on the value of that argument itself (since x AND x is identical to x). So we can forget about the AND altogether here since it's discarded - all we need to concern ourselves with is the value in edx.

With TESTL, the zero flag ZF is set to 1 only if the value is zero. TESTL also forces the overflow flag OF to 0 and sets the sign flag SF only if the high bit is set.

JLE will then jump if either ZF is set to 1, or SF <> OF.

So, the jump will execute if either:

  • edx was zero; or
  • edx had its high bit set.

Hence it will jump for edx values of 0 or 0x80000000 - 0xffffffff.

Most likely this is a check to ensure that the number is a natural number 0x00000001 - 0x7fffffff, the jump would be to an error handling routine of some sort and a valid natural number would continue without the jump, something like:

loop_for_number:
    call   get_number_into_edx
    testl  %edx, %edx
    jle    loop_for_number

    ; carry on here knowing that edx >= 1

For a description of the various jumps and the flags they use, see here.

于 2013-01-30T03:03:36.250 回答
3

在 x86 汇编中,几乎所有条件跳转都基于标志(除了jcxzjecxzloop/ loopneloopnz。这意味着所有重要的是标志的值。

jle是 的同义词jng。跳转条件为ZF = 1 or SF <> OF。您可能需要查看Intel x86 JUMP 快速参考

testAF 确实设置了除link之外的所有标志,所以到目前为止一切看起来都很好。

根据此链接,逻辑运算始终为零OF。这意味着您的跳转实际上会是ZF = 1 or SF = 1,因此在您的代码jle中如果edx0或在范围之间会跳转0x80000000...。0xffffffff

于 2013-01-30T02:57:17.023 回答