News:

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

Creating stack references dynamicly

Started by DoomyD, August 25, 2008, 05:00:03 PM

Previous topic - Next topic

DoomyD

Out of curiosity: I wondered if there is a way to create a symbolic stack reference without using the LOCAL directive - I usually do so manualy when working with structures: I use the [reg + <struct.item>] \ lea reg,[reg + <struct>] , but I believe a macro could be more efficiant here. For an elaboration: I want to do so from the middle of a procedure, as I find it easier to work with.
Any ideas?

Mark Jones

May I ask, how would this be (different/better/more useful) than just using LOCAL?
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

DoomyD

For an example, VirtualProtectEx requires a pointer to a variable to set with the old access protection, which I don't use. Creating a local for that task is simply not not that useful in my opinion. The following is my choise for this API:push 0
invoke VirtualProtectEx, hProcess, lpAdress, dwSize, flNewProtect, esp ;esp < esp = the new variable
add esp, 4
.
.
.
Another example would be Process32First\Next:invoke CreateToolHelp32Snapshot, <dwFlags>, <th32ProcessID>
.if (eax)
  push eax ;<<<<
  invoke Process32First, [esp+4], <LPPROCESSENTRY32> ;<[esp+4] = snapshot handle
  .repeat
    ...
    invoke Process32Next, [esp+4], <LPPROCESSENTRY32>
  .until (!eax)
  call CloseHandle
.endif
As you can see, its not hard to work with a single dword, but what I want to do, is to create the PROCESSENTRY32 dynamicly(sub\add esp, sizeof PROCESSENTRY32 ), as my procedure might be a bit large and I want to keep it close to the code that uses it. In addition, when the Process32First\Next section of the code is conditional, a local directive could result as useless.

jj2007

Quote from: DoomyD on August 26, 2008, 12:09:18 PM
as my procedure might be a bit large and I want to keep it close to the code that uses it

If you use the stack, it doesn't make a difference if the LOCAL is declared on top or close to your code. Unless, of course, you decide to make the code section writeable and to put the variable really close:

include \masm32\include\masm32rt.inc

.data?
f2sConW dw 0

.code
start:
; int 3 ; set a breakpoint in OllyDbg if you want to see what ml does
jmp @F
MyStruc:
dd 100, 200, 300, 400
@@:
mov edx, offset MyStruc

mov eax, [edx]
pushad
invoke MessageBox, 0, str$(eax), chr$("Test 1"), MB_OK
popad

mov eax, [edx+4]
pushad
invoke MessageBox, 0, str$(eax), chr$("Test 2"), MB_OK
popad

exit
end start