INF: Sharing Common C Variables with MASM

ID Number: Q76510

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.00 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

You can share variables between a C main program and a MASM

subprogram. This is accomplished by declaring the C variables outside

the main() function definition, which makes them public names. 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 <name>:<type>

where <name> represents the public name of the variable as it is

declared in the main module, and <type> can be either BYTE, WORD,

DWORD, FWORD, QWORD, or TBYTE.

More Information:

The following program examples illustrate how to share variables

between a C main program and a MASM subprogram. The types of variables

used are char, int, and long, which have corresponding EXTRN types of

BYTE, WORD, and DWORD.

Link the two programs below with the following command:

link cmain masmsub,,, /nod llibce;

Sample Code 1

-------------

// Filename: CMAIN.C

// Compile options needed: /c /AL

#include <stdio.h>

extern void far MasmSub () ;

char charvar = 'a' ; // Declaring variables outside of a function

int intvar = 1 ; // definition makes them public.

long longvar = 32768 ;

main ()

{

while (intvar < 11) // Display and increment variables 3 times.

{

printf ("%c %d %ld\n", charvar, intvar, longvar) ;

MasmSub () ;

}

}

Sample Code 2

-------------

; Filename: MASMSUB.ASM

; Assemble options needed: /mx /ml

.MODEL LARGE, C

.DATA

EXTRN charvar:BYTE ; The EXTRN directive enables a MASM

EXTRN intvar: WORD ; procedure to access public variables.

EXTRN longvar:DWORD

.CODE

PUBLIC MasmSub

MasmSub PROC FAR

INC charvar

INC intvar

INC WORD PTR longvar ; Incrementing the low word of longvar adds

RET ; 1 to the value

MasmSub ENDP

END

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00