370 likes | 529 Views
MACROS. Suthida Chaichomchuen std@kmitnb.ac.th. Purposes. To simplify and reduce the amount of repetitive coding. To reduce errors caused by repetitive coding. To make an assembly program more readable. Functions that implemented by macro.
E N D
MACROS Suthida Chaichomchuen std@kmitnb.ac.th
Purposes • To simplify and reduce the amount of repetitive coding. • To reduce errors caused by repetitive coding. • To make an assembly program more readable.
Functions that implemented by macro • Input/Output operation that load register and perform interrupt • Conversions of ASCII and binary data • Multiword arithmetic operation • String-handling routines
Format of macro definition macroname MACRO [parameter list] [instructions] ENDM ;Define macro ;Body of macro ;End of macro
INITZ MACRO ;Define macro MOV AX,@data ; Body MOV DS,AX ; of MOV ES,AX ; macro definition ENDM ;End of macro Simple macro definitions
FINISH MACRO ;Define macro MOV AX,4C00H ;Body of INT 21H ;macro definition ENDM ;End of macro Simple macro definitions
Using parameter in Macros PROMPT MACRO MESSAGE ;dummy argument MOV AH,09H LEA DX,MESSAGE INT 21H ENDM ;End of macro
Using parameter in Macros MESSAGE2 DB ‘Enter the date as mm/dd/yy’ PROMPT MESSAGE2
Macro Comments PROMPT MACRO MESSAGE ; This macro permits a display of messages MOV AH,09H ;Request display LEA DX,MESSAGE ;prompt INT 21H ENDM
Macro Listing directive • .LALL : List all - including the leading period. • .SALL : suppress all - not list any source code of a macro expansion • .XALL : default - list only the instructions that generate object code.
The LOCAL directive Used for define the data items and instruction labels within the macro definition itself. LOCAL dummy-1, dummy-2, . . ;One or more dummy argument
Including Macros from a library Programs can use any of the cataloged macros by use an INCLUDE directive like this. INCLUDE Path and file name Example: INCLUDE F:\MACRO.LBY
The PURGE directive Use to delete the unwanted macros from the current assembly. PURGE Macro names Example: PURGE PROMPT, DIVIDE
Concatenation Used the ampersand (&) character tells the assembler to join text or symbols. STRMOVE MACRO TAG REP MOVS&TAG ENDM
Repetition directive • REPT : Repeat Directive • IRP : Indefinite Repeat Directive • IRPC : Indefinite Repeat Character Directive
Repetition directive : REPT Repeat a block of statements up to ENDM according to the number of times in the expression entry. REPT expression
REPT : Examples REPT 4 DEC SI ENDM
REPT : Examples N = 0 REPT 5 N = N + 1 DB N ENDM
Repetition directive : IRP Repeat a block of instructions up to ENDM. IRP dummy, <argument>
IRP : Examples IRP N, <3, 9, 17, 25, 28> DB N ENDM
IRP : Examples IRP REG, <AX, BX, CX, DX> PUSH REG ENDM
Repetition directive : IRPC Repeat a block of statements up to ENDM. IRPC dummy, string
IRPC : Examples IRPC N, 345678 DW N ENDM
Conditional directives IFxx (condition) . . . ELSE (optional) . . . ENDIF (end of IF)
Conditional directives • IF : If the expression evaluates to a nonzero value, assemble the statements within the conditional block . • IFE : If the expression evaluates to a zero, assemble the statements within the conditional block .
Conditional directives • IF1 (no expression) : If processing pass 1, act on the statements in the conditional block . • IF2 (no expression) : If processing pass 2, act on the statements in the conditional block .
Conditional directives • IFDEF symbol : If the symbol is defined in the program or is declared as EXTRN, process the statements in the conditional block. • IFNDEF symbol : If the symbol is not defined or is not declared as EXTRN, process the statements in the conditional block.
Conditional directives • IFB <argument> : If the argument is blank, process the statements in the conditional block. The argument requires angle brackets. • IFNB <argument> : If the argument is not blank, process the statements in the conditional block. The argument requires angle brackets.
Conditional directives • IFIDN <arg-1>,<arg-2> : If the argument-1 string is identical to the argument-2 string, process the statements in the conditional block. The argument require angle brackets.
Conditional directives • IFDIF <arg-1>,<arg-2> : If the argument-1 string is different to the argument-2 string, process the statements in the conditional block. The argument require angle brackets.
Conditional directives • IF and IFE can use the relational operators : • EQ (equal) • NE (not equal) • LT (less than) • LE (less than or equal) • GT (greater than) • GE (greater than or equal)
Examples FINISH MACRO RETCODE MOV AH, 3CH IFNB <RETCODE> MOV AL, RETCODE ENDIF INT 21H ENDM
Examples INT21 MACRO FUNCTION, DXADDRES MOV AH, FUNCTN IFNB <DXADDRES> MOV DX, OFFSET DXADDRES ENDIF INT 21H ENDM
EXITM directives IFxx [condition] . . . (invalid condition) EXITM . . . ENDIF
Using IF and IFDEF conditions IF CNTR ; Macro expansion terminated EXITM ENDIF
Using IFIDN condition IFIDN <&TAG>,<B> REP MOVSB . . .
Using IFIDN condition IFIDN <&TAG>,<W> REP MOVSW . . .