Question about disasembled function call on linux and

Discussion in 'C Programming' started by Shi Jin, Sep 10, 2003.

  1. Shi Jin

    Shi Jin Guest

    Hi there,

    I am now having a puzzle regarding what a function call actually does when
    called.
    I have a simple code to see what's going on. It is simply calling another
    simple function from main().
    The disassembled code for the function under linux (with gcc) looks like this:

    pushl %ebp ;push old %ebp
    movl %esp, %ebp ;make new stack frame
    subl $4, %esp ;make room for local variable---x
    ;then the code doing the calculation inside the function

    But the corresponding instructions under WinXP(with MSVC7, same code ) is
    push ebp
    mov ebp,esp
    sub esp,0CCh ; 0CCh=204
    push ebx
    push esi
    push edi
    .....

    The problem is why doesn't linux push the registers ebx,esi,edi into the
    stack to save them. Since the C calling convetion assumes that function
    call do not modify ESI,EDI and EBX.

    Is that because in the code under linux, those registers are not used. So
    it is not necessary to back them up? Maybe in some other cases, they are
    actually used, then there would be some push instructions, just like the
    windows disassembled code. Is that true?

    Another puzzle for me is that in the function, there is only one integer
    as a local variable. So "subl $4, %esp" is enough for that. But I don't
    know why the windows code uses "sub esp,0CCh", which sounds like 51 32-bit
    variables are there. Or the number 51 is just some randomly large number?

    Thanks for any comment or advice.

    Shi
     
    Shi Jin, Sep 10, 2003
    #1
    1. Advertising

  2. Shi Jin

    Shi Jin Guest

    Re: Question about disasembled function call on linux and windows

    In order to make it clearer, I put the code here. Those who have interst
    may actually try it.
    //begin
    #include<stdio.h>
    #include<stdlib.h>

    int lincomp(int a,int b,int c, int d)
    {
    int x;
    x=a*b+c*d;
    return x;
    }

    int main()
    {
    int y;
    y=lincomp(0,1,10,16);
    printf("y=%d\n",y);
    exit(0);
    }

    //end
    On Tue, 09 Sep 2003 19:09:31 -0400, Shi Jin wrote:

    > Hi there,
    >
    > I am now having a puzzle regarding what a function call actually does when
    > called.
    > I have a simple code to see what's going on. It is simply calling another
    > simple function from main().
    > The disassembled code for the function under linux (with gcc) looks like this:
    >
    > pushl %ebp ;push old %ebp
    > movl %esp, %ebp ;make new stack frame
    > subl $4, %esp ;make room for local variable---x
    > ;then the code doing the calculation inside the function
    >
    > But the corresponding instructions under WinXP(with MSVC7, same code ) is
    > push ebp
    > mov ebp,esp
    > sub esp,0CCh ; 0CCh=204
    > push ebx
    > push esi
    > push edi
    > ....
    >
    > The problem is why doesn't linux push the registers ebx,esi,edi into the
    > stack to save them. Since the C calling convetion assumes that function
    > call do not modify ESI,EDI and EBX.
    >
    > Is that because in the code under linux, those registers are not used. So
    > it is not necessary to back them up? Maybe in some other cases, they are
    > actually used, then there would be some push instructions, just like the
    > windows disassembled code. Is that true?
    >
    > Another puzzle for me is that in the function, there is only one integer
    > as a local variable. So "subl $4, %esp" is enough for that. But I don't
    > know why the windows code uses "sub esp,0CCh", which sounds like 51 32-bit
    > variables are there. Or the number 51 is just some randomly large number?
    >
    > Thanks for any comment or advice.
    >
    > Shi
     
    Shi Jin, Sep 10, 2003
    #2
    1. Advertising

  3. Off Topic: Question about disasembled function call on linux and

    Shi Jin wrote:

    >
    >
    > I am now having a puzzle regarding what a function call actually does when
    > called.
    > I have a simple code to see what's going on. It is simply calling another
    > simple function from main().
    > The disassembled code for the function under linux (with gcc) looks like this:
    >
    > pushl %ebp ;push old %ebp
    > movl %esp, %ebp ;make new stack frame
    > subl $4, %esp ;make room for local variable---x
    > ;then the code doing the calculation inside the function
    >
    > But the corresponding instructions under WinXP(with MSVC7, same code ) is
    > push ebp
    > mov ebp,esp
    > sub esp,0CCh ; 0CCh=204
    > push ebx
    > push esi
    > push edi
    > ....
    >
    > The problem is why doesn't linux push the registers ebx,esi,edi into the
    > stack to save them. Since the C calling convention assumes that function
    > call do not modify ESI,EDI and EBX.
    >
    > Is that because in the code under linux, those registers are not used. So
    > it is not necessary to back them up? Maybe in some other cases, they are
    > actually used, then there would be some push instructions, just like the
    > windows disassembled code. Is that true?
    >
    > Another puzzle for me is that in the function, there is only one integer
    > as a local variable. So "subl $4, %esp" is enough for that. But I don't
    > know why the windows code uses "sub esp,0CCh", which sounds like 51 32-bit
    > variables are there. Or the number 51 is just some randomly large number?
    >
    > Thanks for any comment or advice.
    >
    >


    Sorry, this is off-topic in the comp.lang.c newsgroup.
    Try the gnu.gcc.help newsgroup instead.
     
    E. Robert Tisdale, Sep 10, 2003
    #3
  4. Re: Question about disasembled function call on linux and windows

    Shi Jin <> scribbled the following:
    > In order to make it clearer, I put the code here. Those who have interst
    > may actually try it.
    > //begin
    > #include<stdio.h>
    > #include<stdlib.h>


    > int lincomp(int a,int b,int c, int d)
    > {
    > int x;
    > x=a*b+c*d;
    > return x;
    > }


    > int main()
    > {
    > int y;
    > y=lincomp(0,1,10,16);
    > printf("y=%d\n",y);
    > exit(0);
    > }


    > //end
    > On Tue, 09 Sep 2003 19:09:31 -0400, Shi Jin wrote:


    >> Hi there,
    >>
    >> I am now having a puzzle regarding what a function call actually does when
    >> called.
    >> I have a simple code to see what's going on. It is simply calling another
    >> simple function from main().
    >> The disassembled code for the function under linux (with gcc) looks like this:


    (snip assembler stuff)

    This is not a C question at all. This concerns a particular
    implementation, and the ISO C standard does not mandate any particular
    implementation to be used.
    Please ask on comp.unix.programmer, comp.gcc.help, or a Linux
    newsgroup. Thanks.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "All that flower power is no match for my glower power!"
    - Montgomery Burns
     
    Joona I Palaste, Sep 10, 2003
    #4
  5. Shi Jin

    Sandeep Guest

    Sandeep, Sep 10, 2003
    #5
  6. Re: Question about disasembled function call on linux and windows

    "Joona I Palaste" <> wrote in message
    news:bjmeri$2vn$...

    (snip)

    > This is not a C question at all. This concerns a particular
    > implementation, and the ISO C standard does not mandate any particular
    > implementation to be used.


    (snip)

    It may or may not be off topic. A question as to whether the generated code
    of a particular compiler is legal within ISO C should be on topic. That may
    be questionable in this case.

    -- glen
     
    Glen Herrmannsfeldt, Sep 10, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. grbgooglefan
    Replies:
    2
    Views:
    439
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    459
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    411
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    268
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    206
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page