Assembly Language for Beginners

(nextflipdebug2) #1

3.6. NETWORK ADDRESS CALCULATION EXAMPLE



  • Take the fourth (lowest) byte, apply OR operation to this byte and return the value. The return value
    contain the 4th byte now.

  • Take the third byte, shift it left by 8 bits. You’ll get a value like0x0000bb00wherebbis your third byte.
    Apply the OR operation to the resulting value and returning value. The return value has contained
    0x000000aaso far, so ORing the values will produce a value like0x0000bbaa.

  • Take the second byte, shift it left by 16 bits. You’ll get a value like0x00cc0000, whereccis your
    second byte. Apply the OR operation to the resulting value and returning value. The return value
    has contained0x0000bbaaso far, so ORing the values will produce a value like0x00ccbbaa.

  • Takethefirstbyte, shiftitleftby24bits. You’llgetavaluelike0xdd000000, whereddisyourfirstbyte.
    Apply the OR operation to the resulting value and returning value. The return value has contained
    0x00ccbbaaso far, so ORing the values will produce a value like0xddccbbaa.


And this is how it’s done by non-optimizing MSVC 2012:


Listing 3.8: Non-optimizing MSVC 2012

; denote ip1 as "dd", ip2 as "cc", ip3 as "bb", ip4 as "aa".
_ip1$ = 8 ; size = 1
_ip2$ = 12 ; size = 1
_ip3$ = 16 ; size = 1
_ip4$ = 20 ; size = 1
_form_IP PROC
push ebp
mov ebp, esp
movzx eax, BYTE PTR _ip1$[ebp]
; EAX=000000dd
shl eax, 24
; EAX=dd000000
movzx ecx, BYTE PTR _ip2$[ebp]
; ECX=000000cc
shl ecx, 16
; ECX=00cc0000
or eax, ecx
; EAX=ddcc0000
movzx edx, BYTE PTR _ip3$[ebp]
; EDX=000000bb
shl edx, 8
; EDX=0000bb00
or eax, edx
; EAX=ddccbb00
movzx ecx, BYTE PTR _ip4$[ebp]
; ECX=000000aa
or eax, ecx
; EAX=ddccbbaa
pop ebp
ret 0
_form_IP ENDP


Well, the order is different, but, of course, the order of the operations doesn’t matter.


Optimizing MSVC 2012 does essentially the same, but in a different way:


Listing 3.9: Optimizing MSVC 2012 /Ob0

; denote ip1 as "dd", ip2 as "cc", ip3 as "bb", ip4 as "aa".
_ip1$ = 8 ; size = 1
_ip2$ = 12 ; size = 1
_ip3$ = 16 ; size = 1
_ip4$ = 20 ; size = 1
_form_IP PROC
movzx eax, BYTE PTR _ip1$[esp-4]
; EAX=000000dd
movzx ecx, BYTE PTR _ip2$[esp-4]
; ECX=000000cc
shl eax, 8
; EAX=0000dd00
or eax, ecx
; EAX=0000ddcc
movzx ecx, BYTE PTR _ip3$[esp-4]

Free download pdf