News:

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

ZEROLOCALS

Started by Ficko, December 15, 2009, 10:46:47 AM

Previous topic - Next topic

Ficko

I didn't see this macro anywhere - may I missed it  :P -
So there is it.


; -----------------------
;; ZEROLOCALS [n] - n stays for how many GPR was pushed in "uses"
; -----------------------
ZEROLOCALS MACRO N
LOCAL X
IFNB <N>
X = @SubStr(<N>,1)*4
ELSE
X = 12
ENDIF
xchg edx, edi
mov edi, esp      ; transfer esp to EDI
add edi, X        ; add n*4, skips the pushed esi or edi or ebx
mov ecx, ebp
sub ecx, edi
sar ecx, 2
xor eax, eax
rep stosd
xchg edx, edi
ENDM


sample:


.686p
.model flat
include ..\Macros.inc
.code
MySUB proc near stdcall uses esi edi ebx Param01:DWORD
LOCAL Buffer[12] :BYTE
ZEROLOCALS
;Your code
ret 4
MySUB endp
end




Ficko

Ops.. :red

I gues we need a subsection for MACROs for easier look up. ::)

hutch--

this is something you usually see in a basic compiler, the spec of local values being either zero or a null string.

I fail to see what is wrong with doing it manually.

mov local1, 0
mov local2, 0 etc ....
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave


        OPTION  PROLOGUE:NONE
        OPTION  EPILOGUE:NONE

SomeDamnProc proc someParm1:dword,someParm2:dword

local1  TEXTEQU [ebp-4]
local2  TEXTEQU [ebp-8]
local3  TEXTEQU [ebp-12]

        push    esi
        push    edi
        push    ebx
        push    ebp
        xor     eax,eax
        mov     ebp,esp
        push    eax           ;zero local1
        push    eax           ;zero local2
        push    eax           ;zero local3

:8)

Ficko

Quote
I fail to see what is wrong with doing it manually.

Nothing.
But imagine you have a proc with 3-4 (or more) localy declared windows structures.
Lots of times you use only 1-2 members from maybe 10 but the rest have to be zero before calling the api.

Or you have buffers which you have to empty time to time.

Or .... :bg

dedndave

puffers ?   :eek

ohhhhhhhhhhh - buffers ! - lol

TNick

I'm shore he meant tuffers... we all know what tuffers are  :lol  :lol


hutch--

On the rare occasion that you must zero large amouns of local addressing, just use a memfill routine, saves messing around with the stack in almost all instances.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Ghandi

Generally speaking, if im using locals in a routine, i will do as Hutch suggested, inline. Point EDI at the start of your buffer, zero al, set ECX to size of buffer and then rep stosb. Larger buffers i will use rep stosd. I dont have any need to for a rep stosb to catch any trailing bytes, the locals are always on a DWORD alignment.

Its no hassle and if im worried about saving registers, i'll simply incorporate the uses directive. I know i can manually push the registers but its a feature of MASM so i'll use it :D. Most things i write arent that time critical that the zeroing of the buffers will cause a bottleneck and for anything which needs speed, i zero only as i absolutely need to.

HR,
Ghandi

Ficko

Quote
ohhhhhhhhhhh - buffers ! - lol
Quote
I'm shore he meant tuffers...

He.. He.. He. :bdg

I like to see that you guys hungarian is match up with my english. :toothy

Ficko

Quote
Generally speaking, if im using locals in a routine, i will do as Hutch suggested, inline. Point EDI at the start ....

That's exactly the macro is doing above. :P

Ghandi

Yes, but its a macro, where i was saying i have no problems with typing a few extra lines manually. I was merely joining in the thread. As the other thread linked points out, RtlZeroMemory seems to be an efficient API (o0 can this be right? hehe, j/k) but its just a personal preference of mine that i code most things manually. What another person does is no concern of mine and its a case of 'horses for courses', there was no criticism from me.

If i did want to macro it, i'd probably just do something slow and which requires some (very) basic math to provide the correct dword count:


ZeroLocals MACRO dwNumDwords
  push edi
  xor eax,eax
  lea edi,dword ptr [esp+4]
  mov ecx,dwNumDwords
  rep stosd
  pop edi
EndM

Once again, no time critical stuff with this... Its slow (uses rep stosd) but it works, there is also no support for the 'uses' directive. Like i said, i like to manually code things, so macros arent my forte' either.

HR,
Ghandi

Ficko

Quote
no time critical stuff with this... Its slow (uses rep stosd)

That's a myth. :bg

"rep stosd" is just as fast as RtlZeroMemory. :P

hutch--

Funny enough, unoless you are zeroing over 500 bytes, REP STOSD is not a good choice, its known to be slow in this context. If you must zero locals on entry and you don't want to run the zero instructions inline, a DWORD loop will do it with less registers and faster if its under 500 odd bytes.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php