How can I force the compiler to not generate the "BX LR" for the return function. I also set the option --ARM_only(in compiler + assembler + linker) but this is not useful and I have this code at last in axf file. my target in Keil is set to ARM7TDMI (I can not set my target to ARM7 or any other target without Thumb) so what can we do to prevent generate "BX LR"?
Asked
Active
Viewed 1,524 times
0
-
2What's wrong with `BX LR`? – starblue Jan 13 '13 at 10:10
-
If this is only for one function you can use embedded assembly (search for naked function). – auselen Jan 13 '13 at 17:13
1 Answers
4
BX
won't switch to Thumb mode if the least significant bit of the target address is 0. In other words, it can be used as a regular branch as well.
The instruction cycle times for BX LR
and e.g. MOV PC,LR
are also identical (2S + 1N cycles) on the ARM7TDMI, so there's nothing to be won in terms of performance from using one or the other.

Michael
- 57,169
- 9
- 80
- 125
-
But what can we do if want to have compiler to generate MOV PC,Lr? It seems that all the new compilers generate BX LR for return of the function(http://newsgroups.derkeiler.com/Archive/Comp/comp.sys.arm/2006-04/msg00031.html).In this case can we use linker options to replace the "BX LR" with "MOV PC,LR"? what is the option? I want to have a code without "BX LR"! – user1973744 Jan 13 '13 at 09:26
-
1That link you posted sums it up pretty well; it's standard to use `BX` for returning from a function. (on ARMv4T "Return sequences must restore any saved registers and then **use a bx instruction to return to the caller**", see http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf). You could possibly get a `MOV PC,LR` return instruction if you told the compiler to generate code for ARMv4 (i.e. no `T`). But the question is, why do you not want `BX` in the first place? It works fine, and it's the same size and speed as the `MOV` version. – Michael Jan 13 '13 at 10:32
-
you would need to use an open source compiler and modify the backend. bx lr is the proper instruction for everything including and after the ARMv4T. It would be a broken compiler back end to generate mov pc,lr – old_timer Jan 13 '13 at 19:39
-
you do get a savings if mov pc,lr is okay and that is when you use ldmia with the pc, you save an instruction. if you need bx then you cant use ldmia and it (can) cost you that extra instruction. – old_timer Jan 13 '13 at 20:23