x86汇编语言 - 试运行和对标志的影响 [英] x86 assembly Language - TEST operation and its effect on flags

查看:229
本文介绍了x86汇编语言 - 试运行和对标志的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在x86汇编语言的家庭作业和不理解的测试操作究竟是如何影响进位,零,并签署标志。据我所知,我们正在做的按位比较两个操作数。在第一个例子中,比特1,2,3,4,和7中的匹配起来。这是否意味着的结果和是11110010?这将设置符号标志,以消极权利?零标志将不被设置,因为这二进制结果不为0和进位?我真的不知道如何着手。感谢您的帮助。

  MOV人,00001111b
测试人,00000010B;一个。 CF = ZF = SF =< BR>< BR>MOV人,00000110b
CMP人,00000101b;湾CF = ZF = SF =< BR>< BR>MOV人,00000101b
CMP人,00000111b; C。 CF = ZF = SF =< BR>< BR>


解决方案

你写的,86 TEST 操作执行按位操作。你应该再次检查真值表和:

 输入输出
A B A和B
0 0 0
0 1 0
1 0 0
1 1 1

测试将在SRC每次2的对应位和DST操作数,所以只有位是做到这一点的操作 1 在这两个将成为 1 在结果中: 00001111b&安培; 00000010B 只会给 00000010B

上的标志的作用是现在看简单 - ZF = 0,因为其结果是非零,SF = 0,因为其结果的MSB是关闭的,和CF = 0,因为测试不会将它(这是一个合理的操作,而不是算术一个)。

顺便说一句, TEST 是相当便宜的运作去,所以你可能会注意到它经常被用来作为一个简单的零检查 - 测试RAX, RAX 将和反对本身(造成当然相同的值),所以你得到一个不错的方法来检查,如果RAX为零(对于如由一个 JE 分支之后),或负(通过使用SF以 JS 分公司)

现在,其他2个问题涉及另一个操作 - CMP 做一个实际的减法(它做同样的事情 SUB ,也丢弃结果,仅更新标志)。


  • 第一种办法计算 00000110b - 00000101b = 00000001B 与ZF = SF = 0(为与上述相同的原因),和CF = 0,因为我们没有任何需要进位/借。


  • 第二个会计算 00000101b - 00000111b = 11111110 (补重presentation为 5 - 7 = -2 ),ZF仍然是0同上,但这个时候你会看到SF = 1,因为我们得到了一个否定的结果使MSB开启,CF = 1,因为计算做了一个借捉奸最高位。


有一个有点细点就在这里CF和其(溢出标志)对应的 - 这些数字只是数字,直到你决定使用它们的方式他们并不意味着符号或无符号值。但是86有保有任何可能性,正确的标志,因此,它基本上使用CF无符号运算,有效地意味着最后一次操作是 5 - 7 = 254 ,如果你借一个额外的位到MSB(这是什么CF = 1这里标记)中。该不会设置,因为如果你考虑这些完全相同的操作符号算术,你真的做了 5 - 7 = -2 这是完全合法的和没 ŧ溢/下溢。

在另一方面,如的操作127 + 127 = 254 会做相反的,它不会切换CF(因为没有什么不好的事情,如果你考虑这是无符号算术),但会得到,因为设置如果这些符号的值,你刚才说的 127 + 127 = -2 这显然是错误的,因为有过去最大符号值溢出一个字节可以存储(127)

I'm working on an x86 assembly language homework assignment and do not understand how exactly the test operation effects the carry, zero and sign flags. As I understand it, we are doing a bitwise comparison AND on two operands. In the first example, bits 1,2,3,4, and 7 match up. Does this mean that the result of the AND is 11110010? This would set the sign flag to negative right? Zero flag would not be set because this binary result is not 0. And the carry flag? I'm not really sure how to proceed. Thanks for the help.

mov al,00001111b
test al,00000010b ; a. CF= ZF= SF=<br><br>

mov al,00000110b
cmp al,00000101b  ; b. CF= ZF= SF=<br><br>

mov al,00000101b
cmp al,00000111b  ; c. CF= ZF= SF=<br><br>

解决方案

As you wrote, x86 TEST operation does a bitwise AND operation. You should check again the truth table for AND:

INPUT   OUTPUT
A   B   A AND B
0   0       0
0   1       0
1   0       0
1   1       1

TEST would do this operation for each 2 corresponding bits in the src and the dst operands, so only bits that are 1 in both would become 1 in the result: 00001111b & 00000010b would simply give 00000010b.

The effect on the flags is now simple to see - ZF=0 since the result is non-zero, SF=0 since the result MSB is off, and CF=0 because TEST won't set it (it's a logical operation, not an arithmetic one).

By the way, TEST is quite cheap as operations go, so you may notice it's used often as a simple zero check - TEST RAX, RAX would AND the RAX register against itself (resulting in the same value of course), so you get a nice way to check if RAX is zero (for e.g. to be used by a je branch immediately after), or negative (by using the SF with a js branch)

Now, the other 2 questions deal with another operation - CMP does an actual subtraction (it's doing the same thing as SUB, but also discards the result, only updating the flags).

  • The first would compute 00000110b - 00000101b = 00000001b, with ZF=SF=0 (for the same reasons as above), and CF=0 since we didn't have any need for carry/borrow.

  • The second would compute 00000101b - 00000111b = 11111110 (two's complement representation for 5 - 7 = -2), ZF is still 0 as above, but this time you'd see SF=1 since we got a negative result so the MSB is on, and CF=1 since the calculation did a "borrow" unto the MSB.

There's a bit of a fine point here regarding CF and its counterpart OF (overflow flag) - the numbers are just numbers, they don't mean signed or unsigned values until you decide to use them as such. However x86 has to maintain the correct flags for any possibility, so it basically uses CF for unsigned operations, effectively meaning that the last operation was 5 - 7 = 254 as if you "borrowed" an extra bit to the MSB (and this is what CF=1 marks here). The OF won't get set because if you consider these exact same operation as signed arithmetic, you've really done 5 - 7 = -2 which is perfectly legal and didn't overflow/underflow.

On the other hand, an operation such as 127 + 127 = 254 would do the opposite, it won't switch the CF (because nothing bad happened if you consider this as unsigned arithmetics), but the OF will get set because if these are signed values, you've just said 127 + 127 = -2 which is obviously wrong because there was an overflow past the max signed value a byte can store (127)

这篇关于x86汇编语言 - 试运行和对标志的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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