473,626 Members | 3,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why doesn't this function call save these registers?

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

Nov 13 '05 #1
3 2801
You are right that ebx, esi, edi, and ebp do not need to be saved if they
are not used. This is why GCC isn't saving them.
From the looks of things, I think you used VC in debug mode. In debug mode the compiler adds extra data to the stack. For example, the buffer overrun
check and variable initialization checks add data to the stack. When
optimizations are enabled, smaller functions won't use a frame pointer, so
they don't initialize ebp.

-Matt

"Shi Jin" <ji********@hot mail.com> wrote in message
news:pa******** *************** *****@hotmail.c om... 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

Nov 13 '05 #2
>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
....
Nobody said the linkage conventions are the same for MSVC7 and gcc.
Nobody said you can link MSVC7 output and gcc output together and
get something that works (although if gcc uses MSVC7 libraries,
it is likely that you can.
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.
*WHICH* C calling convention? Every compiler is free to have a different
one. It may even be true that code compiled with -O3.14159 cannot
be linked with code compiled with -O3.141592, as the linkage conventions
can be different.
Is that because in the code under linux, those registers are not used. So
it is not necessary to back them up?
Linkage conventions aren't going to require pushing and popping
registers unnecessarily. If the values are the same at exit as
at entry without pushing and popping, why bother doing it?
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?
Try writing some code that might use these and see. ESI and EDI might
be used in code that does array indexing with variables. Maybe.
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?


It may be room for the Microsoft DRM extensions at the register level.
Or perhaps it's room for Bill's wallet. Or maybe it has something to
do with C++ exceptions.

Gordon L. Burditt

Nov 13 '05 #3

"Shi Jin" <ji********@hot mail.com> wrote in message
news:pa******** *************** *****@hotmail.c om...

generally, stdcall or pascal (deprecated on windows) calling conventions are
used in the windows codebase. cases of cdecl (linux & unix) calling
convention is more rare in windows. the differences in these calling
conventions are well documented.

-bryan
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

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4285
by: Michael | last post by:
Guys, I'm interested in how the compiler implements function calls, can anyone correct my understanding/point me towards some good articles. When a function is called, is the stack pointer incremented by the size of the variables declared in the called function, and then the function will know where these are in memory relative to the current stack pointer position. ALso how are variables returned, is that by a set position in the...
9
2349
by: Gernot Frisch | last post by:
class A { A(int mode) {pF = F1;} void F1() {} void (*pF)(); }; How do I do something like this? I need a function that depends on variable 'mode' and will be called
2
2412
by: kelvin | last post by:
Hi, I've this piece of code which does not work at all. Can anyone point out my mistake? I've 2 buttons. History button will call verifyFields() function and lead to different page for processing. verifyFields() is working fine and so does the history() function.
8
13995
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on stack. this contains the arguments this function was called with, address to return to after return from this function (the location in the previous stack frame), location of previous frame on stack (base or start of this frame) and local variables...
10
2176
by: Nitin | last post by:
Ppl , Want to have ur opinions on having function calls like the one stated below: function funcA ( struct A *st_A , struct B *st_B ) { st_A->a = st_B->a
64
3363
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be "refactor-ed" to be
23
7797
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
7
1415
by: Ancient_Hacker | last post by:
In days of old, for no discernible reason, many CPU's had the ability to execute one or more instructions out of registers. Not very common today. Which brings up a semi interesting point. We know the "register" keyword suggests the compiler keep the following variable in a register. We also have the "inline" suggestion in C++ to suggest inlining a
1
3877
by: faultykid | last post by:
I would like to store a variable then call it back later. I have a variable on line 198 www = ''+this._ad.clickUrl+''; and on line 321 i try document.write(www);
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.