flat assembler
Message board for the users of flat assembler.

Index > Windows > C Library Example question?

Author
Thread Post new topic Reply to topic
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 23 Apr 2010, 14:58
Hey!

I've just recently looked at Vid's flibc example, and everything makes a lot of sense. Very Happy

The only thing I'm trying to comprehend now is when you're making function calls, why do you add numbers to the stack pointer after you make the call?

For example...
Code:
        ; write welcome text and prompt text
        ; puts("Hello\nPlease type a number: ")
        push    _hello
        call    puts
        add     esp, 4         
    


Is it really necessary?

Thanks!
[/code]
Post 23 Apr 2010, 14:58
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 23 Apr 2010, 15:01
Upon further inspection of the code, I notice you add four when you only have one parameter, and eight when you have two... Does this mean that you have to change the stack pointer yourself after you push values on the stack, so not to overwrite anything? And I thought that the stack pointer automatically changed when something was pushed on the stack...
Post 23 Apr 2010, 15:01
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 23 Apr 2010, 15:17
Also, I'm playing around with some code I wrote based off of this (a simple "Hello World!" program), and I'm wondering... does it matter which order the sections are in? The reason I ask is because it wasn't working when I had the data sections before the code section (.data and .idata).

Also, I noticed when I tried to jmp to the imports directly, rather than making a call to a wrapper it didn't work... Why is that?

Sorry, I'm just trying to learn. Smile
Post 23 Apr 2010, 15:17
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 23 Apr 2010, 15:34
From the looks of your code it seems to be _cdecl calling convention.
Post 23 Apr 2010, 15:34
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat 23 Apr 2010, 15:49
What's the "_cdecl calling convention"? Sorry... I'm still very new at this.
Post 23 Apr 2010, 15:49
View user's profile Send private message Reply with quote
a115433



Joined: 05 Mar 2010
Posts: 144
a115433 23 Apr 2010, 16:10
cdecl = arguments removed by caller.
Post 23 Apr 2010, 16:10
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 23 Apr 2010, 16:57
Post 23 Apr 2010, 16:57
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 23 Apr 2010, 21:47
The add esp,* is to remove the parameters from the stack. You're pushing a 4 byte pointer onto the stack as an arg and removing it after the function you call returns.
Post 23 Apr 2010, 21:47
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 23 Apr 2010, 23:01
note: add esp,4 is equivalent to pop reg32, but without reg overwriting.

something fun would be to do pop dword[ss:esp], it will do exactlly the same thing, but slower because of the memory transfert from [ss:esp] to [ss:esp]...
like a move eax,eax...
Post 23 Apr 2010, 23:01
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 24 Apr 2010, 03:28
edfed wrote:

something fun would be to do pop dword[ss:esp]

A very novel idea... Although slower, it seems like it would be a lot less error prone, because of how clear it is as to what you're doing and why.
Post 24 Apr 2010, 03:28
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 24 Apr 2010, 09:25
edfed wrote:
something fun would be to do pop dword[ss:esp], it will do exactlly the same thing, but slower because of the memory transfert from [ss:esp] to [ss:esp]...
like a move eax,eax...
You have to be careful with such claims: here is what Intel SDM wrote about pop (underline is mine):
Quote:
If the ESP register is used as a base register for addressing a destination operand in memory, the POP instruction computes the effective address of the operand after it increments the ESP register.
add esp, 4 trashes EFLAGS, though they're not used in cdecl calling convention. lea esp, [esp+4] doesn't.
Post 24 Apr 2010, 09:25
View user's profile Send private message Reply with quote
peter



Joined: 09 May 2006
Posts: 63
peter 26 Apr 2010, 01:44
On calling conventions:

Raymond Chen's blog post (with links to MSDN)
Wikipedia
Agner Fog's manual
Post 26 Apr 2010, 01:44
View user's profile Send private message Visit poster's website 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.