Macros. Program structure similar to procedure. As for procedure names, macro names represent a...

20
Macros

Transcript of Macros. Program structure similar to procedure. As for procedure names, macro names represent a...

Macros

Macros Program structure similar to procedure.

As for procedure names, macro names represent a group of instructions

Macros (Cont.) A procedure is called at execution

time; control transfers to the procedure and returns after executing its statements.

A macro is invoked at assembly time.

The assembler copies the macro's statements into the program at the position of the invocation. (This is called a macro expansion.)

Definition•A macro is a block of text that has been given a name. •When the assembler encounters the name during assembly, it inserts the block into the program. •The text may consist of instructions, pseudo-ops, comments, or references to other macros.

Syntax: macro_name  MACRO      d1, d2, …, dn             statements (body of macro)            ENDM

Where d1, d2, …, dn is a list of dummy arguments.

Example

We know that the operands of the MOV instruction cannot both be word variables, but we can define a macro to move a word source variable to a word destination variable.

MOVW    MACRO WORD1, WORD2         PUSH WORD2        POP WORD1        ENDM

How to use a Macro

To use a macro in a program, we invoke it.

Syntax:

macro_name a1, a2, a3,…,an

where a1, a2, …, an is a list of actual arguments.

Example on using Macro

Invoke the macro MOVW to move B to A, where A and B are word variables.

.DATA A  DW ? B  DW ?

.CODE … MOVW A, B

Macro expansionMacro expansion - character substitution of actual arguments.

These instructions: MOVW A, DX MOVW A+2, B

Insert the following (equivalent) code into a program:   PUSH DX POP A PUSH B POP A+2

Restoring Registers 

Good programming practice :

•A procedure should restore the registers that it uses, unless they contain output values.

•The same is true for macros.

Example Program Listing.MODEL SMALL.STACK 100H

.DATA A DW 1,2 B DW 3

.CODE MOVW MACRO WORD1,WORD2     PUSH WORD2     POP WORD1     ENDM

Program Listing (Cont.)MAIN PROC     MOV AX,@DATA     MOV DS,AX   

MOVW A,DX     MOVW A+2,B

;dos exit     MOV AH,4C00H     INT 21H MAIN ENDP     END MAIN

Listing FilesIf your program is called e.g. prog1.asm, then: ml /Fl prog1.asmwill produce a listing file called prog1.lst, with the invoked macros expanded, and error messages followingthe lines they refer to.

Finding Assembly Errors

Errors found during macro expansion are listed at the point of the macro invocation. To find where the mistake really is, you need to inspect the macro expansion in the .LST file --- this is especially helpful if the macro invokes other macros.

Local Labels•A macro with a loop or decision structure may contain labels. •If such a macro is invoked more than once in a program, duplicate labels occur, causing an assembly error. •To avoid this, generate local labels in macros by using the LOCAL pseudo-op in the macro declaration.   •The LOCAL directive must appear on the first line of the  macro following the name of the macro.

Syntax:

    LOCAL list of labels

ExampleA macro to place the largest of two words in AX:

GET_BIG   MACRO   WORD1, WORD2      LOCAL OUT         ; local label      MOV AX, WORD1      CMP AX, WORD2      JG OUT      MOV AX, WORD2OUT: ENDM  Successive invocations of this macro causes the assemblerto insert labels ??0000, ??0001, ??0002, etc. into the program. These labels are unique and not likely to conflict with labels chosen by the user.

Macros that Invoke Other Macros

•Macros may invoke other macros, or themselves.

•When this happens, the assembler expands each macro as it is encountered.

Macro Libraries

•Macros may be stored in separate files, called libraries.  •Use the INCLUDE pseudo-op to include the library file in a program.   •This copies all of the macros defined in the library file into the program listing.  •The INCLUDE statement can appear anywhere before the included macros are invoked in the program.

Example of syntax:

    INCLUDE    A:\MACROS.INC         ; path and filename

Tradeoffs between Macros and Procedures

Assembly Time A program containing macros normally takes

longer to assemble than a program containing procedures because of the time required to do the macro expansions.

Execution Time A program containing macros typically executes

faster than a program with procedures, because the macro code is "in-line" and there is no requirement to transfer control.

Tradeoffs between Macros and Procedures (Cont.)

Program Size A program containing macros will usually be larger than

a program with procedures because of the additional lines of code generated during the macro expansion.

Best Uses of Macros and Procedures Macros are very good for short, frequently executed

tasks (e.g., short utility functions).  They are easier to write

and have greater readability than procedures. Procedures are good for handling longer, more complicated (and less frequently used) tasks.

Assembling a routine with macrosIf the program is called prog1, then employ:

ml /Fl /Zi prog1.asm util.lib

The “/Fl” option causes a listing program called, in this case, prog1.lst, to be generated. It shows were assembler errors occur, and supplies the macro expansions.

The “/Zi” option is to produce a program (prog1.exe), which you can debug using: cv prog1

Textbook Reading (Jones):

Chapter 8