注意以上三句加红的:
(1)if (a == 0x80):机器码3D 80000000 //用eax可省去寄存器
00401054 3D 80 00 00 00 cmp eax,80h
(2)if (a == (char) 0x80):机器码83 F9 80 //F9=ecx
00401080 83 F9 80 cmp ecx,80h
(3)if (a == (unsigned char) 0x80):机器码81 FA 80000000 //FA=edx
004010AA 81 FA 80 00 00 00 cmp edx,80h
再参考:
IA-32 Intel Architecture Software Developer’s Manual, Volume 2;p3-76表:CMP—Compare Two Operands
Opcode Instruction Description
3C ib CMP AL, imm8 Compare imm8 with AL
3D iw CMP AX, imm16 Compare imm16 with AX
3D id CMP EAX, imm32 Compare imm32 with EAX
80 /7 ib CMP r/m8, imm8 Compare imm8 with r/m8
81 /7 iw CMP r/m16, imm16 Compare imm16 with r/m16
81 /7 id CMP r/m32,imm32 Compare imm32 with r/m32
83 /7 ib CMP r/m16,imm8 Compare imm8 with r/m16
83 /7 ib CMP r/m32,imm8 Compare imm8 with r/m32
38 /r CMP r/m8,r8 Compare r8 with r/m8
39 /r CMP r/m16,r16 Compare r16 with r/m16
39 /r CMP r/m32,r32 Compare r32 with r/m32
3A /r CMP r8,r/m8 Compare r/m8 with r8
3B /r CMP r16,r/m16 Compare r/m16 with r16
3B /r CMP r32,r/m32 Compare r/m32 with r32
段落:Description
..........When an immediate value is used as an operand, it is signextended
to the length of the first operand......
第一个if用的机器码为3D 同时操作数为四字节的80000000 (4字节)=(00000080H)
第二个if用的机器码为83,寄存器 F9,操作数是 80(1字节),此处,由于上面的那句话,可见,此1字节的80会被有符号扩展为32位长度(因为寄存器(操作数1)是32位的) =80FFFFFF(即FFFFFF80H);
第三个if用的机器码为81,寄存器 FA,操作数 80000000(4字节)=(00000080H)
所以,第一、三个if比较是相等的因为:易见寄存器中的值都是00000080;同时立即数是4字节长的00000080H;
而第二个if由于要进行有符号扩展,所以比较结果不等;
注意,第一、三个机器码(3D、81)只接受16/32bit的立即数;而第二个机器码83只接受8bit立即数;
鄙人拙见,不妥之处,望讨论;谢谢关注;