Assembly Language for Beginners

(nextflipdebug2) #1

3.6. NETWORK ADDRESS CALCULATION EXAMPLE


Optimizing MSVC 2012 does almost the same, but without unnecessary reloading of the input value:


Listing 3.11: Optimizing MSVC 2012 /Ob0

_a$ = 8 ; size = 4
_print_as_IP PROC
mov ecx, DWORD PTR _a$[esp-4]
; ECX=ddccbbaa
movzx eax, cl
; EAX=000000aa
push eax
mov eax, ecx
; EAX=ddccbbaa
shr eax, 8
; EAX=00ddccbb
and eax, 255
; EAX=000000bb
push eax
mov eax, ecx
; EAX=ddccbbaa
shr eax, 16
; EAX=0000ddcc
and eax, 255
; EAX=000000cc
push eax
; ECX=ddccbbaa
shr ecx, 24
; ECX=000000dd
push ecx
push OFFSET $SG3020 ; '%d.%d.%d.%d'
call DWORD PTR impprintf
add esp, 20
ret 0
_print_as_IP ENDP


3.6.4 form_netmask() and set_bit().


form_netmask()makes a network mask value fromCIDRnotation. Of course, it would be much effective
to use here some kind of a precalculated table, but we consider it in this way intentionally, to demonstrate
bit shifts.


We will also write a separate functionset_bit(). It’s a not very good idea to create a function for such
primitive operation, but it would be easy to understand how it all works.


Listing 3.12: Optimizing MSVC 2012 /Ob0

_input$ = 8 ; size = 4
_bit$ = 12 ; size = 4
_set_bit PROC
mov ecx, DWORD PTR _bit$[esp-4]
mov eax, 1
shl eax, cl
or eax, DWORD PTR _input$[esp-4]
ret 0
_set_bit ENDP


_netmask_bits$ = 8 ; size = 1
_form_netmask PROC
push ebx
push esi
movzx esi, BYTE PTR _netmask_bits$[esp+4]
xor ecx, ecx
xor bl, bl
test esi, esi
jle SHORT $LN9@form_netma
xor edx, edx
$LL3@form_netma:
mov eax, 31
sub eax, edx

Free download pdf