According to X-Calibre, repeating string instructions are slow on PPRO+. But anyway, it would be more effective to use dword movements rather than byte ones as have been demonstrated through replies to this thread.
With rep, you can do:


push edi ; let's not forget to save volative registers
xor eax, eax ; we're storing 0s
mov ecx, sizeof mybuffer shr 2 ; how many dwords to clear
mov edi, OFFSET mybuffer
rep stosd
pop edi


You might want to complete the storing with bytes if the buffer is not dword aligned like so:


push edi ; let's not forget to save volative registers
xor eax, eax ; we're storing 0s
mov ecx, sizeof mybuffer shr 2 ; how many dwords to clear
mov edi, OFFSET mybuffer
rep stosd
mov ecx, (sizeof mybuffer) AND 3
rep stosb
pop edi


With standard looping it can be accomplished like this:


mov edx, OFFSET mybuffer - 4
mov ecx, sizeof mybuffer shr 2
xor eax, eax
@@st: mov dword ptr [edx+ecx*4], eax
dec ecx
jnz @@st


This succesfully clears mybuffer and trashes edx, ecx and eax while doing so. You might want to go with this code to preserve as much registers as possible:


mov ecx, sizeof mybuffer shr 2
@@st: mov [mybuffer-4+ecx*4], 0
dec ecx
jnz @@st

This only modifies ecx.

Happy coding.
Posted on 2001-07-18 19:27:43 by comrade
Oh and don't really use RtlZeroMemory() as SaVaGe suggested. It is okay, but if you are using assembly it would be best if you cleared memory. You will save some overhead too.
Posted on 2001-07-18 19:28:34 by comrade
bAZiK,

For simplicity, run a STOSD loop. It is to your advantage to make the buffer in DWORD intervals anyway and this means you don't have to clean up bytes at the end.

Current optimisation on later machines in hardware make STOSD combined with REP reasonably fast, both testing and literature like Agner Fog's optimisation manual show that string instructions by themselves are slow but both MOVSD and STOSD are fast when used with the repeat prefix.

If size is not a problem, the runtime in the MASM32 library is fast enough, its an unrolled loop of 8 x DWORD writes and the code is in the MASM32 library directory. The procedure is "memfill".

Regards,

hutch@pbq.com.au
Posted on 2001-07-19 03:03:28 by hutch--
Ok, I solved my Problem! Thanx all for your replies! (I love this MessageBoard )
Posted on 2001-07-19 07:55:29 by bazik
comrade, please do explain why using an api in this situation is not a good idea.



...
.data?
mybuffer db 512 dup (?)
...

.code
...
mov mybuffer, szAnyTextFromControl
...
; ---- clear buffer here
...
invoke SendMessage, hEdit, WM_SETTEXT, addr mybuffer, 512
; editbox should be empty


i would only considered as a bad idea to use api if it's in a loop and the code for clearing a buffer is being use more than once. there's no point of not using api if you just need to clear the buffer once.

my suggestion is, use api if you're planning to clear the buffer once in a while when speed is not a factor.

-- -----------------something for you to work on comrade--------


push edi ; let's not forget to save volative registers
xor eax, eax ; we're storing 0s
mov ecx, sizeof mybuffer shr 2 ; how many dwords to clear
mov edi, OFFSET mybuffer
rep stosd
pop edi

------and--------------------

mov edx, OFFSET mybuffer - 4
mov ecx, sizeof mybuffer shr 2
xor eax, eax
@@st: mov dword ptr [edx+ecx*4], eax
dec ecx
jnz @@st

this code has problem. it will empty the buffer, except 2 char at the end.





mov ecx, sizeof mybuffer shr 2
@@st: mov [mybuffer-4+ecx*4], 0
dec ecx
jnz @@st

this code also has problem. in that it only empty half of the buffer.


those are not big deal. just something for you to look out.
Posted on 2001-07-19 09:19:37 by disease_2000
Do not forget dword ptr after the mov :)
Posted on 2001-07-19 14:14:53 by comrade
If it is a text buffer, you can clear it this way :

mov buffer[0], 0

(s)
Posted on 2001-07-20 11:17:58 by (scalp)