flat assembler
Message board for the users of flat assembler.

Index > Windows > Passing Parameters to a Function, Explaination?

Author
Thread Post new topic Reply to topic
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 07 Sep 2010, 16:37
i have this question about passing parameters
i saw an example that did the following:
Code:
gcdmem:
 push     ebp
 mov     ebp, esp
 mov        eax, [esp+8]
    


the way i understand this code is:
push ebp ; this stores the parameters in the stack to be read one by one directly from the stack
mov ebp, esp ; takes the address of the stack and store it in ebp, i dunno y he did this
mov eax, [esp+8] ; i dunno y he started with 8, and i also dunno why he didnt call ebp directly

the things i mentioned that i dunno in the comments is that i dunno why it isnt done like this
push ebp
mov eax, [ebp+8]

any explaination?
Post 07 Sep 2010, 16:37
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 07 Sep 2010, 16:53
The first two instructions are to set up the EBP-frame, ESP-frame procs don't have that, but then accessing local variables and parameters is more complicated because ESP typically changes during execution (when using PUSH for instance). Also, addressing via [ESP+x] makes the instructions 1 byte longer than [EBP+x].

The code used [esp+8] because after using PUSH EBP, you have [ESP]= old EBP, [ESP+4] = return address (used by RET instruction), and [ESP+8] = first function's argument. However, considering this is an EBP-frame function, I think you may have copied the last instruction wrong and actually it is "mov eax, [ebp+8]", but it is OK anyway because at that point EBP=ESP and perhaps it is even an optimization because the CPU does not have to wait for mov ebp, esp completion.
Post 07 Sep 2010, 16:53
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 07 Sep 2010, 19:47
man, im sorry
but can u go simpler?
just a little bit?
i read it like 10 times
- "frame procs" whats that?
- "[ESP+x] makes the instructions 1 byte longer" how so? for example, makes it like [ESP+9] inted of [ESP+8]?
- "ESP typically changes during execution (when using PUSH for instance)." why will it change after push?

and sorry for copying the last line wrong
Post 07 Sep 2010, 19:47
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 07 Sep 2010, 20:47
ESP holds a value (memory address) called the stack pointer. Parameters are stored on the stack (in memory relative to ESP). To access these values we need to maintain a pointer relative to ESP -- which is difficult when ESP changes (as more space is needed on the stack).

PUSH works by adjusting ESP to make space on the stack, and then storing the value at the address ESP. PUSH {regmem} is analogous to SUB ESP,4 / MOV [ESP],{regmem}.

The use of a frame pointer allows the stack pointer to fluxuate in a dynamic manner without loosing a point of reference. EBP is used in this manner above. First the contents of EBP are preserved on the stack, then ESP is preserved in EBP.

Instruction encoding is one byte shorter with offsets from EBP than they are from ESP -- this is what the processor sees and not in reference to the mnemonic representation. How the processor handles instructions can effect execution performance. Smaller code takes up less space in memory/disk/network.

Can you share with us your experience/goals to assist in communication?
Post 07 Sep 2010, 20:47
View user's profile Send private message Visit poster's website Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 07 Sep 2010, 21:20
thanks alot bitRAKE, i got it now Very Happy
actually i dont have much experience when it comes to low level coding, im kinda good with delphi and win32 platform applications.
and i got no goals, i was just watching some asm vedios and this one poped out and i didnt understand it.

is there a book or paper that explains this small things? or it comes with experience?
Post 07 Sep 2010, 21:20
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 08 Sep 2010, 08:29
Somewhere in the Intel manuals these basic things must be described in detail. Certainly, volume 1, but I don't know what chapter - maybe right at the start? Randy Hyde's AoA (Art of Assembly Language) most certainly covers it as well. Some people just jump in modifying existing sources - which works well once you have an overview of the architecture in mind.

...and ask lots of questions, of course. Wink
Post 08 Sep 2010, 08:29
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 08 Sep 2010, 08:46
bitRAKE wrote:
Certainly, volume 1, but I don't know what chapter - maybe right at the start?
Yes, it is in Volume 1. Chapter 6 - Procedure calls, interrupts and exceptions; section 6.2 - Stacks.
Post 08 Sep 2010, 08:46
View user's profile Send private message Visit poster's website Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 08 Sep 2010, 15:22
thanks alot Very Happy
ill start reading right now
Post 08 Sep 2010, 15:22
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.