Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-09-2005, 08:41 AM   #1 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
[Win32] Stack basics?

I'd like to know whether each thread in an application gets its own stack space that isn't modified by other applications. I'd also like to confirm that parameters to functions are sent to the functions by being pushed onto the stack in reverse order and popped off in the correct order. To illustrate my point, are these two statements essentially the same?
Code:
ExecuteMyFunction(param1,param2,param3);

and

__asm
   {
   push param3
   push param2
   push param1
   call ExecuteMyFunction
   }
The reason is that I'm trying to make a printf-style function that outputs in a different way. It's defined (very simplified) as follows:
Code:
//ignore amtldlm :D
void Message(void *amtldlm,char *stri,...)
{
printf("Message: ");
printf(stri); //other parameters still sitting on stack
printf("\n");
}
The idea is that the user passes a printf-format string (stri) and any additional parameters. The additional params are never retrieved so should be one the stack, right?
Looking at stdarg.h, it looks instead like each parameter is just copied side-by-side into memory; in which case is there some way (easier than parsing the string and using va_arg) that I can transfer the extra parameters from one function to the other?

Rgds.
TheSheep is offline   Reply With Quote
Old 09-09-2005, 09:03 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Something like this ?
Code:
void Message(void *amtldlm, const char* fmt, ...)
{
  va_list ap; 
  fprintf(stdout, "Message: ");
  va_start(ap, fmt);
  vfprintf(stdout, fmt, ap);
  va_end(ap);
  fprintf(stdout, "\n");
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-09-2005, 09:52 AM   #3 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
Fantastic, thanks!!
TheSheep is offline   Reply With Quote
Old 09-10-2005, 05:27 AM   #4 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
As for your original question...
For that particular example, essentially yes, but it depends on the calling convention the function uses.

In the default C/C++ calling convention, cdecl, arguments are pushed onto the stack right to left by the calling function, and the calling function is also the one that cleans up the stack.

Win32 API functions use a different calling convention, stdcall. Arguments are pushed right to left as with cdecl, but the called function is responsable for cleaning up the stack (ie, stdcall functions end with stuff like ret 4, ret 8, etc).

Since for variable argument functions only the calling functions knows the number of arguments, cdecl has to be used for those.

Win16 used the pascal/fortran calling convention where arguments are pushed left to right onto the stack. Called function cleans the stack.

There's also fastcall, used by MS for some stuff, which passes the first two arguments that fit into a register through ecx & edx and passes the rest rigth to left through the stack. Called functions cleans up the stack.

Thiscall is the default for C++ member function, with "this" being passed through ecx, the rest right to left through the stack, called function cleans the stack.

GCC has the "regparm(num)" attribute you can use on a function to specify it should pass up to num arguments through registers. I think the maximum is 3.

You don't really need to know all that for solving your problem, as redhead pretty much did , but it's nice stuff to know anyway.
Locutus is offline   Reply With Quote
Old 09-10-2005, 07:59 AM   #5 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
Quote:
You don't really need to know all that for solving your problem, as redhead pretty much did , but it's nice stuff to know anyway
It certainly is good to know; I might actually end up using this info though for another project (a security patch for a game, which hooks an existing DLL function). Thanks.
TheSheep is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL Basics tutorial nathacof Standard C, C++ 3 06-30-2005 11:52 PM
stack unwinding deepak_kr_mehta Standard C, C++ 2 05-07-2005 06:04 AM
Mastered the basics, now what? DavH27 PHP 23 08-24-2004 06:08 PM


All times are GMT -8. The time now is 12:11 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting