Microsoft KB Archive/104616

= INFO: Sharing Common C Types with MASM =

Article ID: 104616

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft C Professional Development System 6.0a
 * Microsoft C Professional Development System 6.0a
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5

-



This article was previously published under Q104616





SUMMARY
The sample code below demonstrates how to share variables of type char, short, and long between a Microsoft C program and a Microsoft Macro Assembler (MASM) program. This is accomplished by declaring the C variables outside the a function definition, which makes them public. The MASM subprogram can gain access to these public variables by declaring them with the EXTRN directive. The EXTRN directive has the following format   EXTRN : where represents the public name of the variable as it is declared in the main module, and can be either BYTE, WORD, DWORD, FWORD, QWORD, or TBYTE. Note that in MASM 6.0 and later, EXTRN is a synonym for EXTERN. If you are using MASM 6.x, then you should use EXTERNDEF, because it is more flexible when used in different contexts.



MORE INFORMATION
The samples below include one C file and two different assembly files. The two assembly files demonstrate how to share variables 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

void MasmFunc(void); char charvar = 'a';      // Declaring variables outside short shortvar = 1;      // of a function definition long longvar = 32768L;   // makes them public.

}
 * 1) ifdef __cplusplus
 * 1) endif

main {  while (shortvar < 11)  // Display and increment variables 11 times {     printf ("%c %d %ld\n", charvar, shortvar, longvar) ; MasmFunc ; } }

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

.DATA                           ; NOTE: You can put these as EXTERNDEF ; in .INC file and include it here. EXTRN charvar:BYTE       ; The EXTRN directive enables a MASM EXTRN shortvar:WORD       ; procedure to access public variables. EXTRN longvar:DWORD

.CODE MasmFunc PROC inc charvar inc shortvar add WORD PTR longvar, 1 adc WORD PTR longvar+2, 0 ret MasmFunc 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

.DATA                            ; NOTE: You can put these as EXTERNDEF ; in .INC file and include it here. EXTERNDEF charvar:BYTE     ; The EXTRN directive enables a MASM EXTERNDEF shortvar:WORD    ; procedure to access public variables. EXTERNDEF longvar:DWORD

.CODE MasmFunc PROC inc charvar inc shortvar inc longvar ret MasmFunc ENDP END The following is the output of the program:   a 1 32768 b 2 32769 c 3 32770 d 4 32771 e 5 32772 f 6 32773 g 7 32774 h 8 32775 i 9 32776 j 10 32777

Additional query words: 8.00 8.00c 9.00 mixed language

Keywords: kbinfo kbcode kbcompiler KB104616

-

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

© Microsoft Corporation. All rights reserved.