1 / 20

ICS312 Set 14

ICS312 Set 14. MACROS. Macros. The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions. Macros (Cont.).

tamal
Download Presentation

ICS312 Set 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS312 Set 14 MACROS

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

  3. 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.)

  4. 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 separated by commas. Definition

  5. 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

  6. 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 separated by commas.

  7. 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

  8. Macro expansion • 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

  9. 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.

  10. 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

  11. 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

  12. Listing Files If your program is called e.g. prog1.asm, then: ml /Fl prog1.asm will produce a listing file called prog1.lst, with the invoked macros expanded,and error messages following the 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.

  13. 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

  14. Example A 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 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.

  15. Macros that Invoke Other Macros • Macros may invoke other macros, or themselves. • When this happens, the assembler • expands each macro as it is encountered.

  16. 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

  17. 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.

  18. 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.

  19. Assembling a routine with macros If 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

  20. Textbook Reading (Jones): Chapter 8

More Related