Article ID: 104616
Article Last Modified on 7/5/2005
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. 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.
// Filename: CMAIN.C
// Compile options needed: /c
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
void MasmFunc(void);
char charvar = 'a'; // Declaring variables outside
short shortvar = 1; // of a function definition
long longvar = 32768L; // makes them public.
#ifdef __cplusplus
}
#endif
main ()
{
while (shortvar < 11) // Display and increment variables 11 times
{
printf ("%c %d %ld\n", charvar, shortvar, longvar) ;
MasmFunc () ;
}
}
; 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
; 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