Microsoft KB Archive/104617

= How to pass a shot between a program that is written in C/C++ and a function that is written with Microsoft Macro Assembler (MASM) =

Article ID: 104617

Article Last Modified on 6/1/2005

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q104617





SUMMARY
The sample code below demonstrates how to pass a short from a program that is written in Microsoft C to a procedure that is written with the Microsoft Macro Assembler (MASM). The MASM function also returns a short to the C program.

Registers are used to return values of simple data types. For 16-bit code, such as an MS-DOS program, use the following conventions for returning data to a C program: char                  AL short, int, near *     AX long, far *            DX:  High order portion (segment) AX: Low order portion (offset) For 32-bit code, such as a Windows NT program, use the following conventions for returning data to a C program: char                  AL short                  AX long, int, *           EAX The samples below include one C file and two different assembly files. The two assembly files demonstrate how to pass a variable in small model for MS-DOS and in flat model for Windows NT. Link only the appropriate assembly module to the C module.

Note that MASM 6.1 or later and the C/C++ 32-bit compiler that ships with Visual C++, 32-bit Edition, are required to build the flat model Windows NT version.

Sample Code
// Filename: CMAIN.C // Compile options needed: /c


 * 1) include 

extern "C" {
 * 1) ifdef __cplusplus
 * 1) endif

short MasmSub(short);

}
 * 1) ifdef __cplusplus
 * 1) endif

main {  short var = 1; printf ("%d\n", var); printf ("%d", MasmSub(var)); }

Sample Code for MS-DOS Small Model Version
; Filename: MASMSUB.ASM
 * Assemble options needed for MASM: /MX
 * Assemble options needed for ML: /c /Cx

.MODEL small, C .286 .CODE

MasmSub PROC, \ sVar:WORD

mov ax, sVar    ; Load the short into AX. add ax, 32766   ; Because the function returns a short (a 2-byte   ret              ; value), C will get the return value from AX. MasmSub ENDP END

Sample Code for Windows NT Flat Model Version
; Filename: MASMSUB.ASM
 * Assemble options needed for ML: /c /Cx /coff

.386 .MODEL flat, C .CODE

MasmSub PROC, \ sVar:WORD

mov ax, sVar    ; Load the short into AX. add ax, 32766   ; Because the function returns a short (a 2-byte   ret              ; value), C will get the return value from AX. MasmSub ENDP END

The following is the output of the program:  1 32767

Additional query words: mixed language

Keywords: kbhowtomaster kblangc kbcode KB104617

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.