macros suthida chaichomchuen [email protected]. purposes to simplify and reduce the amount of...

37
MACROS Suthida Chaichomchuen [email protected]

Upload: catherine-snow

Post on 05-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

MACROS Suthida Chaichomchuen

[email protected]

Page 2: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

PurposesTo simplify and reduce the amount of repetitive coding.

To reduce errors caused by repetitive coding.

To make an assembly program more readable.

Page 3: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

Page 4: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Format of macro definition

macronameMACRO [parameter list][instructions]ENDM

;Define macro;Body of macro;End of macro

Page 5: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Simple macro definitions

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

Page 6: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Simple macro definitions

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

Page 7: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Using parameter in Macros

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

Page 8: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Using parameter in Macros

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

PROMPT 2

Page 9: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Macro Comments

PROMPT MACRO MESSAGE; This macro permits a display of messages

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

Page 10: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

.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

Page 11: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

Page 12: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

Page 13: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

The PURGE directive

Use to delete the unwanted macros from the currentassembly.

PURGE Macro names

Example: PURGE PROMPT, DIVIDE

Page 14: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Concatenation

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

STRMOVE MACRO TAGREP MOVS&TAGENDM

Page 15: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

REPT : Repeat DirectiveIRP : Indefinite Repeat Directive

IRPC : Indefinite Repeat Character Directive

Repetition directive

Page 16: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

REPT expression

Repetition directive : REPT

Page 17: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

REPT 4DEC SI

EEEE

REPT : Examples

Page 18: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

N = 0REPT 5

N = N + 1DB N

EEEE

REPT : Examples

Page 19: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Repeat a block of instructions up to ENDM.

IRP dummy, <argument>

Repetition directive : IRP

Page 20: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

ENDM

IRP : Examples

Page 21: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

ENDM

IRP : Examples

Page 22: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

Repeat a block of statements up to ENDM.

IRPC dummy, string

Repetition directive : IRPC

Page 23: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

IRPC N, 345678DW N

ENDM

IRPC : Examples

Page 24: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

IFxx (condition) . . .

EEEE (optional) . . .

ENDIF (end of IF)

Conditional directives

Page 25: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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 .

Page 26: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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 .

Page 27: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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.

Page 28: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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.

Page 29: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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.

Page 30: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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.

Page 31: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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)

Page 32: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

FINISH MACRO RETCODEMOV AH, 3CHIFNB <RETCODE>

MOV AL, RETCODEENDIFINT 21HENDM

Examples

Page 33: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

INT21 MACRO FUNCTION, DXADDRESMOV AH, FUNCTNIFNB <DXADDRES>

MOV DX, OFFSET DXADDRESENDIFINT 21HENDM

Examples

Page 34: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

IFxx [condition] . . . (invalid condition)

EEEEE . . .

ENDIF

EXITM directives

Page 35: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

IF CNTR ; Macro expansion terminated

EXITMENDIF

Using IF and IFDEF conditions

Page 36: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

Using IFIDN condition

Page 37: MACROS Suthida Chaichomchuen std@kmitnb.ac.th. Purposes  To simplify and reduce the amount of repetitive coding.  To reduce errors caused by repetitive

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

Using IFIDN condition