Microsoft KB Archive/39520

From BetaArchive Wiki
Knowledge Base

Article ID: 39520

Article Last Modified on 10/16/2003


  • Microsoft Macro Assembler 5.1 Standard Edition
  • Microsoft Macro Assembler 6.0 Standard Edition
  • Microsoft Macro Assembler 6.0a
  • Microsoft Macro Assembler 6.0b

This article was previously published under Q39520


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.


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
EXTRN _fac:QWORD          ; __fac is the global variable used by C
                          ; for storing floating point accumulations
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

abc     ENDP

Program Output:

The value of y is 7.000000.

Additional query words: kbinf 5.10 6.00 6.00a 6.00b

Keywords: KB39520