The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: iaNac on August 17, 2006, 10:49:09 PM

Title: What do API functions have to do with esp register?
Post by: iaNac on August 17, 2006, 10:49:09 PM
I'm learning masm32 and used Quick Editor to generate a template that displays a small window.  I've been studying the code and there's a GotoXY procedure that centers the new window on the screen based on the screen ht and wid and the new window ht and wid.  There are mov opcodes that involve esp, such as mov eax, [esp+8].  Question: I thought data was passed to/from API calls using variables.  Is it routine to use esp or other registers?  If so, where do I get the info I need to do this?

Thanks in advance

ps. I can give more code details if necessary but I'm really just looking for a concept answer.
Title: Re: What do API functions have to do with esp register?
Post by: hutch-- on August 17, 2006, 11:37:51 PM
iaNac,

Welcome on board. If its the one I am thinking of its a procedure with the stack frame removed which involves the memory addresses being handled directly in ESP rather than with EBP in a normal stack frame procedure.

ESP is used as the pointer to the current location of stack memory which is used to pass argumnets to procedures in the normal manner. When you use code like,


mov eax, [esp+4]
mov ecx, [esp+8]


You are dereferencing the address stored in ESP and writing the VALUE contained at that address to the two respective registers.
Title: Re: What do API functions have to do with esp register?
Post by: Hjortur on August 18, 2006, 01:58:39 AM
Hi iaNac, and welcome aboard!

Parameters are not passed to API functions with variables but rather with the stack.  So if you are calling a function with three parameters
they will be pushed on the stack in reverse order.
e.g.
invoke Somefunc, param1,param2,param3
is equal to:
push param3
push param2
push param1
call Somefunc


Then these parameters are accesed by esp + offset.  If all parameters are DWORDs the first parameter is at memory address [esp],
param2 is at [esp+4] and param3 at [esp+8]. 
Just remember that the stack grows downward in memory, took a while for me to get it.

Hope that cleared it up!


....And I forgot values from APIs are usually (if not always?) returned in the eax register
Title: Re: What do API functions have to do with esp register?
Post by: sinsi on August 18, 2006, 10:58:49 AM
Quotethe first parameter is at memory address [esp]
The parameter at [esp] is the return address from the CALL, so param1 is at [esp+4]
Title: Re: What do API functions have to do with esp register?
Post by: Hjortur on August 18, 2006, 04:50:15 PM
Quote from: sinsi on August 18, 2006, 10:58:49 AM
Quotethe first parameter is at memory address [esp]
The parameter at [esp] is the return address from the CALL, so param1 is at [esp+4]

Very sorry I just forgot about that! 
Thanks for correcting me