Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about disasembled function call on linux and

Reply
Thread Tools

Question about disasembled function call on linux and

 
 
Shi Jin
Guest
Posts: n/a
 
      09-09-2003
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
 
Reply With Quote
 
 
 
 
Shi Jin
Guest
Posts: n/a
 
      09-09-2003
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


 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-09-2003
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.



 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-10-2003
Shi Jin <(E-Mail Removed)> 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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
Sandeep
Guest
Posts: n/a
 
      09-10-2003
You may also checkout the following articles on C to assembly generation:

http://www.eventhelix.com/RealtimeMa...ranslation.htm

http://www.eventhelix.com/RealtimeMa...anslation2.htm

http://www.eventhelix.com/RealtimeMa...anslation3.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Cases in PDF
 
Reply With Quote
 
Glen Herrmannsfeldt
Guest
Posts: n/a
 
      09-10-2003

"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:bjmeri$2vn$(E-Mail Removed)...

(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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 0 01-30-2008 04:19 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments