The MASM Forum Archive 2004 to 2012
Welcome, Guest. Please login or register.
October 23, 2021, 06:07:41 PM

Login with username, password and session length
Search:     Advanced search
128553 Posts in 15254 Topics by 684 Members
Latest Member: mottt
* Home Help Search Login Register
+  The MASM Forum Archive 2004 to 2012
|-+  General Forums
| |-+  The Laboratory (Moderator: Mark_Larson)
| | |-+  ZEROLOCALS
« previous next »
Pages: [1] 2 Print
Author Topic: ZEROLOCALS  (Read 12428 times)
Ficko
Member
*****
Posts: 305


ZEROLOCALS
« on: December 15, 2009, 10:46:47 AM »

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

Code:
; -----------------------
;; 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:

Code:
.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

« Last Edit: December 15, 2009, 01:26:58 PM by Ficko » Logged
jj2007
Member
*****
Gender: Male
Posts: 6011



Re: ZEROLOCALS
« Reply #1 on: December 15, 2009, 11:33:45 AM »

Logged

Ficko
Member
*****
Posts: 305


Re: ZEROLOCALS
« Reply #2 on: December 15, 2009, 12:37:48 PM »

Ops.. redface

I gues we need a subsection for MACROs for easier look up. Roll Eyes
Logged
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: ZEROLOCALS
« Reply #3 on: December 15, 2009, 01:06:45 PM »

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 ....
Logged

Regards,



Download site for MASM32
http://www.masm32.com
dedndave
Member
*****
Posts: 12523


Re: ZEROLOCALS
« Reply #4 on: December 15, 2009, 01:14:09 PM »


        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

 Cool
Logged
Ficko
Member
*****
Posts: 305


Re: ZEROLOCALS
« Reply #5 on: December 15, 2009, 01:21:16 PM »

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 .... BigGrin
Logged
dedndave
Member
*****
Posts: 12523


Re: ZEROLOCALS
« Reply #6 on: December 15, 2009, 01:23:56 PM »

puffers ?   Eek

ohhhhhhhhhhh - buffers ! - lol
Logged
TNick
Member
*****
Gender: Male
Posts: 543


School level in ASM: second grade


Re: ZEROLOCALS
« Reply #7 on: December 15, 2009, 01:28:48 PM »

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

Logged
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: ZEROLOCALS
« Reply #8 on: December 15, 2009, 01:31:54 PM »

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.
Logged

Regards,



Download site for MASM32
http://www.masm32.com
Ghandi
Member
*****
Posts: 169


Re: ZEROLOCALS
« Reply #9 on: December 15, 2009, 01:38:39 PM »

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
Logged
Ficko
Member
*****
Posts: 305


Re: ZEROLOCALS
« Reply #10 on: December 15, 2009, 01:39:58 PM »

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

He.. He.. He. BadGrin

I like to see that you guys hungarian is match up with my english. tgrin
Logged
Ficko
Member
*****
Posts: 305


Re: ZEROLOCALS
« Reply #11 on: December 15, 2009, 01:44:36 PM »

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. Tongue
Logged
Ghandi
Member
*****
Posts: 169


Re: ZEROLOCALS
« Reply #12 on: December 15, 2009, 01:51:56 PM »

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:

Code:
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
Logged
Ficko
Member
*****
Posts: 305


Re: ZEROLOCALS
« Reply #13 on: December 15, 2009, 02:06:25 PM »

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

That's a myth. BigGrin

"rep stosd" is just as fast as RtlZeroMemory. Tongue
Logged
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: ZEROLOCALS
« Reply #14 on: December 15, 2009, 02:11:46 PM »

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.
Logged

Regards,



Download site for MASM32
http://www.masm32.com
Pages: [1] 2 Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP The MASM Forum Archive 2004 to 2012 | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!