Reverse Engineering for Beginners

(avery) #1

CHAPTER 16. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES CHAPTER 16. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES


Listing 16.5: Optimizing MSVC 2012

; a7
_a$ = 8
_f1 PROC
mov ecx, DWORD PTR _a$[esp-4]
; ECX=a
lea eax, DWORD PTR [ecx
8]
; EAX=ECX8
sub eax, ecx
; EAX=EAX-ECX=ECX
8-ECX=ECX7=a7
ret 0
_f1 ENDP


; a28
_a$ = 8
_f2 PROC
mov ecx, DWORD PTR _a$[esp-4]
; ECX=a
lea eax, DWORD PTR [ecx
8]
; EAX=ECX8
sub eax, ecx
; EAX=EAX-ECX=ECX
8-ECX=ECX7=a7
shl eax, 2
; EAX=EAX<<2=(a7)4=a*28
ret 0
_f2 ENDP


; a17
_a$ = 8
_f3 PROC
mov eax, DWORD PTR _a$[esp-4]
; EAX=a
shl eax, 4
; EAX=EAX<<4=EAX
16=a16
add eax, DWORD PTR _a$[esp-4]
; EAX=EAX+a=a
16+a=a*17
ret 0
_f3 ENDP


ARM


Keil for ARM mode takes advantage of the second operand’s shift modifiers:


Listing 16.6: Optimizing Keil 6/2013 (ARM mode)

; a7
||f1|| PROC
RSB r0,r0,r0,LSL #3
; R0=R0<<3-R0=R0
8-R0=a8-a=a7
BX lr
ENDP


; a28
||f2|| PROC
RSB r0,r0,r0,LSL #3
; R0=R0<<3-R0=R0
8-R0=a8-a=a7
LSL r0,r0,#2
; R0=R0<<2=R04=a74=a28
BX lr
ENDP


; a17
||f3|| PROC
ADD r0,r0,r0,LSL #4
; R0=R0+R0<<4=R0+R0
16=R017=a17
BX lr
ENDP

Free download pdf