编译环境:ADS1.2 调试环境:AXD Armulate
发现问题:同一条语句,不同的编译选项编译,执行结果不同。
语句: if((d & 0x0fffffff) == 0x05555550)
选用-O1选项编译时生成代码:(armcc –O1 –g+ -S –fs –cpu ARM7TDMI –o outO1.s test.c) 000004 e1a00200 MOV r0,r0,LSL #4 000008 e1500221 CMP r0,r1,LSR #4 00000c 1a000001 BNE |L1.24| 其中 r0为0xA5555550 r1为0x05555550
执行结果为假: ;r0 中为0xA5555550 MOV r0,r0,LSL #4 ;r0中为0x55555500, r1中为0x05555550, r1,LSR #4 为0x00555555 CMP r0,r1,LSR #4 ;不相等,为假 BNE |L1.24|
选用-O0选项编译时生成代码:(armcc –O0 –g+ -S –fs –cpu ARM7TDMI –o outO0.s test.c) 000000 e1a01000 MOV r1,r0 000004 e1a00201 MOV r0,r1,LSL #4 000008 e59f202c LDR r2,|L1.60| 00000c e1520220 CMP r2,r0,LSR #4 其中 r0为0xA5555550
执行结果为真: ;r0 中为0xA5555550 MOV r1,r0 ;r0和r1中都为0xA5555550 MOV r0,r1,LSL #4 ;r0中为0x55555500 LDR r2,|L1.60| ;r2中为0x05555550 CMP r2,r0,LSR #4 ;r0,LSR #4为0x05555550, 与r2相等,为真
源程序: int test(unsigned int d) { if((d & 0x0fffffff) == 0x05555550) return 1; else return 0; }
int main(void) { int d; d = test(0xA5555550); return d; } 使用-O1选项生成的汇编代码: (armcc –O1 –g+ -S –fs –cpu ARM7TDMI –o outO1.s test.c) ; generated by ARM C Compiler, ADS1.2 [Build 805]
; commandline [-O1 -S -g+ -fs "-IC:Program FilesARMADSv1_2INCLUDE"] CODE32
AREA ||.text||, CODE, READONLY
test PROC ;;;1 int test(unsigned int d) ;;;2 { 000000 e59f1020 LDR r1,|L1.40| ;;;3 //unsigned int bak; ;;;4 ;;;5 if((d & 0x0fffffff) == 0x05555550) 000004 e1a00200 MOV r0,r0,LSL #4 000008 e1500221 CMP r0,r1,LSR #4 00000c 1a000001 BNE |L1.24| ;;;6 return 1; 000010 e3a00001 MOV r0,#1 ;;;7 else ;;;8 return 0; ;;;9 } |L1.20| 000014 e1a0f00e MOV pc,lr |L1.24| 000018 e3a00000 MOV r0,#0 ;8 00001c eafffffc B |L1.20| ;8 ENDP
main PROC ;;;11 int main(void) ;;;12 { 000020 e59f0004 LDR r0,|L1.44| ;;;13 unsigned int d; ;;;14 ;;;15 d = test(0xA5555550); 000024 eafffffe B test |L1.40| 000028 05555550 DCD 0x05555550 |L1.44| 00002c a5555550 DCD 0xa5555550 ;;;16 ;;;17 return d; ;;;18 } ENDP
END
使用-O0选项生成的汇编代码: (armcc –O0 –g+ -S –fs –cpu ARM7TDMI –o outO0.s test.c) ; generated by ARM C Compiler, ADS1.2 [Build 805]
; commandline [-O0 -S -g+ -fs "-IC:Program FilesARMADSv1_2INCLUDE"] CODE32
AREA ||.text||, CODE, READONLY
test PROC ;;;1 int test(unsigned int d) ;;;2 { 000000 e1a01000 MOV r1,r0 ;;;3 //unsigned int bak; ;;;4 ;;;5 if((d & 0x0fffffff) == 0x05555550) 000004 e1a00201 MOV r0,r1,LSL #4 000008 e59f202c LDR r2,|L1.60| 00000c e1520220 CMP r2,r0,LSR #4 000010 1a000001 BNE |L1.28| ;;;6 return 1; 000014 e3a00001 MOV r0,#1 ;;;7 else ;;;8 return 0; ;;;9 } |L1.24| 000018 e1a0f00e MOV pc,lr |L1.28| 00001c e3a00000 MOV r0,#0 ;8 000020 eafffffc B |L1.24| ;8 ENDP
main PROC ;;;11 int main(void) ;;;12 { 000024 e52de004 STR lr,[sp,#-4]! ;;;13 unsigned int d; ;;;14 ;;;15 d = test(0xA5555550); 000028 e59f0010 LDR r0,|L1.64| 00002c ebfffffe BL test 000030 e1a03000 MOV r3,r0 ;;;16 ;;;17 return d; 000034 e1a00003 MOV r0,r3 ;;;18 }000038 e49df004 LDR pc,[sp],#4 |L1.60| 00003c 05555550 DCD 0x05555550 |L1.64| 000040 a5555550 DCD 0xa5555550 ENDP
END
|