ICS 312 Set 14 MACROS Macros n n

  • Slides: 20
Download presentation
ICS 312 Set 14 MACROS

ICS 312 Set 14 MACROS

Macros n n The program structure is similar to that for procedures. As for

Macros n n The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions

Macros (Cont. ) n n n A procedure is called at execution time; control

Macros (Cont. ) n n n 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

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 d 1, d 2, …, dn statements (body of macro) ENDM Where d 1, d 2, …, dn is a list of dummy arguments separated by commas.

Example We know that the operands of the MOV instruction cannot both be word

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 WORD 1, WORD 2 PUSH WORD 2 POP WORD 1 ENDM

How to use a Macro To use a macro in a program, we invoke

How to use a Macro To use a macro in a program, we invoke it. Syntax: macro_name a 1, a 2, a 3, …, an where a 1, a 2, …, an is a list of actual arguments separated by commas.

Example on using Macro Invoke the macro MOVW to move B to A, where

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 expansion - character substitution of actual arguments separated by commas. These instructions: MOVW

Macro expansion - character substitution of actual arguments separated by commas. 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

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 100 H. DATA A DW 1, 2 B

Example Program Listing. MODEL SMALL. STACK 100 H. DATA A DW 1, 2 B DW 3. CODE MOVW MACRO WORD 1, WORD 2 PUSH WORD 2 POP WORD 1 ENDM

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

Program Listing (Cont. ) MAIN PROC MOV AX, @DATA MOV DS, AX MOVW A, DX MOVW A+2, B ; dos exit MOV AH, 4 C 00 H INT 21 H MAIN ENDP END MAIN

Listing Files If your program is called e. g. prog 1. asm, then: ml

Listing Files If your program is called e. g. prog 1. asm, then: ml /Fl prog 1. asm will produce a listing file called prog 1. lst, with the invoked macros expanded, and error messages following the lines they refer to. Finding Assembly 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 • If such a macro is invoked more than once in a

Local Labels • If such a macro is invoked more than once in a program, duplicate labels occur, causing an assembly error. • To avoid this, get the Assembler translator to substitute unique labels at each invocation of the macro by declaring the label as local. • The LOCAL declaration must appear on the first line of the macro following the name of the macro. Syntax: LOCAL list of labels

Example A macro to place the largest of two words in AX: GET_BIG MACRO

Example A macro to place the largest of two words in AX: GET_BIG MACRO WORD 1, WORD 2 LOCAL OUT ; local label MOV AX, WORD 1 CMP AX, WORD 2 JG OUT MOV AX, WORD 2 OUT: ENDM Successive invocations of this macro causes the assembler to insert labels ? ? 0000, ? ? 0001, ? ? 0002, etc. into the program in place of “OUT”. 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. •

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

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 n Assembly Time A program containing macros normally takes

Tradeoffs between Macros and Procedures n 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. n 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. ) n Program Size A program containing macros

Tradeoffs between Macros and Procedures (Cont. ) n 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. n 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 macros If the program is called prog 1, then employ:

Assembling a routine with macros If the program is called prog 1, then employ: ml /Fl /Zi prog 1. asm util. lib The “/Fl” option causes a listing program called, in this case, prog 1. lst, to be generated. It shows were assembler errors occur, and supplies the macro expansions. The “/Zi” option is to produce a program (prog 1. exe), which you can debug using: cv prog 1

Textbook Reading (Jones): Chapter 8

Textbook Reading (Jones): Chapter 8