1.14. CONDITIONAL JUMPS
|L0.14|
; return
BX lr
ENDP
The functions differ in the branching instruction:BGTandBLT. It’s possible to use conditional suffixes in
ARM mode, so the code is shorter.
MOVccis to be executed only if the condition is met:
Listing 1.133: Optimizing Keil 6/2013 (ARM mode)
my_max PROC
; R0=A
; R1=B
; compare A and B:
CMP r0,r1
; return B instead of A by placing B in R0
; this instruction will trigger only if A<=B (hence, LE - Less or Equal)
; if instruction is not triggered (in case of A>B), A is still in R0 register
MOVLE r0,r1
BX lr
ENDP
my_min PROC
; R0=A
; R1=B
; compare A and B:
CMP r0,r1
; return B instead of A by placing B in R0
; this instruction will trigger only if A>=B (hence, GE - Greater or Equal)
; if instruction is not triggered (in case of A<B), A value is still in R0 register
MOVGE r0,r1
BX lr
ENDP
Optimizing GCC 4.8.1 and optimizing MSVC 2013 can useCMOVccinstruction, which is analogous toMOVcc
in ARM:
Listing 1.134: Optimizing MSVC 2013
my_max:
mov edx, DWORD PTR [esp+4]
mov eax, DWORD PTR [esp+8]
; EDX=A
; EAX=B
; compare A and B:
cmp edx, eax
; if A>=B, load A value into EAX
; the instruction idle if otherwise (if A<B)
cmovge eax, edx
ret
my_min:
mov edx, DWORD PTR [esp+4]
mov eax, DWORD PTR [esp+8]
; EDX=A
; EAX=B
; compare A and B:
cmp edx, eax
; if A<=B, load A value into EAX
; the instruction idle if otherwise (if A>B)
cmovle eax, edx
ret
64-bit
#include <stdint.h>