8.2.3 Positioning External Declarations

Although LINK determines the actual address of an external symbol, the assembler assumes a default segment for the symbol, based on the location of the external directive in the source code. You should therefore position EXTERN and EXTERNDEF directives according to these rules:

If you know which segment defines an external symbol, put the EXTERN statement in that segment.

If you know the group but not the segment, position the EXTERN statement outside any segment and reference the variable with the group name. For example, if var1 is in DGROUP, you would reference the variable as mov DGROUP:var1, 10.

If you know nothing about the location of an external variable, put the EXTERN statement outside any segment. You can use the SEG directive to access the external variable like this:

mov ax, SEG var1

mov es, ax

mov ax, es:var1

If the symbol is an absolute symbol or a far code label, you can declare it external anywhere in the source code.

Summary: Always close opened segments.

Any segments opened in include files should always be closed so that external declarations following an include statement are not incorrectly placed inside a segment. Any include statements in your program should immediately follow the .MODEL, OPTION, and processor directives.

For the same reason, if you want to be certain that an external definition is outside a segment, you can use @CurSeg. The @CurSeg predefined symbol returns a blank if the definition is not in a segment. For example,

.DATA

.

.

.

@CurSeg ENDS ; Close segment

EXTERNDEF var:WORD

See Section 1.2.3, “Predefined Symbols,” for information about predefined symbols such as @CurSeg.