The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: bolzano_1989 on October 18, 2011, 04:34:44 PM

Title: asmintro.chm, Single DWORD copy, 2 DWORD copy each iteration
Post by: bolzano_1989 on October 18, 2011, 04:34:44 PM
In Basic Loop Design section of asmintro.chm of masm32, there're some lines of code:

Single DWORD copy.

      mov esi, src
      mov edi, dst
      mov ecx, count  ; count in ecx
      shr ecx, 2      ; divide by 4

    label:
      mov eax, [esi]
      add esi, 4

      mov [edi], eax
      add edi, 4
      dec ecx
      jnz label


I couldn't understand what is "count in ecx" and why we need to divide this value by 4 in:
Quoteshr ecx, 2

Unrolled to perform 2 DWORD copies each iteration.

      mov esi, src
      mov edi, dst
      mov ecx, count      ; count in ecx
      shr ecx, 3          ; divide by 8

    label:
      mov eax, [esi]      ; read 1st DWORD
      mov ebx, [esi + 4]  ; read 2nd DWORD
      mov [edi], eax      ; write 1st DWORD
      mov [edi + 4], ebx  ; write 2nd DWORD
      add esi, 8          ; add 8 to esi

  add edi, 8          ; add 8 to edi
      dec ecx
      jnz label


I couldn't understand what is "count in ecx" and why we need to divide this value by 8 in:
Quoteshr ecx, 3

Could you explain those things to me  :bg ?
Title: Re: asmintro.chm, Single DWORD copy, 2 DWORD copy each iteration
Post by: jj2007 on October 18, 2011, 04:54:06 PM
Usually you call, for example, a memcopy proc by passing two pointers (src, destination) and a byte count. If every "action" inside your loop copies a DWORD, i.e. 4 bytes, then you must perform less loops.
Title: Re: asmintro.chm, Single DWORD copy, 2 DWORD copy each iteration
Post by: dedndave on October 18, 2011, 04:54:28 PM
the initial count value is the number of bytes
this needs to be converted to the number of loop iterations
for the first loop, 4 bytes are handled in each pass (a dword)
for the second loop, 8 bytes are handled in each pass (2 dwords)
so, the initial count value is shifted to the right, which divides it by powers of 2
Title: Re: asmintro.chm, Single DWORD copy, 2 DWORD copy each iteration
Post by: bolzano_1989 on October 18, 2011, 05:19:20 PM
Thank you, dedndave and jj2007 :U.
"count in ecx" is the number of bytes to be copied :bg .