Reverse Engineering for Beginners

(avery) #1

CHAPTER 17. FLOATING-POINT UNIT CHAPTER 17. FLOATING-POINT UNIT


VMRS APSR_nzcv, FPSCR
BLE loc_2E08
VLDR D16, [SP,#0x20+a]
VSTR D16, [SP,#0x20+val_to_return]
B loc_2E10

loc_2E08
VLDR D16, [SP,#0x20+b]
VSTR D16, [SP,#0x20+val_to_return]


loc_2E10
VLDR D16, [SP,#0x20+val_to_return]
VMOV R0, R1, D16
MOV SP, R7
LDR R7, [SP+0x20+b],#4
BX LR


Almost the same as we already saw, but there is too much redundant code because theaandbvariables are stored in the
local stack, as well as the return value.


Optimizing Keil 6/2013 (Thumb mode)


Listing 17.22: Optimizing Keil 6/2013 (Thumb mode)
PUSH {R3-R7,LR}
MOVS R4, R2
MOVS R5, R3
MOVS R6, R0
MOVS R7, R1
BL __aeabi_cdrcmple
BCS loc_1C0
MOVS R0, R6
MOVS R1, R7
POP {R3-R7,PC}

loc_1C0
MOVS R0, R4
MOVS R1, R5
POP {R3-R7,PC}


Keil doesn’t generate FPU-instructions since it cannot rely on them being supported on the target CPU, and it cannot be done
by straightforward bitwise comparing. So it calls an external library function to do the comparison:__aeabi_cdrcmple.


N.B. The result of the comparison is to be left in the flags by this function, so the followingBCS(Carry set—Greater than or
equal) instruction can work without any additional code.


17.7.3 ARM64


Optimizing GCC (Linaro) 4.9


d_max:
; D0 - a, D1 - b
fcmpe d0, d1
fcsel d0, d0, d1, gt
; now result in D0
ret


The ARM64ISAhas FPU-instructions which setAPSRthe CPU flags instead ofFPSCRfor convenience. TheFPUis not a
separate device here anymore (at least, logically). Here we seeFCMPE. It compares the two values passed inD0andD1
(which are the first and second arguments of the function) and setsAPSRflags (N, Z, C, V).


FCSEL(Floating Conditional Select) copies the value ofD0orD1intoD0depending on the condition (GT—Greater Than), and
again, it uses flags inAPSRregister instead ofFPSCR. This is much more convenient, compared to the instruction set in
older CPUs.


If the condition is true (GT), then the value ofD0is copied intoD0(i.e., nothing happens). If the condition is not true, the
value ofD1is copied intoD0.

Free download pdf