Hello. I have a question about how local variables work. Say I want to create a local string and ten have it destroyed at the end of the macro/proc. Would this be ok...?

SomeMacro MACRO

LOCAL string
LOCAL array

.data
string db "Some text here",0
array dd 100 dup(0)

.code
/* Do stuff */

endm

...Will this work properly? Or will it cause the stack pointer to get messed up? Just curious because I've been letting my program run for 24+ hours and sometime after that, the comp crashes. Heh, it might be me, or it might be something else, I never let my computer run that long, and it always waits to crash until after I am at work, so who knows :grin:
Posted on 2001-11-13 02:08:08 by AlexEiffel
AlexEiffel,

I havent tested it, but I think every instance of your macro will define a variable string (with a masm generated internal name) in the data section, so the stack isnt involved at all. But you possibly will get assembly errors because variable array is defined more than once.

japheth
Posted on 2001-11-13 03:31:55 by japheth
Thanks japheth. How would I go about creating a temporary string or array then? How does this work in a loop?



// C++ loop to keep things short and simple
while(true)
{
SomeMacro
}


Will this generate data each time through the loop? I thought the data was defined at assembly time, so I would think it is only set aside one time.
Posted on 2001-11-13 03:43:49 by AlexEiffel
Your are right, in your example the string will be generated only 1 time, of course.

If you want to generate a temporary string in a proc but not with the LOCAL directive (possibly because you dont know the number of vars you need), you may use a standard heap allocation or allocate the space on the stack dynamically (with sub esp,sizeof var mov reg,esp).

japheth
Posted on 2001-11-13 03:46:49 by japheth
When you say "standard heap allocation", do you mean the api calls, or are you talking about something different. Wouldn't the stack modification that you mentioned be faster? Also, I think I understand what the "sub esp,sizeof var" does, but why the "mov reg,esp"? Is it so you can then push the stack pointer so you can restore it later? Thanks for all of your help.
Posted on 2001-11-13 04:06:08 by AlexEiffel
HGLOBAL GlobalAlloc(


UINT uFlags, // object allocation attributes
DWORD dwBytes // number of bytes to allocate
);


The returned value is the actual memory location for your use. A value of null means no memory could be allocated.
Posted on 2001-11-13 04:13:58 by eet_1024
AlexEiffel,
yes i meant the api calls (HeapAlloc, LocalAlloc, GlobalAlloc) which are all the same.
the "mov reg,esp" is there because esp is very "variable".

Actually I use some selfwritten macros for these purposes (to be exactly: only to create simple and fast LIFO structures). These are:



;*** alloc space on the stack smalloc, sfree (local only)

smalloc macro reg:req,bytes:req
local ?bytes
?bytes = (bytes + 3) and 0fffffffch
sub esp,?bytes
mov reg,esp
push ?bytes+4
endm

sfree macro
add esp,[esp]
endm

sreload macro reg:req,index ;reload address of stack items
ifnb <index>
mov reg,esp
repeat index
add reg,[reg]
endm
add reg,4
else
lea reg,[esp+4]
endif
endm



to use:
smalloc eax,512 ;allocates 512 bytes on stack
to free:
sfree

sfree often isnt really nessessary because all space is freed on next "ret" in masm. macro sreload is only for special purposes. May be biRAKE can code a better version :) .

japheth
Posted on 2001-11-13 04:26:01 by japheth
Sorry to keep bothering you japheth, but I have a few things to ask about the macro you posted.


?bytes = (bytes + 3) and 0fffffffch
sub esp,?bytes
mov reg,esp
push ?bytes+4


ok, here goes.
1. Why do you add 3 to bytes and then and with 0fffffffch?
2. Why do you add 4 to ?bytes?

Hmm...I thought I had more questions than that. Ah well, I've made you type too much tonight anyway so it's best that I keep it to two ;) Thanks once again. Thank you too eet_1024.
Posted on 2001-11-13 04:46:17 by AlexEiffel
AlexEiffel, to answer your questions:

1. Why do you add 3 to bytes and then and with 0fffffffch?
Because I want esp to remain "dword aligned". Because in the other case you will get many unwanted surprises.

2. Why do you add 4 to ?bytes?
Because in these macros for every allocated item there is a "length" prefix added (which is the size of the var + size of dword). So it can be freed simply by doing "add esp,"

japheth
Posted on 2001-11-13 04:53:53 by japheth
Ok, I think I get what you are saying. Thanks for being so patient. :)
Posted on 2001-11-13 05:04:03 by AlexEiffel