Assembly Language for Beginners

(Jeff_L) #1

1.22. MANIPULATING SPECIFIC BIT(S)


dec r8 ; R8--
jne SHORT $LL4@f
fatret 0
f ENDP


Here theROLinstruction is used instead ofSHL, which is in fact “rotate left” instead of “shift left”, but in
this example it works just asSHL.


You can read more about the rotate instruction here:.1.6 on page 1034.


R8here is counting from 64 to 0. It’s just like an invertedi.


Here is a table of some registers during the execution:


RDX R8
0x0000000000000001 64
0x0000000000000002 63
0x0000000000000004 62
0x0000000000000008 61
... ...
0x4000000000000000 2
0x8000000000000000 1

At the end we see theFATRETinstruction, which was explained here:1.22.5 on the previous page.


Optimizing MSVC 2012


Listing 1.293: Optimizing MSVC 2012

a$ = 8
f PROC
; RCX = input value
xor eax, eax
mov edx, 1
lea r8d, QWORD PTR [rax+32]
; EDX = 1, R8D = 32
npad 5
$LL4@f:
; pass 1 ------------------------------
test rdx, rcx
je SHORT $LN3@f
inc eax ; rt++
$LN3@f:
rol rdx, 1 ; RDX=RDX<<1
; -------------------------------------
; pass 2 ------------------------------
test rdx, rcx
je SHORT $LN11@f
inc eax ; rt++
$LN11@f:
rol rdx, 1 ; RDX=RDX<<1
; -------------------------------------
dec r8 ; R8--
jne SHORT $LL4@f
fatret 0
f ENDP


Optimizing MSVC 2012 does almost the same job as optimizing MSVC 2010, but somehow, it generates
two identical loop bodies and the loop count is now 32 instead of 64.


To be honest, it’s not possible to say why. Some optimization trick? Maybe it’s better for the loop body to
be slightly longer?


Anyway, such code is relevant here to show that sometimes the compiler output may be really weird and
illogical, but perfectly working.

Free download pdf