macros suthida chaichomchuen std@kmitnb.ac.th. purposes to simplify and reduce the amount of...

Post on 05-Jan-2016

218 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MACROS Suthida Chaichomchuen

std@kmitnb.ac.th

PurposesTo 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 operationString-handling routines

Format of macro definition

macronameMACRO [parameter list][instructions]ENDM

;Define macro;Body of macro;End of macro

Simple macro definitions

INITZ MACRO ;Define macroMOV AX,@data ; BodyMOV DS,AX ; ofMOV ES,AX ; macro definitionENDM ;End of macro

Simple macro definitions

FINISH MACRO ;Define macroMOV AX,4C00H ;Body ofINT 21H ;macro definitionENDM ;End of macro

Using parameter in Macros

PROMPT MACRO MESSAGE;dummy argumentMOV AH,09HLEA DX,MESSAGEINT 21HENDM ;End of macro

Using parameter in Macros

2MESSAGE DB ‘Enter the date as mm/dd/yy’

PROMPT 2

Macro Comments

PROMPT MACRO MESSAGE; This macro permits a display of messages

MOV AH,09H ;Request displayLEA DX,MESSAGE ;promptINT 21HENDM

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

Macro Listing directive

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

PURGE Macro names

Example: PURGE PROMPT, DIVIDE

Concatenation

Used the ampersand (&) character tells the assembler to join text or symbols.

STRMOVE MACRO TAGREP MOVS&TAGENDM

REPT : Repeat DirectiveIRP : Indefinite Repeat Directive

IRPC : Indefinite Repeat Character Directive

Repetition directive

Repeat a block of statements up to ENDM according to the number of times in the expression entry.

REPT expression

Repetition directive : REPT

REPT 4DEC SI

EEEE

REPT : Examples

N = 0REPT 5

N = N + 1DB N

EEEE

REPT : Examples

Repeat a block of instructions up to ENDM.

IRP dummy, <argument>

Repetition directive : IRP

IRP N, <3, 9, 17, 25, 28>DB N

ENDM

IRP : Examples

IRP REG, <AX, BX, CX, DX>PUSH REG

ENDM

IRP : Examples

Repeat a block of statements up to ENDM.

IRPC dummy, string

Repetition directive : IRPC

IRPC N, 345678DW N

ENDM

IRPC : Examples

IFxx (condition) . . .

EEEE (optional) . . .

ENDIF (end of IF)

Conditional directives

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

- - 1 2IFIDN <arg >,<arg > : EEE EEEEE EEE-E EEEEEE EE E1

- 2dentical to the argument string, processthest at ement s i n t he condi EEEEEE EEE EEEEEEEE EEEEEEE EE. glebr acket s.

Conditional directives

- - 1 2IFDIF <arg >,<arg > : I - 1f the argument string is d

- 2ifferent to the argument string, processthest at ement s i n t he condi tionalblock. The ar gument r equi r e an glebr acket s.

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)

FINISH MACRO RETCODEMOV AH, 3CHIFNB <RETCODE>

MOV AL, RETCODEENDIFINT 21HENDM

Examples

INT21 MACRO FUNCTION, DXADDRESMOV AH, FUNCTNIFNB <DXADDRES>

MOV DX, OFFSET DXADDRESENDIFINT 21HENDM

Examples

IFxx [condition] . . . (invalid condition)

EEEEE . . .

ENDIF

EXITM directives

IF CNTR ; Macro expansion terminated

EXITMENDIF

Using IF and IFDEF conditions

IFIDN <&TAG>,<B>REP MOVSB. . .

Using IFIDN condition

IFIDN <&TAG>,<W>REP MOVSW. . .

Using IFIDN condition

top related