Macro can be defined as a group of repetitive
instructions in a program that are codified only once but can be repeated n
number of times.
Syntax
Macro_name MACRO argument1, argument2,…..argumentn
Statement
1
Statement
2
Statement
k
EndM
An actual argument can be any variable, immediate value
or a register name. They may also be
duplicates of other names like labels, variables etc.
A macro call designed to generate assembly language
instructions must be called in a code segment.
A macro call designed to generate data definitions must
appear in a portion o f a program where it will not be considered as an
instruction.
Lets take an example. A complex 8086 program contains
many complicated procedures. Each time a procedure is called all the flags need
to be stored in stack and then retrieved. Adding long pushing lines at the
beginning of the procedures and poping lines at the end of the procedure. So
instead of writing these lines repetitively in every procedure we create 2
macros. One for pushing and other for poping. Like this.
PUSH_ALL MACRO
}
PUSH F
}
PUSH AX
}
PUSH BX
}
PUSH CX
}
PUSH DX
}
PUSH BP
}
PUSH SI
}
PUSH DI
}
PUSH DS
}
PUSH ES
}
PUSH SS
ENDM
|
Similar to this one there will be POP_ALL MACRO.
Now calling macros inside a procedure.
RATE PROC FAR
ASSUME CS:PROCEDURES, DS:PARAMETERS
PUSH_ALL ; macro call
;initialize statements
MOV AX, PARAMETERS
MOV DS, AX
RATE ENDP
PROCEDURES ENDS
|
A macro definition can occur anywhere before END
directive.
A macro definition cannot occur inside another macro.
Usually all macros are placed at beginning of program before segment
definition.
A macro definition is not assembled until macro is
called. Each macro call is replaced by statements in the macro.
Passing parameters to a macro.
Simple example
MOV_NUMBERS MACRO
NUMBER,SOURCE,DESTINATION
MOV CX,NUMBER
LEA SI, SOURCE
LEA DI, DESTINATION
CLD
REP MOVSB; copy from SI to DI till
counter 0
MOV_NUMBERS ENDM
|
Nested macros.
A macro calling another macro is called a nested macro.
TASM and MASM can contain a macro that calls a previously defined macros. All
macros should be defined before segment definition though their order can be
anything. Like for example…
DISPLAY_CHAR MACRO
CHAR
PUSH AX
PUSH DX
MOV AH , 02H
MOV DL ,
CHAR
INT 21H
POP DX
POP AX
ENDM
CRLF
MACRO
DIPLAY_CHAR 0DH
DISPLAY_CHAR 0AH
ENDM
|
No comments:
Post a Comment