We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello!
I am trying to write a simple delay function in inline assembler, but no matter how hard I try, the complier always modifies my assembly code. My original BNE LOOP command was compiled into a strange combination of BEQ and B instructions:
63: LOOP: 0x000069CC E1A00000 NOP 64: SUBS value,value,#0x00000001 0x000069D0 E2500001 SUBS R0,R0,#0x00000001 0x000069D4 0A000000 BEQ 0x000069DC 65: BNE LOOP 66: END: 67: } 0x000069D8 EAFFFFFC B 0x000069D0 68: } 0x000069DC E12FFF1E BX R14
And when I used the upper (compiler suggested) code as my source, the result was even more absurd:
63: LOOP: 0x000069CC E1A00000 NOP 64: SUBS value,value,#0x00000001 0x000069D0 E2500001 SUBS R0,R0,#0x00000001 0x000069D4 1A000000 BNE 0x000069DC 65: BEQ END 0x000069D8 EA000000 B 0x000069E0 66: B LOOP 0x000069DC EAFFFFFB B 0x000069D0 67: END: 68: } 0x000069E0 E1A00000 NOP 69: } 0x000069E4 E12FFF1E BX R14
Can anyone tell me, why the compiler always tends to modify the user code?!? Can this "optimization" be turned off and is there any other way to avoid such cases in the future?
The code was compiled using ARM realview compiler v. 3.1.0.919.
Kind regards,
Matic
Here is one way:
__asm void delay(int n) { SUBS R0, R0, #1 BNE .-4 }
Thanx, it works, but I would still like to hear an explanation for such behavior.
Regards, Matic
The inline assembler works as advertised, see here: http://www.keil.com/support/man/docs/armcc/armcc_chdgddcj.htm
Among other things, it says:
The inline assembler is a high-level assembler, and the code it generates might not always be exactly what you write. Do not use it to generate more efficient code than the compiler generates. Use embedded assembler or the ARM assembler armasm for this purpose.