大会比较了解的标志 [英] Assembly comparison flags understanding

查看:99
本文介绍了大会比较了解的标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力了解以下code汇编代码片段:

I am struggling to understand the following code snippet in assembler:

if ( EAX >= 5 )
  EBX = 1;
else
  EBX = 2;"

在这个汇编写的(根据我的书):

In assembler this is written as (according to my book):

1     cmp eax, 5       ;(assuming eax is signed)
2     js signon        ;goto signon if SF = 1
3     jo elseblock     ;goto elseblock if OF = 1 and SF = 0
4     jmp thenblock    ;goto thenblock if SF = 0 and OF = 0
5 signon:
6     jo thenblock     ;goto thenblock if SF = 1 and OF = 1
7 elseblock:
8     mov ebx, 2
9     jmp next
10 thenblock:
11    mov ebx, 1
12    next:

我可以理解所产生的标志可以是:(IF(EAX> = 5)):

I can understand that the resulting flags can be: (if ( EAX >= 5 )):

SF = 0安培; OF = ​​0
但我不明白的标志怎么可以是:
SF = 1和; OF = ​​1?什么计算给出了这样的?

SF = 0 & OF = 0 But I cannot understand how the flags can be: SF = 1 & OF = 1? What computation gives this?

要澄清我的意思是:

如果eax中是在较低的负约束它可能会溢​​出到正通过减去5.如果这将是在上部正约束它不能溢出到负减去5?

If eax is in the lower negative bound it would potentially overflow into the positive by subtracting 5. If it would be in the upper positive bound it could not overflow into the negative by subtracting 5?

感谢您!

推荐答案

要想到这些容易得多3位数字,所有尺度的条款。嗯,如果这是签署(你没有指定你的高层次code /后),那么四位更好,因为你使用了​​5穿行于数近5(这表明ALU的输出)

Much easier to think of these in terms of 3 bit numbers, it all scales. Hmmm, if this is signed (you didnt specify/post in your high level code) then four bits is better because you used a 5. Walk through the numbers near 5 (this shows the output of the alu)

cmp reg,5
0111 - 0101 = 0111 + 1010 + 1 = 10010
0110 - 0101 = 0110 + 1010 + 1 = 10001
0101 - 0101 = 0101 + 1010 + 1 = 10000
0100 - 0101 = 0100 + 1010 + 1 = 01111
0011 - 0101 = 0011 + 1010 + 1 = 01110

现在,你必须了解硬件的工作原理。当你做减法颠倒出​​来ALU的进位标志的一些处理器家族,别人不。无论哪种方式,你绝对可以看到在5状态改变 - 5点。而且你不需要进位标志反正在这里,code犯规使用它。

Now you have to understand how the hardware works. Some processor families when you do a subtract invert the carry flag coming out of the alu, others dont. either way you can definitely see a state change at the 5 - 5 point. And you dont need the carry flag here anyway, code doesnt use it.

如果你正在做数学签字,然后尝试一些负数为好。

In case you are doing signed math, then try some negative numbers as well.

0000 - 0101 = 0000 + 1010 + 1 = 01011  
1111 - 0101 = 1111 + 1010 + 1 = 11010
1110 = 0101 = 1110 + 1010 + 1 = 11001

这揭示了这个问题的一些情况。

And that sheds some light on the problem.

符号溢出是暂时不等于对加法器的最高位,进位输出定义为随身携带。这会导致混乱,所以我们只需要知道那里边界。

signed overflow is defined as the carry in being not equal to the carry out on the msbit of the adder. That can get messy so we just need to know where that boundary is.

0111 - 0101 = 7 - 5 = 2
0110 - 0101 = 6 - 5 = 1
0101 - 0101 = 5 - 5 = 0
0100 - 0101 = 4 - 5 = -1
0011 - 0101 = 3 - 5 = -2

和等。使用该4位模式,在签订间pretation限制在+7(0b0111)下调至-8(0b1000)。所以-3之后 - 5,我们会惹上麻烦:

and so on. Using this 4 bit model, in a signed interpretation we are limited to +7 (0b0111) down to -8 (0b1000). So the after -3 - 5 we will get into trouble:

1110 - 0101 = 1110 + 1010 + 1 = 11001 , -2 - 5 = -7
1101 - 0101 = 1101 + 1010 + 1 = 11000 , -3 - 5 = -8
1100 - 0101 = 1100 + 1010 + 1 = 10111 , -4 - 5 = 7 (-9 if we had more bits)
1011 - 0101 = 1011 + 1010 + 1 = 10110 , -5 - 5 = 6 (-10 if we had more bits)
1010 - 0101 = 1010 + 1010 + 1 = 10101 , -6 - 5 = 5 (-11 if we had more bits)
1001 - 0101 = 1001 + 1010 + 1 = 10100 , -7 - 5 = 4 (-12 if we had more bits)
1000 - 0101 = 1000 + 1010 + 1 = 10011 , -8 - 5 = 3 (-13 if we had more bits)

后者五个是一个带符号的溢出,经签署的结果不能被重新在可用比特数psented $ P $。 (还记得我们打了四位系统现在,这顶位是进位,在视觉上删除它,当你看结果)。

The latter five are a signed overflow, the signed result cannot be represented in the number of bits available. (remember we are playing with a four bit system for now, that top bit is the carry bit, visually remove it when you look at the result).

的签名标记是简单的结果,这也改变了有趣的界限的最高位。的情况下签署的标志,(结果的最高位)被设置为低于5的正极(EAX)值,并且不会导致一个符号溢出(+4至-3)的负数。所有这一切都是在<第5类所以他们希望有2的结果,第一个测试查找标志的地方设置的情况下,为什么要困扰然后测试符号溢出?这是没有意义的,我们已经知道的所有签署结果在不到5类。额外的跳跃,如果符号溢出犯规受伤。

The signed flag is simply the msbit of the result, which is also changing a the interesting boundaries. Cases where the signed flag, (msbit of result) is set is the positive (eax) values below 5 and the negative numbers that do not result in a signed overflow (+4 down to -3). All of which are in the <5 category so they want to have a result of 2. The first test looks for cases where sign is set, why it bothers to then test the signed overflow? That makes no sense, we already know all signed results are in the less than 5 category. the extra jump if signed overflow doesnt hurt.

所以,如果你属于通过js的登记表,然后符号位关闭,比大于或等于5个号码(要1的结果)或结果阴性足以引起符号溢出(需要2的结果)。所以乔elseblock拿起2例(符号溢出,非常消极的)结果不爽这两种情况。 JMP和需要thenblock 5以上的正数。

so if you fall through js signon then the sign bit is off which is numbers greater than or equal to 5 (want a result of 1) or results negative enough to cause a signed overflow (want a result of 2). so jo elseblock sorts these two cases out by picking up the result of 2 cases (signed overflow, very negative). and jmp thenblock takes the positive numbers above 5.

在我看来像你在这里干什么签署数学(使用签名的溢出标志有点明显)。由于您使用的是5来比较并签署数学,你需要4个或更多位在系统中实现这个code,因此8,32,64,123456位,它不事这一切工作一样的4位系统(此对比)。我发现很容易减少的比特数做了分析。像这样的硬codeD比较使它更加容易,如上面的手计算结果略高于在,甚至更低。然后通过全零(零)为全1(负一)走了符号数,而且非常消极到符号溢出范围。对于无符号数实在是有点更容易,但同样的过程。

It looks to me like you are doing signed math here (somewhat obvious from using the signed overflow flag). Since you are using a 5 to compare against and signed math, you need 4 or more bits in your system to implement this code, so 8, 32, 64, 123456 bits, it doesnt matter it all works the same as a 4 bit system (for this comparision). I find it easier to minimize the number of bits to do the analysis. Hardcoded comparisons like this make it that much easier, as above hand compute results just above, at, and below. then walk through the all zeros (zero) to all ones (minus one) for signed numbers, and very negative into the signed overflow range. for unsigned numbers it is a bit easier but the same process.

这篇关于大会比较了解的标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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