Passing an Integer from C to MASM, Returning a Double

Last reviewed: January 23, 1995
Article ID: Q39520
The information in this article applies to:
  • Microsoft Macro Assembler for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0b

SUMMARY

The following MASM program shows how to receive an integer from a C program, then pass the value back to the C program as a double. Note: This routine was composed for the large-memory model and assumes that a coprocessor exists on the system.

MORE INFORMATION

Below is the C program that calls the MASM routine. It should be compiled for large-memory model and either 8087 or emulator-math library.

Also shown is the MASM routine. The main piece to this program is the FILD instruction that transforms the integer to a floating-point real, then pushes the value on the co-processor stack. The other point is to obtain the segment and offset of the __fac variable. A value requiring more than 4 bytes cannot be stored in the DX and AX registers for return, so a double is returned by storing its address in DX:AX.

Sample Code

/* Compile options needed: /AL /FPi87 (or /FPi)
*/

#include <stdio.h>

extern double abc( int x );

void main()
{
   int x;
   double y;

   x = 7;
   y = abc( x );
   printf( "%The value of y is %lf.\n", y );
}

; Assemble options needed: /Mx

        .MODEL LARGE,C    ; so Masm will use C naming conventions
        .DATA
        .CODE
EXTRN _fac:QWORD          ; __fac is the global variable used by C
                          ; for storing floating point accumulations
PUBLIC abc
abc     PROC    FAR
        push    bp
        mov     bp,sp
        fild    WORD PTR [bp+6]
        mov     dx,SEG _fac
        mov     es,dx
        fstp    QWORD PTR es:_fac  ; this pops the value off of
        mov     ax,OFFSET _fac     ; the co-processor and puts it
        fwait                      ; in __fac
        mov     sp,bp
        pop     bp
        ret

abc     ENDP
        END

Program Output:

The value of y is 7.000000.


Additional reference words: kbinf 5.10 6.00 6.00a 6.00b
KBCategory: kbprg
KBSubCategory: MASMLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 23, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.