Suppose I stored some values or data in a buffer (say myBuffer) Now myBuffer contains say "Win32ASM Is Great." How do I clean "myBuffer" so that I can store "Win32ASM Is Cool?" e-nigma
The only way to blank it is to manually go from position 0 in the buffer to position x, placing 0 in every byte.... But if you are only using the buffer for text, remember text is null terminated, and so you don't need to blank data to overwrite it, just add a zero to the end of it! Once a null terminator is reached, the rest of the buffer will be ignored, so if: myBuffer db "12345678",0,"blah blah blah",0 is essentially read as "12345678", and myBuffer + 9 is equal to "blah blah blah blah"! This means it is easy to cut strings short too! e.g. myBuffer db "Win32ASM is great and cool!",0 mov eax, ADDR myBuffer add eax, 9 mov , 0 ;<- This is a 32bit zero ;now myBuffer = "Win32ASM",0,0,0,0,"great and cool!",0 ;4 Zeros cos it is a 32bit zero we move to the memory Mirno
Mirno is exactly correct. However, there's no need to move a 32 bit zero. A single byte will do: myBuffer db "Win32ASM is great and cool!",0 mov eax, OFFSET myBuffer add eax, 8 mov BTYE PTR , 0 ;<- This is an 8 bit zero ;now myBuffer = "Win32ASM",0," is great and cool!",0 ;1 Zero 'cos it is a 8 bit zero we move to the memory
i think there is an api to do that, right? RtlZeroMemory
if you just put a zero at the first byte, your buffer will appear to be clear for most functions. (s)
You guys are correct. But I don't see why we have to clear the buffer first. If you want to replace one text with another, simply call lstrcpy.
It really depends what is needed, in some instances, a full buffer needs to be set to a value, there is a fast function in the current MASM32.LIB called "memfill", about 190 meg/sec on my PIII. ( next version is faster :) ) If you need the buffer recognised as an empty string buffer, Ernie's suggestion is the easiest and fastest way to do it, just write an ascii zero at byte one. If as in the example, you want to place a new string in the buffer, Icz is right, just write the new string to it as the string is zero terminated and it will read correctly. If you need to do this on a recursive basis, forget the API, it is too slow. Use a memory copy function written in assembler, on a short string < 64 bytes, use BYTE copy, on longer strings, use DWORD copy and do the remainder as bytes. Regards, email@example.com
i would juts like to say, one of the great things about assembly langauge is that you can do a lot of things simply and easily without the surport of higher level functions: ie:
will fill ecx bytes of memory with al, without the need for a function call. It will fill 64Mb of mem (67,108,864 bytes), in appox 402,653,188 clocks (or 0.8 seconds on a 500Mtz pc). This doesn't include pentiums dual pipes or p3 tlb caches, branch predictions or out of order instruction execution etc. I think that everybody here, should expect to never see a call to some function like RtlZeroMem, cause really, unless you are gonna be zeroing extremly large amount of mem, in which disk caching will sloow everything down anyway, size should be your primery optimization.
mov edi,Offset of buffer to fill mov ecx,bytes to fill add ecx,edi mov al,byte to fill, ie NULL @@: mov byte ptr ,al or immed8 inc edi cmp ecx,edi jne @B
I don't see why you'd want to use an API. It is way overbloated. And if you want to put in zeros, rather that a loop with mov, I'd use rep stosb.