x86 TEST指令不起作用? [英] x86 TEST instruction not working?

查看:77
本文介绍了x86 TEST指令不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想着把头撞在墙上,这对我来说毫无意义...

I've been banging my head against the wall figuring this out, and this is making no sense to me...

我认为您可以使用test比较两个值是否相等,如此处所示...为什么不能行吗?

I thought you could use test to compare two values for equality, as shown here... why doesn't it work?

int main()
{
    __asm
    {
        mov EAX, 1;
        mov EDX, EAX;
        test EAX, EDX;
L:      jne L;
    }
}

推荐答案

您对TEST指令所做的期望不正确.

Your expectation of what the TEST instruction does is incorrect.

该指令用于执行位测试.您通常会使用它来测试"如果设置了某些位,则给出掩码.它将与JZ(如果为零则跳转)或JNZ(如果非零则为跳转)指令结合使用.

The instruction is used to perform bit tests. You would typically use it to "test" if certain bits are set given a mask. It would be used in conjunction with the JZ (jump if zero) or JNZ (jump if not zero) instructions.

测试涉及对两个操作数执行按位与运算,并设置适当的标志(丢弃结果).如果没有设置掩码中的相应位,则ZF(零标志)将为1(所有位均为零).如果要测试是否设置了任何内容,则可以使用JNZ指令.如果要测试是否未设置任何内容,则可以使用JZ指令.

The test involves performing a bitwise-AND on the two operands and sets the appropriate flags (discarding the result). If none of the corresponding bits in the mask are set, then the ZF (zero flag) will be 1 (all bits are zero). If you wanted to test if any were set, you'd use the JNZ instruction. If you wanted to test if none were set, you'd use the JZ instruction.

JEJNE不适合该指令,因为它们对标志的解释不同.

The JE and JNE are not appropriate for this instruction because they interpret the flags differently.

您正在尝试对某些变量执行相等性检查.您应该使用CMP指令.通常,您将使用它来相互比较值.

You are trying to perform an equality check on some variables. You should be using the CMP instruction. You would typically use it to compare values with each other.

比较有效地减去了操作数,并且只设置了标志(丢弃结果).相等时,两个值的差为0(ZF = 1).不相等时,两个值之差为非零(ZF = 0).如果要测试它们是否相等,可以使用JE(如果相等则跳转)指令.如果要测试它们是否相等,可以使用JNE(如果不相等,则跳转)指令.

The comparison effectively subtracts the operands and only sets the flags (discarding the result). When equal, the difference of the two values is 0 (ZF = 1). When not equal, the difference of the two values is non-zero (ZF = 0). If you wanted to test if they were equal, you'd use the JE (jump if equal) instruction. If you wanted to test if they were not equal, you'd use the JNE (jump if not equal) instruction.

在这种情况下,由于您使用了TEST,因此产生的标志将产生ZF = 0(0x1& 0x1 = 0x1,非零).从ZF = 0开始,JNE指令将采用您在此处看到的分支.

In this case, since you used TEST, the resulting flags would yield ZF = 0 (0x1 & 0x1 = 0x1, non-zero). Since ZF = 0, the JNE instruction would take the branch as you are seeing here.

如果要检查是否相等,则需要使用CMP指令比较这些值,而不是TEST.

You need to compare the values using the CMP instruction if you are checking for equality, not TEST them.

int main()
{
    __asm
    {
        mov EAX, 1
        mov EDX, EAX
        cmp EAX, EDX
L:      jne L          ; no more infinite loop
    }
}

这篇关于x86 TEST指令不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆