Hi,
I have an asm file and a C file that I compile like this:
gcc -m32 -c process.s
gcc -m32 -c mem.c
ld process.o mem.o .........
in process.s I call functions from mem.c.
At home I compile this using GCC4 and when I disassemble mem.o (the one writtin in C), I notice that only the EAX register is being used. That is fine, I rely on that because in process.s I don't want the other registers modified.
But now I compile this using GCC3.4.6 and I noticed that my function uses ECX and EDX without even pushing and poping them and at the end. So the function in process.s cannot assume that the registers it uses will still be correct after calling a function written in C.
Obviously, it is better when it uses several registers instead of memory, but it should push/pop them?
Any one have a solution for this, other than pushing my registers before calling the function? There must be a flag I could set with gcc.
gcc and registers
- jerryleecooper
- Member
- Posts: 233
- Joined: Mon Aug 06, 2007 6:32 pm
- Location: Canada
There are several general purpose registers: EAX, EBX, ECX, EDX, ESI and EDI.
Three of these are also called 'scratch registers': EAX, ECX, EDX. These, according to the __cdecl calling convention, are not required to be maintained across a function call. So the caller may never assume these registers to have the same value on return as when called.
Three of these are also called 'scratch registers': EAX, ECX, EDX. These, according to the __cdecl calling convention, are not required to be maintained across a function call. So the caller may never assume these registers to have the same value on return as when called.
Re: gcc and registers
I don't think so. GCC does not assume any registers are being saved accross function calls, except for the obvious ones (ESP and EBP). All general purpose registers (EAX, EBX, ECX, EDX, ESI, EDI) may be trashed. EAX holds the return value if it's a non-void function. If there is a flag, check the GCC manual pages.dumaisp wrote:Any one have a solution for this, other than pushing my registers before calling the function? There must be a flag I could set with gcc.
EDIT: JamesM already provided a more detailed answer. It seems only EAX, ECX and EDX may be trashed.
JAL
Oh, I found this on another thread:
http://www.asmtrauma.com/Articles.htm
Very good documentation.
I find it odd though, the routine should clean itself up. i.e. save/restore all modified registers and clean the stack after. It's like saying: I'm gonna do the dishes before I eat instead of after i'm done eating.
http://www.asmtrauma.com/Articles.htm
Very good documentation.
I find it odd though, the routine should clean itself up. i.e. save/restore all modified registers and clean the stack after. It's like saying: I'm gonna do the dishes before I eat instead of after i'm done eating.
No it shouldn't. The caller knows which registers are still needed and which ones could be trashed, so I'm quite OK with the caller saving what it still needs before calling a function. This is thinking beyond x86, mind you.dumaisp wrote:I find it odd though, the routine should clean itself up.
Cleaning up the stack done by the callee could become problematic when you think about varargs...
Every good solution is obvious once you've found it.