Assembly Language for Beginners

(Jeff_L) #1

1.18. REPLACING ARITHMETIC INSTRUCTIONS TO OTHER ONES


; $v0 = $a0<<5 = $a032
sll $a0, 2
; $a0 = $a0<<2 = $a0
4
jr $ra
subu $v0, $a0 ; branch delay slot
; $v0 = $a032-$a04 = $a0*28


_f3:
sll $v0, $a0, 4
; $v0 = $a0<<4 = $a016
jr $ra
addu $v0, $a0 ; branch delay slot
; $v0 = $a0
16+$a0 = $a0*17


64-bit


#include <stdint.h>


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


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


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


x64


Listing 1.196: Optimizing MSVC 2012

; a7
f1:
lea rax, [0+rdi
8]
; RAX=RDI8=a8
sub rax, rdi
; RAX=RAX-RDI=a8-a=a7
ret


; a28
f2:
lea rax, [0+rdi
4]
; RAX=RDI4=a4
sal rdi, 5
; RDI=RDI<<5=RDI32=a32
sub rdi, rax
; RDI=RDI-RAX=a32-a4=a*28
mov rax, rdi
ret


; a17
f3:
mov rax, rdi
sal rax, 4
; RAX=RAX<<4=a
16
add rax, rdi
; RAX=a16+a=a17
ret

Free download pdf