Function Return Values. STMicroelectronics PGI
বিজ্ঞাপন
বিজ্ঞাপন
Linux86 and Win32 Programming Model
Function Return Values
Functions Returning No Value
Functions that return no value are also called procedures or void functions. These functions put no particular value in any register.
Functions Returning Scalars
• A function that returns an integral or pointer value places its result in register %eax.
• A function that returns a long long integer value places its result in the registers %edx and %eax. The most significant word is placed in %edx and the least significant word is placed in %eax.
• A floating-point return value appears on the top of the floating point stack. The caller must then remove the value from the floating point stack, even if it does not use the value. Failure of either side to meet its obligations leads to undefined program behavior. The standard calling sequence does not include any method to detect such failures nor to detect return value type mismatches. Therefore, the user must declare all functions properly. There is no difference in the representation of single-, double- or extended-precision values in floating-point registers.
• A call instruction pushes the address of the next instruction (the return address) onto the stack. The return instruction pops the address off the stack and effectively continues execution at the next instruction after the call instruction. A function that returns a scalar or no value must preserve the caller's registers. Additionally, the called function must remove the return address from the stack, leaving the stack pointer (%esp) with the value it had before the call instruction was executed.
Functions Returning Structures or Unions
If a function returns a structure or union, then the caller provides space for the return value and places its address on the stack as argument word zero. In effect, this address becomes a hidden first argument.
A function that returns a structure or union also sets %eax to the value of the original address of the caller's area before it returns. Thus, when the caller receives control again, the address of the returned object resides in register %eax and can be used to access the object. Both the calling and the called functions must cooperate to pass the return value successfully:
• The calling function must supply space for the return value and pass its address in the stack frame;
• The called function must use the address from the frame and copy the return value to the object so supplied;
• The called function must remove this address from the stack before returning.
Failure of either side to meet its obligation leads to undefined program behavior. The standard function calling sequence does not include any method to detect such failures nor to detect structure and union type mismatches. Therefore, you must declare the function properly.
The following table illustrates the stack contents when the function receives control, after the call instruction, and when the calling function again receives control, after the ret
instruction.
358
ডাউনলোড
বিজ্ঞাপন