PDA

View Full Version : Push local variable address


Iwarez
August 20th, 2010, 15:11
Ok, I'm in the process of converting high level code to asm to speed up some operations on binary trees. Now I've come to some situation where the asm code I wrote is so stupid looking that I thought that there MUST be a way to do it better

What I have is this routine entry code:
Code:

Delete1ItemByPosition:

%define snNodeData ebp + 8
%define lStartIndex ebp + 12
%define Item ebp + 16

%define RebuildTreeFromIndex ebp - 4
%define CurrentParentIndex ebp - 8
%define bDone ebp - 12
%define ArrayPointer ebp - 16
%define LastIndex ebp - 20

push ebp
mov ebp, esp
sub esp, 20


Now I want to push a local variable memory location for another function that wants some of it's variables as reference instead of value.

So for example the local variable 'LastIndex' I did this:
Code:

mov ebx, ebp
sub ebx, 20
push ebx


The problem with that code is that
a) It doesn't clearly reference my local variable 'LastIndex'
b) It's 3 instructions

This code looks so stupid that I came here to ask for a better solution. I only learned asm from reversing so I hope I'm missing a very obvious instruction here...

I already looked through some dll's to find a similiar situation but I couldn't find one.

HELP!

Thanks in advance, I-Warez

Neitsa
August 20th, 2010, 16:32
Hi,

(As it seems that you're using nasm) This assembler has a way to define local variables through the %local directive:

http://www.nasm.us/doc/nasmdoc4.html#section-4.8.3

Sorry for not providing a better example than the manual as I'm not really a nasm expert.

Hope it helps.

Iwarez
August 21st, 2010, 05:01
Hi Neitsa,

Thanks for your link, but I'm afraid it won't answer my question. I did however show me a nice way to let nasm calculate the amount of stack space I need for local variables. Thanks for that!

Let me rephrase my question:
If you have a localvariable defined as ebp-8 you can access it from within your routine as mov eax, [LocalVar] and if you need to push the value of the variable for another routine you simple push the variable like push [LocalVar].

What if you want to push the memorylocation? push ebp-8 doesn't exists...

EDIT:
I just realized I could use this
Code:
lea ebx, [LocalVar]
push ebx


SOLVED!

Neitsa
August 21st, 2010, 05:51
I'm sorry I did not understand your question , I could have answered it earlier.

Yep, 'LEA' each time you want an address rather than a value. Glad you found it!

Iwarez
August 21st, 2010, 06:16
Well you helped me a lot with the %local compiler directive. That simplified my routine declaration a lot. Also it helped to get rid of the arguments defines by using the %arg directive. So I learned some more and it looks nicer too. Thanks!