cdecl and floating points
Posted: Mon Mar 22, 2010 3:58 pm
The 32-bit cdecl calling convention says that the primitive data types, except for floating point values, are returned in EAX or EDX:EAX, depending on its size. EBX, ESI, EDI and EBP are preserved. EAX, ECX and EDX can be destroyed and aren't preserved. ESP and EBP are used for stack access, specially EBP which will hold the original base address of parameters in the stack, and ESP can be modified to create a stack frame, as an example here:
http://en.wikibooks.org/wiki/X86_Disass ... ack_Frames
Everything seems to be well until there. It seems that a cdecl function could be coded like this:
Now, what seems strange are the floating point values and how to handle them, which are said to be returned as a single return value in FP0 register of the FPU. First of all there doesn't seem to be a convention to save anything from the FPU state. Is this supposition correct?
The other problem is, how can a program using floating point values run in a machine without FPU, like a 386DX. How can a "float" type routine return value and "float" arguments in stack be handled at compile time?
http://en.wikibooks.org/wiki/X86_Disass ... ack_Frames
Everything seems to be well until there. It seems that a cdecl function could be coded like this:
Code: Select all
some_function:
push ebp ;save EBP
mov ebp,esp ;point EBP to current stack position
add ebp,8 ;discard, in EBP, the saved EBP size to point directly to parameters
;and also the return addres that was pushed by near CALL instruction
push ebx
push esi
push edi
pop edi
pop esi
pop ebx
pop ebp
ret
Now, what seems strange are the floating point values and how to handle them, which are said to be returned as a single return value in FP0 register of the FPU. First of all there doesn't seem to be a convention to save anything from the FPU state. Is this supposition correct?
The other problem is, how can a program using floating point values run in a machine without FPU, like a 386DX. How can a "float" type routine return value and "float" arguments in stack be handled at compile time?