Initializing Structures


Initializing individual fields to 0:
  // struct FILEINFO fi2 = {0, "", {{0, 0, 0}, {0, 0, 0}}}; 
00401D8B   mov         dword ptr [ebp-150h],0
00401D95   mov         cl,byte ptr [string "" (00428d80)]
00401D9B   mov         byte ptr [ebp-14Ch],cl
00401DA1   xor         edx,edx
00401DA3   mov         dword ptr [ebp-14Bh],edx
00401DA9   mov         dword ptr [ebp-147h],edx
00401DAF   mov         dword ptr [ebp-143h],edx
00401DB5   mov         dword ptr [ebp-13Ch],0
00401DBF   mov         dword ptr [ebp-138h],0
00401DC9   mov         dword ptr [ebp-134h],0
00401DD3   mov         dword ptr [ebp-130h],0
00401DDD   mov         dword ptr [ebp-12Ch],0
00401DE7   mov         dword ptr [ebp-128h],0
Initializing all to 0 (compiler optimization)
  // struct FILEINFO fi4 = {0}; 
00401504   mov         dword ptr [ebp-27Ch],0
0040150E   mov         ecx,47h
00401513   xor         eax,eax
00401515   lea         edi,[ebp-278h]
0040151B   rep stos    dword ptr [edi]
Initializing all to 0 at runtime (no optimization)
  // memset(&f5, 0, sizeof(struct FILEINFO)); 
0040151D   push        120h
00401522   push        0
00401524   lea         eax,[ebp-39Ch]
0040152A   push        eax
0040152B   call        memset (00406630)
00401530   add         esp,0Ch


Code for memset:


memset proc
        .FPO    ( 0, 3, 0, 0, 0, 0 )
        mov     edx,[esp + 0ch] ; edx = "count"
        mov     ecx,[esp + 4]   ; ecx points to "dst"
        test    edx,edx         ; 0?
        jz      short toend     ; if so, nothing to do
        xor     eax,eax
        mov     al,[esp + 8]    ; the byte "value" to be stored
; Align address on dword boundary
        push    edi             ; preserve edi
        mov     edi,ecx         ; edi = dest pointer
        cmp     edx,4           ; if it's less then 4 bytes
        jb      tail            ; tail needs edi and edx to be initialized
        neg     ecx
        and     ecx,3           ; ecx = # bytes before dword boundary
        jz      short dwords    ; jump if address already aligned
        sub     edx,ecx         ; edx = adjusted count (for later)
adjust_loop:
        mov     [edi],al
        inc     edi
        dec     ecx
        jnz     adjust_loop
dwords:
; set all 4 bytes of eax to [value]
        mov     ecx,eax         ; ecx=0/0/0/value
        shl     eax,8           ; eax=0/0/value/0
        add     eax,ecx         ; eax=0/0val/val
        mov     ecx,eax         ; ecx=0/0/val/val
        shl     eax,10h         ; eax=val/val/0/0
        add     eax,ecx         ; eax = all 4 bytes = [value]
; Set dword-sized blocks
        mov     ecx,edx         ; move original count to ecx
        and     edx,3           ; prepare in edx byte count (for tail loop)
        shr     ecx,2           ; adjust ecx to be dword count
        jz      tail            ; jump if it was less then 4 bytes
        rep     stosd
main_loop_tail:
        test    edx,edx         ; if there is no tail bytes,
        jz      finish          ; we finish, and it's time to leave
; Set remaining bytes
tail:
        mov     [edi],al        ; set remaining bytes
        inc     edi
        dec     edx             ; if there is some more bytes
        jnz     tail            ; continue to fill them
; Done
finish:
        mov     eax,[esp + 8]   ; return dest pointer
        pop     edi             ; restore edi
        ret
toend:
        mov     eax,[esp + 4]   ; return dest pointer
        ret
memset  endp