Assembly Language for Beginners

(Jeff_L) #1

1.18. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES


32-bit


#include <stdint.h>


int f1(int a)
{
return a*7;
};


int f2(int a)
{
return a*28;
};


int f3(int a)
{
return a*17;
};


x86


Listing 1.192: 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:

Free download pdf