[英]How does the assembly TEST instruction work with these jump instructions?
Using AT&T assembly syntax, I'm trying to understand how testl
is used in assembly code. 使用AT&T汇编语法,我试图理解如何在汇编代码中使用
testl
。 Specifically: 特别:
testl %edx, %edx
jle .L3
I know testl
does a bitwise and
of the same value to set the condition flags, but how can I interpret 'jump if less than or equal to' if it isn't comparing two values? 我知道
testl
按位and
相同的值设置条件标志,但如果不比较两个值,我如何解释'如果小于或等于'则跳转?
Here's an excerpt from the official documentation from Intel on test: 以下是英特尔测试官方文档的摘录:
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;
And the same on jle: 在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
). 因此,如果
edx
为0,则执行跳转(因为edx AND edx = edx
,仅当edx
为0时为0,并且当AND
的结果为0时ZF
设置为1)或者edx
的最重要位为1(因为SF = most significant bit
edx AND edx
SF = most significant bit
(或等效地, edx
本身)和OF
始终为0,这意味着SF ≠ OF
仅在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. IOW,仅当
edx
被解释为有符号整数时edx
时才执行跳转,或等效地,当edx
被解释为无符号整数时为0或更大或等于0x80000000时。
In x86 assembly almost all conditional jumps are based on flags (except jcxz
, jecxz
, loop
and loopne
/ loopnz
). 在x86汇编中,几乎所有条件跳转都基于标志(
jcxz
, jecxz
, loop
和loopne
/ loopnz
)。 This means that all that matters are the values the flags have. 这意味着重要的是标志所具有的值。
jle
is synonymous with jng
. jle
是同义jng
。 The jump condition is ZF = 1 or SF <> OF
. 跳转条件是
ZF = 1 or SF <> OF
。 You may want to check Intel x86 JUMP quick reference . 您可能需要查看Intel x86 JUMP快速参考 。
test
does set all flags except AF
link , so everything looks good so far. test
确实设置了除AF
链接之外的所有标志,所以到目前为止所有内容都很好
According to this link logical operations always zero OF
. 根据此链接,逻辑运算始终为零
OF
。 That means your jump would practically be ZF = 1 or SF = 1
, so in your code jle
would jump if edx
was 0
or between range 0x80000000
... 0xffffffff
. 这意味着你的跳转几乎是
ZF = 1 or SF = 1
,所以在你的代码中,如果edx
为0
或者在0x80000000
... 0xffffffff
范围内, jle
会跳转。
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). 具有相同参数的
TESTL
(如edx
和edx
)根据该参数本身的值设置标志(因为x AND x与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
. 所以我们可以完全忘记
AND
,因为它被丢弃了 - 我们需要关注的是edx
的价值。
With TESTL
, the zero flag ZF
is set to 1 only if the value is zero. 使用
TESTL
,仅当值为零时,零标志ZF
才设置为1。 TESTL
also forces the overflow flag OF
to 0 and sets the sign flag SF
only if the high bit is set. TESTL
还强制溢出标志OF
为0,并且仅在高位置1时才设置符号标志SF
。
JLE
will then jump if either ZF
is set to 1, or SF <> OF
. 如果
ZF
设置为1或SF <> OF
,则JLE
将跳转。
So, the jump will execute if either: 因此,如果有以下情况,跳转将执行:
edx
was zero; edx
为零; or edx
had its high bit set. edx
位设置很高。 Hence it will jump for edx
values of 0
or 0x80000000 - 0xffffffff
. 因此,它将跳转为
edx
值0
或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: 很可能这是一个检查,以确保该数字是一个自然数
0x00000001 - 0x7fffffff
,跳转将是某种错误处理例程,一个有效的自然数将继续没有跳转,如:
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 . 有关各种跳转及其使用的标志的说明,请参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.