130 likes | 309 Views
Macros. Covers chapter 8, pages 300 – 314. Run-time vs. Assemble-time. Almost everything but the main body of an assembly-language program deals with assemble-time Header Equates Data Closing For the most part, the only run-time portion is the body. Run-time vs. Assemble-time (cont.).
E N D
Macros Covers chapter 8, pages 300 – 314
Run-time vs. Assemble-time • Almost everything but the main body of an assembly-language program deals with assemble-time • Header • Equates • Data • Closing • For the most part, the only run-time portion is the body
Run-time vs. Assemble-time (cont.) • Programming the assembler • Directives – instructions to the assembler on how or what to assemble • Macros – programmer-defined directives
Macros • Macros exist to • avoid repetition (or avoid typing) • increase clarity • avoid function call overhead • Macros work by substitution • Types of macros • Replacing • Repeating
Replacing Macros • Syntax: MACRO <macro name> <parameters> <code or other macros> ENDM <macro name> • Example: MACRO Terminate mov ah, 04Ch mov al, [exCode] int 21h ENDM Terminate • Example program term.asm
.lst File • Created by the /l tasm command-line switch • Contains everything but the kitchen sink: • program code • machine code • expanded macros • symbol table • macro table • group and segment table • Extremely useful for debugging code with macros
Parameters • Formal parameters • only exist within the macro • do not represent immediate values or offsets • are text-replaced • Example: MACRO Inc2 v1 inc v1 inc v1 ENDM Inc2 • Example program inc2.asm
Parameters (cont.) • Actual parameters are the text that is actually used when substituting the macro • Example: Inc2 ax • The text “ax” is the actual parameter • Example program shleft.asm
Parameter Modifiers • Actual parameters • <> - forces parameter to contain spaces • % - forces evaluation first (use with “()”) • ! – “escapes” other two • Formal parameters • & - recognize next word as a parameter • Example program shleft2.asm • Example program asciiz.asm
Repeating Macros • IRP – Instruction RePeat • Syntax: IRP <formal param>, <element, element, …> <instruction> ENDM • Repeats the instruction (in the source code itself) for every element • Example programs save.asm and save2.asm
Local Labels • Consider the following: MACRO IncNum op, count push cx mov cx, count @@Back: inc op loop @@Back pop cx ENDM IncNum
Local Labels (cont.) • The LOCAL directive solves the problem: MACRO IncNum op, count LOCAL @@Back push cx mov cx, count @@Back: inc op loop @@Back pop cx ENDM IncNum