Question about disasembled function call on linux and

S

Shi Jin

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
 
S

Shi Jin

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
 
E

E. Robert Tisdale

Shi said:
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.
 
J

Joona I Palaste

Shi Jin said:
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);
}

(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 ([email protected]) ---------------------------\
| 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
 
G

Glen Herrmannsfeldt

(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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top