News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Clear Buffer?

Started by Cyrus, November 18, 2007, 06:00:12 AM

Previous topic - Next topic

Cyrus

Can someone please show me the syntax to empty/clear a buffer?

For example LOCAL aBuffer :DWORD

invoke somecall,addr aBuffer

Now aBuffer would hold that returned value from the invoke. Now what would I do to make aBuffer empty again?

Thank you in advance.

u

In this 4-byte case, just
mov aBuffer,0

otherwise, use "rep movsd" and "rep movsb"



Clear macro What
local How
How=sizeof What
if How eq 0
exitm
endif
push eax
push ecx
push edi

lea edi,What
xor eax,eax
if How ge 4
mov ecx,How/4
How = How - (How/4)*4
rep stosd
endif
if How
mov ecx,How
rep stosb
endif

pop edi
pop ecx
pop eax
endm



Clear aBuffer
Please use a smaller graphic in your signature.

Cyrus


jj2007

Check also the ClearLocals thread at http://www.masm32.com/board/index.php?topic=7984.0 for a more generic solution.

donkey

The string instructions (stosd/w/b) are fairly slow, you might want to try some of the mmx/sse instructions for larger buffers, I haven't tested this for speed but on very large buffers it might be faster. The buffer must be qword aligned...

// zero the mmx register
pxor mm0,mm0
// initialize our counters
mov edx,[address]
mov ecx,[bsize]
// start our loop
tol:
movq [edx],mm0
add edx,8
sub ecx,8
jnz <tol


Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

From masmlib.hlp :

Quotememfill

memfill proc lpmem:DWORD,ln:DWORD,fill:DWORD

Description
memfill is a fast memory filling procedure that works in DWORD unit sizes. It is usually good practice to allocate memory in intervals of four for alignment purposes and this procedure is designed to work with memory allocated this way.

Parameters

1. lpmem  The address of the memory block to fill.

   2. ln  The buffer length to fill.

   3. fill  The fill character(s).

Return Value
There is no return value.

The fill pattern could be set to 0 to clear buffers.

hutch--

Unless a procedure has a very large number of variables AND they all need to be set to zero I would avoid and looped code method of setting them to zero as it is unreasonably slow in comparison to zeroing on a needs basis or specifically setting a list of variables to zero.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

For string buffers, you can just 'clear' the buffer by setting the first byte to zero; unless you need to remove all evidence of the previous contents (for security reasons) there's no need to go beyond this. The next time you use it, it will appear empty.
mov BYTE PTR [aBuffer],0

If your 'buffer' is only a single dword, you can just set it back to zero.
mov DWORD PTR [aBuffer],0

And for anything larger than 4 dwords, you should fill the block with zeroes (as mentioned above.)
No snowflake in an avalanche feels responsible.

Cyrus

Thanks all. The solutions are great. MUCH better results now. Thanks a million!

asmfan

my 2 cents, instead of
Quote from: Tedd on November 19, 2007, 04:38:17 PM
mov BYTE PTR [aBuffer],0
mov DWORD PTR [aBuffer],0
I'd prefer
and BYTE PTR [aBuffer],0
and DWORD PTR [aBuffer],0
Russia is a weird place