【发布时间】:2014-05-24 02:37:52
【问题描述】:
使用下面的代码,我查看了 MS-VC++ 中的反汇编
int main() {
int a = 0x7fffee
,as; //initialization in hex
short b = 0x7fff
,bs;
//the format specifier %hp of %hd prints lower 2bytes only
printf("a(in dec) = %d : b(in dec) = %d \n",a,b);
printf("a(in hex) = %p : b(in hex) = %p \n",a,b);
as = a << 2;
printf("(a << 2) = %p \n",as);
as = (int)b;
printf("(int)b = %p \n",as);
bs = (short)a;
printf("(short)a = %hp \n",bs);
bs = (short)as;
printf("(short)as = %hp \n",bs);
return 0;
}
对以下反汇编特别感兴趣
17: bs = (short)a; //bs gets only lower 2 bytes from a during typecast
0040B7F3 mov dx,word ptr [ebp-4]
0040B7F7 mov word ptr [ebp-10h],dx
为了从 int 类型转换为 short,使用 dx 寄存器。在输出中我看到了
a(in dec) = 8388590 : b(in dec) = 32767
a(in hex) = 007FFFEE : b(in hex) = 00007FFF
(a << 2) = 01FFFFB8
(int)b = 00007FFF
(short)a = 0000FFEE //Interested to know what will be this value in Big Endian mode
(short)as = 00007FFF
Press any key to continue
我想知道
为什么是
(short)a = 0000FFEE而不是(short)a = 007F or 7FFF引用的装配线在 Big Endian 模式下的行为?谁能解释一下,或者我如何将 MS-VC++ 环境中的内存模型设置为大端或小端,以便我检查一下!
【问题讨论】:
-
x86 是小端,不能切换。
-
它是否适用于为 Xolo 型号手机等 Android 操作系统供电的 Atom 系列?
-
是的,它适用于所有 x86 cpu。引用手册,1.3.1 位和字节顺序部分说:
Intel 64 and IA-32 processors are "little endian" machines -
谢谢小丑!我要在 ARM cpu 上试试这个!!
标签: c visual-c++ assembly disassembly