Click to See Complete Forum and Search --> : Assembler Locals...


JamesSchumacher
May 2nd, 2008, 04:25 PM
I am using inline assembler in C++ (VC++ cl.exe) and I think I am using this code correctly.

Is this 'one way' to access locals in assembler? I only wrote this out this way to get a better understanding of how it works. This does not crash when I call it, so I am believing it's correct.


__declspec(naked) void __stdcall TestLocals()
{
#define _LocalArray -32

_asm
{
push edi ; store the data index
sub esp,_LocalArray ; grow the stack pointer
xor edi,edi ; zero the data index
InternalLoop:
mov eax,4 ; size of an element
mul edi ; multiply by data index
mov ecx,esp ; stack pointer into ecx
add ecx,eax ; add the offset

mov DWORD PTR[ecx],100 ; move into the value
inc edi ; increment the data index

cmp edi,8 ; check to see if we need to continue
jb InternalLoop ; if so, unsigned check - continue the loop

add esp,_LocalArray
pop edi
xor eax,eax
ret 0
}
#undef _LocalArray
}

S_M_A
May 2nd, 2008, 06:06 PM
I might be way out here but sub esp,_LocalArray means that you add 32 to esp (esp = esp - -32). The stack usually grows downwards so adding does not seem like the proper way of reserving more stack space.

JamesSchumacher
May 3rd, 2008, 12:48 PM
Okay, here is my question....

You push variables from right to left, so... that means means that when an argument is access left to right with 8,12,16 modifier to ebp... Doesn't that mean any locals will be negative?

I believe I am doing the locals wrong (even after correcting the point you stated), that is why I am trying to get confirmation from someone.

It doesn't crash, however... I believe the way I am doing it has a possibility of trampling. Is it however? (Not code is this function, other ones)

I am pretty sure I should change my code comparisons versus ESP, and keep in mind any pushes/pops, CORRECT?

Oh wait... Isn't that obvious? Well... I guess not so obvious to everyone else or they knew there was a problem.