在反汇编代码中有符号和无符号之间有什么不同? [英] what is different between signed and unsigned in disassembly code ?

查看:78
本文介绍了在反汇编代码中有符号和无符号之间有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是第一个代码::

This is First Code ::

     unsigned int F1 = 2147483647;
01092C0E  mov         dword ptr [F1],7FFFFFFFh
     unsigned int F2 = 2147483647;
01092C15  mov         dword ptr [F2],7FFFFFFFh

     unsigned int F3=F1+F2;
01092C1C  mov         eax,dword ptr [F1]
01092C1F  add         eax,dword ptr [F2]
01092C22  mov         dword ptr [F3],eax



结果是:4294967294



这是第二代码::




the result is : 4294967294

This is Second Code ::

     int F1 = 2147483647;
010A6ECE  mov         dword ptr [F1],7FFFFFFFh
     int F2 = 2147483647;
010A6ED5  mov         dword ptr [F2],7FFFFFFFh

     int F3=F1+F2;
010A6EDC  mov         eax,dword ptr [F1]
010A6EDF  add         eax,dword ptr [F2]
010A6EE2  mov         dword ptr [F3],eax





结果是:-2







如何sembly知道这是签名还是无符号数?



the result is : -2



How assembly Know if this is Signed or Unsigned number ??

推荐答案

我害怕解决方案1和解决方案2,原则上是正确的,没有发现事物的本质。这是诀窍:整数的二进制表示是专门设计的方式忽略无符号数之间的差异。这种表现在几乎所有现代系统中都占主导地位,被称为2补。以下是它的工作原理: http://en.wikipedia.org/wiki/2%27s_complement [ ^ ]。



这是一个想法:让我们说,你有11111111的字节的二进制表示(任何大小的整数,实际上,这只是一个例子)。它表示无符号255 = 0xFF或有符号-1。不同的数学值,对吧?让我们添加一些其他字节,比如说,(有符号和无符号的数学值相同)。让我们正式添加它们,使用学校列方法并在溢出的情况下继续留下位:

I'm afraid the Solution 1 and Solution 2, correct in principle, did not uncovered the essence of things. Here is the trick: the binary representation of integer numbers is specially designed the way ignoring the difference between unsigned numbers. This representation is dominated in almost all modern systems and is called "2's complement". Here is how it works: http://en.wikipedia.org/wiki/2%27s_complement[^].

Here is the idea: let's say, you have the binary representation of byte (integer number of any size, actually, this is only an example) of 11111111. It represents unsigned 255=0xFF or signed −1. Different mathematical values, right? Let's add some other byte to is, say, (same mathematical value for both signed and unsigned). Let's add them formally, using school "column" method and carrying over to left the bit in case of overflow:
 11111111
+
 00000011 = 3
=========
 00000010 = 2

In无符号和无符号表示,我们有相同的结果(在无符号的情况下,我们也有环绕效果:将1增量最大值255加到0,加2使结果等于2)。 br />


在其他一些情况下,我们得到不同的数学值,但二元结果相同。例如:

In both signed and unsigned representation, we have the same result (in case of unsigned, we also have "wrap around" effect: adding 1 "increment" maximum value of 255 to 0, and adding 2 makes the result equals to 2).

In some other cases, we get different mathematical values but the same binary result. For example:

 11110110
+
 00000010 = 2
=========
 11111000



在有符号值中,它表示-10 + 2 = -8,无符号:0xF6 + 2 = 0xF8,或246 + 2 = 248 。



您可以检查并查看在所有其他整数算术运算中发生的情况。如您所见,CPU不关心有符号和无符号表示;所有的计算都是一样的,只有结果的解释是不同的。您还可以检查是否可以在同一表达式中混合有符号和无符号值,但需要注意不要越过最大值和最小值的障碍,这当然是不同的。



另外,您应该理解带符号负值的绝对值与带有翻转符号位的带符号值不同。如果你有-1,翻转符号位将不会给你1,它将是127.它有重要意义:没有二进制两个表示为零,-0和+0具有相同的表示。



-SA


无符号变量确实代表正值,包括0.

签名数据类型用于可以包含正数和负数的值。例如,如果您希望代表您的银行帐户信息,则表示为签名号码很有用。无符号数通常用作不能保持负值的数组索引。



对于计算机,如果你使用的是有符号或无符号版本,它没有任何区别数据类型,关于内存空间或性能。
An unsigned variable indeed represents positive values including 0.
The signed data type is used for values that can hold both positive and negative numbers. Say for example if you wish to represent your bank account information, it is useful to be represented as a signed number. Unsigned numbers are usually used as array indices which can't hold negative values.

"For the computer however it makes no difference if you use signed or unsigned version of the data type, regarding the memory space or performance."


程序集不知道任何内容。编译器为有符号或无符号数据的操作生成适当的代码。在您显示的代码中,不需要为每种情况做任何不同的事情,因此您会看到相同的消息。 是否当显示数字时,它的解释方式不同。



产生不同代码的代码示例之一是使用位移运算符>> (向右移动)



代码:

The assembly doesn't 'know' anything. The compiler generates the appropriate code for operations on signed or unsigned data. In the code you've shown, there is no need to do anything different for each case, and so you see the same dissasembly. Whether When the number is displayed its simply interpreted differently.

One example of code that will produce different code is using the bit-shift operator >> (shift right)

Code:
int main()
{
    int i = -2;
    unsigned int j = -2;

    i = i>>1;
    j = j>>1;

    printf("int i = %d\n", i);
    printf("unsigned int j = %u\n", j);

    return 0;
}





输出:



Output:

int i = -1
unsigned int j = 2147483647







反汇编:




Disassembly:

CPU Disasm
Hex dump       Command                                                Comments
C74424 1C FEF MOV DWORD PTR SS:[LOCAL.1],-2
C74424 18 FEF MOV DWORD PTR SS:[LOCAL.2],-2

-----------------------------------------------------------------------
D17C24 1C     SAR DWORD PTR SS:[LOCAL.1],1
D16C24 18     SHR DWORD PTR SS:[LOCAL.2],1
-----------------------------------------------------------------------

8B4424 1C     MOV EAX,DWORD PTR SS:[LOCAL.1]
894424 04     MOV DWORD PTR SS:[LOCAL.7],EAX                       ;<%d> => -2.
C70424 643040 MOV DWORD PTR SS:[LOCAL.8],OFFSET 00403064           ;fmt = "int i = %d
"
E8 60080000   CALL <JMP.&msvcrt.printf>                            ;MSVCRT.printf
8B4424 18     MOV EAX,DWORD PTR SS:[LOCAL.2]
894424 04     MOV DWORD PTR SS:[LOCAL.7],EAX                       ;<%u> => 4294967294.
C70424 703040 MOV DWORD PTR SS:[LOCAL.8],OFFSET 00403070           ;fmt = "unsigned int j = %u
"
E8 4C080000   CALL <JMP.&msvcrt.printf>                            ;MSVCRT.printf
B8 00000000   MOV EAX,0
C9            LEAVE
C3            RETN







在无符号整数的情况下,右移意味着我们(a)移除LSB,(b)将所有剩余位1移位到右边然后(c)填充MSB一个0.



但是,在签名int的情况下,所需的行为是不同的。步骤(a)和(b)保持不变,但步骤(c)可能插入1或0 - 这取决于数字是否为负数。负数将插入1,正数将插入0.



无符号整数的指令是SHR(右移),而指令对于有符号的int是SAR(符号调整(移位)右)。




In the case of an unsigned int, shifting right just means that we (a) remove the LSB, (b) shift all of the remaining bits 1 position to the right and then (c) fill the MSB with a 0.

However, in the case of a signed int the required behaviour is different. Steps (a) and (b) remain the same, but step (c) may have a 1 or a 0 inserted - this depends on whether or not the number is negative. A negative number will have a 1 inserted, a positive one will have a 0 inserted.

The instruction for an unsigned int is SHR (Shift Right), while the instruction for a signed int is SAR (Sign Adjust (shift)Right).


这篇关于在反汇编代码中有符号和无符号之间有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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