3.7 Loops: several iterators
push eax
push ecx
call _set_bit
inc bl
movzx edx, bl
add esp, 8
mov ecx, eax
cmp edx, esi
jl SHORT $LL3@form_netma
$LN9@form_netma:
pop esi
mov eax, ecx
pop ebx
ret 0
_form_netmask ENDP
set_bit()is primitive: it just shift left 1 to number of bits we need and then ORs it with the “input” value.
form_netmask()hasaloop: itwillsetasmanybits(startingfromtheMSB)aspassedinthenetmask_bits
argument
3.6.5 Summary
That’s it! We run it and getting:
netmask=255.255.255.0
network address=10.1.2.0
netmask=255.0.0.0
network address=10.0.0.0
netmask=255.255.255.128
network address=10.1.2.0
netmask=255.255.255.192
network address=10.1.2.64
3.7 Loops: several iterators
In most cases loops have only one iterator, but there could be several in the resulting code.
Here is a very simple example:
#include <stdio.h>
void f(int a1, int a2, size_t cnt)
{
size_t i;
// copy from one array to another in some weird scheme
for (i=0; i<cnt; i++)
a1[i3]=a2[i7];
};
There are two multiplications at each iteration and they are costly operations. Can we optimize it some-
how?
Yes, if we notice that both array indices are jumping on values that we can easily calculate without multi-
plication.
3.7.1 Three iterators
Listing 3.13: Optimizing MSVC 2013 x64
f PROC
; RCX=a1
; RDX=a2