flat assembler
Message board for the users of flat assembler.

Index > Main > Passing operands.... Through Stack???

Author
Thread Post new topic Reply to topic
BOTOKILLER



Joined: 07 Jan 2011
Posts: 154
Location: Ukraine
BOTOKILLER 17 Aug 2011, 17:44
Hi everyone!
I'm starting to make functions that require more input data than I can place to registers, I read somewhere that operands can be passed through stack, but how do I relocate CS and EIP saved to stack during call???

_________________
_______________________________
NSOS
Post 17 Aug 2011, 17:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20484
Location: In your JS exploiting you and your system
revolution 17 Aug 2011, 18:23
I'm not clear upon what you mean by "relocate CS and EIP saved to stack" but I am going to assume that you want to free the parameters upon return from the call. Generally there are two ways to approach it. 1) caller frees the parameters (C calling standard) and 2) callee frees the parameters (STDCALL calling standard). There are lots of posts already here about how these standards work at the assembly level. Remember that the search function is your friend.

Moving to main, this should not be in the heap
Post 17 Aug 2011, 18:23
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1678
Location: Toronto, Canada
AsmGuru62 18 Aug 2011, 13:24
Put the parameters into a structure and pass the address to it in a register. Same as stack, but no cleanup required.
Post 18 Aug 2011, 13:24
View user's profile Send private message Send e-mail Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 18 Aug 2011, 13:28
In order to do that, he should learn the proper method using stack first.
Post 18 Aug 2011, 13:28
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1678
Location: Toronto, Canada
AsmGuru62 18 Aug 2011, 16:44
Oh!... I see... coding police arrived!
Smile

Ok, I'll be good.

When you call a procedure - parameters are PUSH-ed into stack:
Code:
push 1
push 2
push 3
call function
    

If you compile that code and run it in debugger - you can stop just after the CALL instruction and you are on a first instruction inside the function. Take a look at the stack memory - at the very top it will be your return address. And then you will see values 3,2,1 - these are your parameters. If you push them in a reverse order - then they will show up as 1,2,3 in natural order.

Now, to release the parameters all needs to be done is supply a RET instruction with a number of BYTES which taken by parameters, like so:

function:
...
RET 3*4 ; <-- 4 bytes per each of 3 parameters

When function returns - the stack will be as BEFORE you pushed first parameter. All that is seen in debugger very well.
Post 18 Aug 2011, 16:44
View user's profile Send private message Send e-mail Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 19 Aug 2011, 04:05
you can pass parameters with predifened structures.
only ONE pointer is needed, and this pointer can be passed in another structure.

double cool effet:

never need to code push param1 param2 param3 etc... only MOV INDEX_REG,OBJECT is needed before to call the function, leave he stack as it is in the origin, a way to save the context of the previous code.

architecture independant, the structure can be a file, useable by any CPU.


but some not cool effects:

this practice is a drug, and as is, it is a bad method for the coding police.
nothing like registers can be passed, because need some mov.
need at least one entry point in the structure tree. and is a little slower than stack.


since i "found" this way to do, i only code like this, and i can spend an entire coding session without writing any asm instruction, all is in the structure.
Post 19 Aug 2011, 04:05
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Aug 2011, 09:42
edfed: you can also fill stack with MOVs, you don't have to allocate anything (like you have to with the object), stack memory is already cached
Post 19 Aug 2011, 09:42
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 19 Aug 2011, 13:41
yep, maybe one day, i will use the stack segment to contain predifined objects.

the problem with stack is really the dynamic aspect, stack is always used for push/pop call ret, and then, it will insert various adresses between objects.

that is a problem if you want to manage a big object list as a file.
Post 19 Aug 2011, 13:41
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1678
Location: Toronto, Canada
AsmGuru62 19 Aug 2011, 13:57
edfed: with a structure in some cases it is re-usable: fill it once, put the address into say, ESI, and then call diffreent functions - do not modify ESI, just functions may modify members of structure. Very smart and fast!

And now you got C++ in action! Neat!
Post 19 Aug 2011, 13:57
View user's profile Send private message Send e-mail Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Aug 2011, 15:35
edfed: Also if you mean "object" in proper sense, not simply as a structure holding parameters for particular call, then you have a problem with recursion. Especially with your predefined objects.
Post 19 Aug 2011, 15:35
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 23 Aug 2011, 18:59
vid:
the reccursion problem is only when you code something reccursive, or when you do something with dynamic object linkibng (create pointers in the structures)

in both case, the problem is solvable by a good attention of what is done.

in pure asm, reccursion too is a problem, if you code something like...

this:
call this

you have reccursion. especially if there is no exit condition test before the call.

the only way to don't have reccurison problem is to test the code before to say it works.

if you don't do dynamic pointer creation, the reccursion is close to impossible, because everything is defined by the coder, and the coder will not think reccursion, but sequences.


AsmGuru62:
modifying the datas is simple, and using the same structure for multiples function ids not a problem.
some functions use other subfunctions, and of course, without data change. just and evolution, or nesting.

Code:
object dd 0,1,2,3
mov eax,object
call trick

trick:
call .trick1
call trick1
ret

.trick1:
call trick1
ret

trick1:
ret
    
Post 23 Aug 2011, 18:59
View user's profile Send private message Visit poster's website Reply with quote
FrozenKnight



Joined: 24 Jun 2005
Posts: 128
FrozenKnight 25 Aug 2011, 13:54
This question is the same as asking; Where can i store information other than in registers?

You can use the stack, program memory, any static memory, dynamic allocated memory, files on disk, memory mapped file, SMC (Self Modifying Code, my favoret when speed isn't an issue) etc... So, just about anywhere. This isn't C/C++/Java/etc... where your options are limited. This is assembly you can do what ever you want. However, the commonly accepted method is to use the stack. For other ideas check out the Wikipedia article http://en.wikipedia.org/wiki/Calling_convention, be sure to go into the section for x86 it's more detailed for this architecture.
Post 25 Aug 2011, 13:54
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.