Fortran/MASM Alignment and Return Value Considerations

Visual Fortran allows you to specify alignment for all data objects. Requesting alignment specifies that bytes may be added as padding, so that the object and its data start on a natural boundary (see Data Alignment Considerations). The MASM default is byte-alignment, so you should specify an alignment of 4 for MASM structures or use the Fortran compiler option /alignment:keyword (or /Zpn).

Your MASM procedure can return a value to your Fortran routine if you prototype it as a function. All return values of 4 bytes or less (except for floating-point values) are returned in the EAX register.

Procedures that return floating-point values return their results on the floating-point processor stack. This is possible because there is always a coprocessor or emulator available for 32-bit compilers.

To return REAL and COMPLEX floating-point values, records, arrays, and values larger than 4 bytes and return user-defined types larger than 8 bytes from assembly language to Fortran, you must use a special convention. Fortran creates space in the stack segment to hold the actual return value and passes an extra parameter as the last parameter pushed onto the stack. This extra parameter contains the address of the stack space that contains the return value. For user-defined types, values of 4 bytes or less are returned in EAX and values of 5 to 8 bytes are returned in EAX:EDX.

In the assembly procedure, put the data for the return value at the location pointed to by the return value offset. Then copy the return-value offset (located at EBP+8 if you've created a stack frame in your assembly code) to EAX. This is necessary because the calling module expects EAX to point to the return value.

The following table summarizes ways to return values.

Summary of Ways to Return Values

Type of Value to Return Method of Returning Value
Integer, logical variable, or user-defined type of size 4 bytes or less Return value in EAX register
Floating-point variable Return value on the FPU stack
Variable of size more than 4 bytes (strings, complex values) or user-defined types more than 8 bytes Return value on stack, address of value in EAX register
User-defined structures between 5 and 8 bytes Return value in EAX:EDX registers.