Macro Library 

The macros that a program invokes can be stored in a file. This makes it possible to create a library file of useful macros and use it using the INCLUDE pseudo-op. For example, suppose the file's name is MACROS, on a disk in drive A. When the assembler encounters the pseudo-op

    INCLUDE A:MACROS
in a program, it copies all the macro definitions from the file MACROS into the program at the position of the INCLUDE statement.

 Position of INCLUDE Statement

Examples of Useful Macros

The following are examples of macros that are useful to have in a macro library.

Example: Write a macro to return to DOS.
   DOS_RTN MACRO     
             MOV AH, 4CH
             INT 21H
           ENDM
 The macro invocation is DOS_RTN         
 

Example: Write a macro to display a carriage return and line feed.
   NEW_LINE MACRO
              MOV AH, 2
              MOV DL, 0DH
              INT 21H
              MOV DL, 0AH
              INT 21H
            ENDM
 The macro invocation is NEW_LINE         
 

Example: Write a macro to display a character string such that the string is a macro parameter.
   DISP_STR MACRO  STRING
              LOCAL START, MSG
   ; save registers
              PUSH AX
              PUSH DX
              PUSH DS
              JMP START
          MSG DB  STRING, '$'
       START:
              MOV AX, CS
              MOV DS, AX
              MOV AH, 9
              LEA DX, MSG
              INT 21H
    ; restore registers
              POP DS
              POP DX
              POP AX
            ENDM
 Sample invocation: DISP_STR "This is a string" 
 

When the DISP_STR macro is invoked, the string argument replaces the STRING parameter. Because the string is being stored in the code segment, CS must be moved to DS.

Using a Macro Library

The preceding macros have been placed in file MACROS. In the following example, we will show how to use a macro library in a program.

Example: Write a program that uses the macro library MACROS to display a message, go to a new line, and display another message.
   .MODEL SMALL
   .STACK 100H
   INCLUDE MACROS
   .CODE
   MAIN    PROC
           DISP_STR   "This is the first line"
           NEW_LINE         
           DISP_STR   "This is the second line" 
           DOS_RTN
   MAIN    ENDP
           END MAIN