Saving registers in C functions

Y

Yan

Hi.

I found weird gcc/vc behaviour when generating functions code.
According to the docs I read, C functions don't save any registers
except ebp (on x86), but I see that both gcc and cl (VC++) seem
to save ebx, esi and edi while inside functions - even with
optimizations. I wonder if this is one of the "language extensions"
that these compilers offer, or is it some part of standard C behaviour
I overlooked in docs?
The way I know there should be stack frames (push ebp, mov ebp, esp ...)
in functions, but both gcc and cl don't create those, so is this register
saving thing part of that "non-standard" behaviour?

Yan
 
J

jacob navia

Yan said:
Hi.

I found weird gcc/vc behaviour when generating functions code.
According to the docs I read, C functions don't save any registers
except ebp (on x86), but I see that both gcc and cl (VC++) seem
to save ebx, esi and edi while inside functions - even with
optimizations. I wonder if this is one of the "language extensions"
that these compilers offer, or is it some part of standard C behaviour
I overlooked in docs?

You are at the assembly language level here

Assembly conventions rule here. Gcc saves ebx,esi,edi as all
other compilers in the x86 world. Think a bit about it. All the
software under linux/windows assumes that values stored
in ebx/esi/edi are preserved across calls. To make different programs
interoperate, it is essential that the same conventions are respected
by all compilers.
The way I know there should be stack frames (push ebp, mov ebp, esp ...)
in functions, but both gcc and cl don't create those, so is this register
saving thing part of that "non-standard" behaviour?

gcc and many other compilers will create a stack frame when
optimizations are not enabled. Sometimes, the compiler
discovers that a stack frame is not really needed, and
eliminates it. This is a normal optimization that makes
the code faster but less debuggable.
 
M

Malcolm

Yan said:
I found weird gcc/vc behaviour when generating functions code.
According to the docs I read, C functions don't save any registers
except ebp (on x86), but I see that both gcc and cl (VC++) seem
to save ebx, esi and edi while inside functions - even with
optimizations.
ANSI C itself doesn't define how functions are to be written. On most
platforms there is a register usage convention for sharing information
between register, and compilers should adhere to that.
I wonder if this is one of the "language extensions"
that these compilers offer, or is it some part of standard C
behaviour I overlooked in docs?
It's nothing to do with the C language. You need to ask on a Wintel group
why the compiler seems to be saving registers unnecessarily.
The way I know there should be stack frames (push ebp, mov ebp, > esp ...)
in functions, but both gcc and cl don't create those, so is
this register saving thing part of that "non-standard" behaviour?
It could well be that parameters are being passed in registers instead of on
the stack. This sort of detail generally doesn't matter, unless you want to
call a C function from assembly.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top