a = -2147483648 - a;编译器优化
我正在尝试学习如何对软件进行逆向工程以及所有技巧来了解编译器优化之前代码的外观。
我多次发现类似的情况:
if (a < 0)
a = -2147483648 - a;
我最初认为这是一个 abs()
:下溢,因此您可以获得正值。但由于 a
是负数(参见 if),这相当于:
if (a < 0)
a = -2147483648 + abs(a);
这将是一个非常小的负数,并且根本不是 a
的绝对值。我缺少什么?
I'm trying to learn how to reverse engineer software and all the tricks to understand how the code looks like before the compiler optimizations.
I found something like this several times:
if (a < 0)
a = -2147483648 - a;
I originally thought it was an abs()
: a underflows so you get the positive value. But since a
is negative (see the if), this is equivalent to:
if (a < 0)
a = -2147483648 + abs(a);
Which will be a very small negative number, and not the absolute value of a
at all. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它正在转换数字,以便第 31 位成为符号位,其余位 (0...30) 表示绝对大小。例如如果a = -5,那么运算后它就变成0x80000005。
It is converting the number so that bit 31 becomes a sign bit, and the rest bits (0...30) denotes the absolute magnitude. e.g. if a = -5, then after the operation it becomes 0x80000005.
它似乎从 2 的补码 转换为 符号幅度
It appears to be converting from 2's complement to sign-magnitude
也许: http://en.wikipedia.org/wiki/Two%27s_complement ?
Maybe: http://en.wikipedia.org/wiki/Two%27s_complement ?
我真诚地希望原始来源说的是 0x80000000 而不是 -2147483648 !十六进制数字至少给读者一个线索。小数是非常神秘的。
I sincerely hope that the original source said 0x80000000 and not -2147483648 ! The hex number at least gives the reader a clue. The decimal is very cryptic.