





						Global Declarations



		 6.1  Introduction

		 The Global Declaration	directives let a programmer
		 define	 labels,  variables,  and  absolute symbols
		 that can be accessed globally,	that is,  from	all
		 modules   in	a   program.   Global  declarations
		 transform ``local''  symbols  (labels,	 variables,
		 and  other  symbols  that  can	be used	only in	the
		 source	files  in  which  they	are  defined)  into
		 ``global'' symbols that are available to all other
		 modules.

		 There	are  the   following   Global	Declaration
		 directives:

		      PUBLIC	  EXTRN

		 The   PUBLIC	directive   is	 used	in   public
		 declarations.	A  public  declaration transforms a
		 locally  defined  symbol  into	 a  global  symbol,
		 making	 it  available to other	modules.  The EXTRN
		 directive is used in  external	 declarations.	 An
		 external  declaration makes a global symbol's name
		 and type known	 in  a	source	file,  letting	the
		 global	 symbol	be used	in that	file.  Every global
		 symbol	must have a public declaration	in  exactly
		 one  source  file of the program.  A global symbol
		 can have external declarations	in  any	 number	 of
		 other	 source	  files.   The	following  sections
		 describe  the	Global	Declaration  directives	 in
		 detail.











								6-1



















	      XENIX Macro Assembler Reference Manual



	      6.2  PUBLIC Directive

	      Syntax

		   PUBLIC name,,,

	      The PUBLIC directive makes the variable, label, or
	      absolute	symbol	given  by  name	available to all
	      other modules in the program.  The  name	must  be
	      the  name	of a variable, label, or absolute symbol
	      defined within the current source	file.	Absolute
	      symbols, if given, can only represent 1- or 2-byte
	      integer or string	values.

	      MASM converts all	lowercase  letters  in	name  to
	      uppercase	 before	 copying  the name to the object
	      file.  The -M1 and -Mx options can be used in  the
	      MASM  command  line  to  direct  MASM  to	preserve
	      lowercase	letters	when copying to	the object file.
	      See  Chapter 2 in	the XENIX Macro	Assembler User's
	      Guide for	more information.

	      Example

			   public  true, test, start
		   true	   =	   0FFFFH      test    db      1
		   start   label   far















	      6-2



















						Global Declarations



		 6.3  EXTRN Directive

		 Syntax

		      EXTRN  name:type ,,,

		 The EXTRN directive defines an	external  variable,
		 label,	 or  symbol  named  name  and whose type is
		 type.	An external item is any	variable, label, or
		 symbol	 that has been publicly	declared in another
		 module	of the program.

		 The name must be the name of a	variable, label, or
		 symbol	 defined  in  another module of	the program
		 and  listed  in  a   PUBLIC   directive   of	the
		 corresponding source file. The	type must match	the
		 type given to the item	in its	actual	definition.
		 It can	be any one of the following:

		      BYTE		      WORD
		      DWORD	      QWORD
		      TBYTE		      FAR	       NEAR
		      ABS

		 The  ABS  type	 is  reserved  for   symbols   that
		 represent absolute numbers.

		 Although the  actual  address	is  not	 determined
		 until	link  time,  the  assembler  may  assume  a
		 default segment for the  external  item  based	 on
		 where the EXTRN directive is placed in	the module.
		 If the	directive is placed inside a  segment,	the
		 external  item	 is  assumed to	be relative to that
		 segment.   In	this  case,   the   item's   public
		 declaration  (in  some	 other module) must be in a
		 segment having	the same name and  attributes.	 If
		 the   directive   is	outside	 all  segments,	 no
		 assumption is made about what segment the item	 is
		 relative to, and the item's public declaration	can
		 be in any segment in any module.   In either case,



								6-3


















	      XENIX Macro Assembler Reference Manual



	      the  segment  override operator (:) can be used to
	      override an external variable's or label's default
	      segment.

	      Example

		   extrn   tagn:near
		   extrn   var1:word, var2:dword



	      6.4  Program Example

	      The following source files  illustrate  a	 program
	      that  uses  public  and  external	 declarations to
	      access instruction labels.  The  program	consists
	      of    two	   modules,   named   ``startmod''   and
	      ``printmod.''  The  ``startmod''	module	is   the
	      program's	 main  module.	 Execution starts at the
	      instruction labeled ``_main'' in ``startmod,'' and
	      passes  to  the  instruction labeled ``_print'' in
	      ``printmod'' where the  XENIX  printf  routine  is
	      called  to  print	 the  message  ``Hello''  at the
	      system console.  Execution  then	returns	 to  the
	      instruction labelled ``_finish'' in ``startmod.''

	      Startmod Module:















	      6-4



















						Global Declarations



			      NAME    startmod
			      public  _main, _finish
			      extrn   _exit:near
			      extrn   _print:near

		      _DATA   segment word public 'DATA'
		      _DATA   ends	DGROUP	group	_DATA

		      _TEXT   segment byte public 'CODE'
			      assume cs:_TEXT,ds:DGROUP

		      _main:			     jmp     _print
		      finish:
			      push    0		      ;exit(0)
			      call    _exit	       _TEXT   ends
			      end

		 Printmod Module:

			      NAME    printmod
			      public  _print
			      extrn   _finish:near
			      extrn   _printf:near

		      _DATA   segment word public 'DATA'
		      string  db      "Hello.",	10, 0
		      _DATA   ends	DGROUP	group _DATA

		      _TEXT   segment byte public 'CODE'
			      assume  cs:_TEXT,	ds:DGROUP
		      _print:
			      push    offset DGROUP:string
			      call    _printf
			      add     sp, 2
			      jmp     _finish	    _TEXT   ends

			      end

		 In this example,  ``startmod''	 publicly  declares
		 two symbols, ``_main''	and ``_finish,'' making	the



								6-5


















	      XENIX Macro Assembler Reference Manual



	      symbols available	to the other source file in  the
	      program.	  Both	of  these  symbols  are	 locally
	      defined as instruction labels later in the  source
	      file,  and  therefore  can  be used as instruction
	      labels in	the other source file.	The ``startmod''
	      file  also contains an external declaration of the
	      symbol  ``_print.''   This   declaration	 defines
	      ``_print''  to  be  a near label and is assumed to
	      have been	publicly declared in  the  other  source
	      file.   The  label  is  used  in a JMP instruction
	      given later in the file.

	      The   ``printmod''   file	  contains   a	  public
	      declaration   of	the  symbol  ``_print''	 and  an
	      external declaration of the symbol ``_finish.'' In
	      this case, ``_print'' is locally defined as a near
	      label and	matches	the external  declaration  given
	      to  it  in ``startmod.'' The symbol ``_finish'' is
	      declared	to  be	a  near	 label,	  matching   its
	      definition in ``startmod.''

	      Before this program can be executed, these  source
	      files  must be assembled individually, then linked
	      together using the system	linker.


















	      6-6























		 Chapter 6


		 Global	Declarations
		 __________________________________________________



		 6.1  Introduction  6-1

		 6.2  PUBLIC Directive	6-2

		 6.3  EXTRN Directive  6-3

		 6.4  Program Example  6-4









































