Re: Asm called by c_func
- Posted by Robert Craig <rds at RapidEuphoria.com> Apr 24, 2007
- 516 views
Larry Miller wrote: > I know that an assembly routine called by call() must preserve all registers. > What registers, if any, must be preserved if the routine is called by c_func? > Am I correct in assuming that the return value will be in the EAX register? > From examples I have determined the calling conventions. I think (but I'm not 100% sure) that with either the cdecl or stdcall conventions, EAX, ECX and EDX need not be saved/restored by the called subroutine. I think EAX is often used to hold the return value, if any. Here are some rough notes that I made on calling conventions several years ago. Unfortunately, I don't think Watcom follows the cdecl conventions exactly the same as the other C compilers do. In general, I don't think it's wise to assume too much detail about these conventions, if you don't really have to. Calling Convention Info ======================= Common to BOTH __cdecl AND __stdcall: 1. All symbols are preceded by an underscore character. 2. Arguments are pushed on the stack from right to left. That is, the last argument is pushed first. 3. Registers EAX, ECX and EDX are not saved and restored when a call is made. __cdecl: - The calling routine will remove the arguments from the stack. - Floating-point values are returned in the same way as structures. When a structure is returned, the called routine allocates space for the return value and returns a pointer to the return value in register EAX. __stdcall: - All C symbols (extern "C" symbols in C++) are suffixed by "@nnn" where "nnn" is the sum of the argument sizes (each size is rounded up to a multiple of 4 bytes so that char and short are size 4). When the argument list contains "...", the "@nnn" suffix is omitted. - The called routine will remove the arguments from the stack. - When a structure is returned, the caller allocates space on the stack. The address of the allocated space will be pushed on the stack immediately before the call instruction. Upon returning from the call, register EAX will contain address of the space allocated for the return value. Floating-point values are returned in 80x87 register ST(0). Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com