Thread: passing arguments to functions

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    passing arguments to functions

    In my book I've read this:
    "Data can only be added in
    double word units. That is, one can not push a single byte on the stack."

    So, I made a little experiment watching how arguments are passed to a function:
    Consider this simple C program:

    Code:
    #include <stdio.h>
    
    void func(int);
    
    int main(void)
    {  
    	func(1);
    }
    
    void func(int a)
    {
    	int b=2;
    }

    Using disassembly in .Net I was abe to see this (among other things):

    Code:
    func(1);
    00411BFE  push        1    
    00411C00  call        @ILT+1405(_func) (411582h) 
    00411C05  add         esp,4
    So it is true! It is obvious that value of argument in this case 1 has been pushed on the stack!

    Then I modified func and with the following code:

    Code:
    #include <stdio.h>
    
    void func(double);
    
    int main(void)
    {  
    	func(1);
    }
    
    void func(double a)
    {
    	int b=2;
    }

    I saw this:

    Code:
    func(1);
    00411BFE  push        3FF00000h 
    00411C03  push        0    
    00411C05  call        @ILT+1405(_func) (411582h) 
    00411C0A  add         esp,8
    Instruction add esp,8 cleaning stack, 8 indicates 8 bytes for data type double. But what is unclear is those first two lines:
    00411BFE push 3FF00000h
    00411C03 push 0

    How can I conclude from this that value of 1 is pushed on the stack?
    I think that little endian notation is used so I try to calculate number taking 0xF03F, but that is number 61503 far away from 1.
    All that assembler code was in main function, and I expected so because default calling convention that is used is --cdecl
    Thanks for help!
    Last edited by Micko; 07-18-2004 at 10:57 AM.

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >"Data can only be added in double word units. That is, one can not push a single byte on the stack."
    What book was this?

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    PC Assembly Language
    Paul A. Carter
    November 11, 2003

    Anyway that sentence is pulled out of context. Of coures it can be passed as single byte.
    Last edited by Micko; 07-18-2004 at 09:38 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > That is, one can not push a single byte on the stack."
    Implementation specific detail. How other compilers for other processors work can be completely different. Many RISC processors pass the first 'n' parameters in registers.

    You only need worry about such detail when you start interfacing C and asm (or C and other languages).

    And how come identical source code generated two different blocks of asm?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Oh, sorry my mistake. Thanks for correcting me Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions Taking Dif. Numbers of Arguments at Dif. Times?
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 04:12 PM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  3. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM