Professional Documents
Culture Documents
Coal 13
Coal 13
Lesson Plan 12
Objectives: The main objectives are: (a) Macros; (b) Macro Definition and Invocation; (c) Label Conflict;
(d) Macros and Procedures.
Content: This lecture will cover Macros and label conflict along with different examples. A macro is a
block of text that has been given a name when MASM encounters the name during assembly, it
inserts the block into the program. The text may consist of instructions, pseudo-ops,
comments,
or references to other macros.
Methods: For every new topic, I will first provide the definition and the basic working, then I will
request some student to come to the board and with the help of class discussion repeat that
discussion. This process will not only give them confidence, but will also clear their idea and
encourage them to openly discuss any complications with this topic.
Resources: Besides the lecture handout, this lesson will draw from the following Text books: Assembly
Language Programming and Organization of the IBM-PC, Ytha Yu and Charles Marut.
Evaluation: In this lecture I will not be using any formal evaluation process. I will be using informal
techniques to monitor if the students have absorbed the material or not: (i) By making the
students step up to the board and solve some examples. (iii) By judging the class interaction,
which is a clear indicator of their interest.
Example:
Define a macro to move a word into a word.
Solution:
MOVW WORD1, WORD2
PUSH WORD2
POP WORD1
ENDM
Where a1, a2, … an is a list of actual arguments. When MASM encounters the
macro name, it expands the macro; that is, it copies the macro statements into the
program at the position of invocation, just as if the user had typed them in. as it
copies the statements, MASM replaces each dummy argument di by the
corresponding actual argument ai and creates the machine code for any
instructions.
Example:
Invoke the macro MOVW to move B to A, where A and B are word variables.
Solution:
MOV A, B
To expand this macro, MASM would copy the macro statements into the program at
the position of the call, replacing each occurrence of WORD1 by A, and WORD2
by B. The result is:
PUSH B
POP A
In expanding a macro, the assembler simply substitutes the character strings defining
the actual arguments for the corresponding dummy ones. For example, the
following calls to the MOVW macro:
MOVW A, DX and MOVW A+2, B
Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
Lesson 12 -2- Computer Organization and Assembly Language (CS-530)
Cause the assembler to insert this code into the program:
PUSH DX and PUSH B
POP A POP A+2
20
Mins Label conflict:
A macro with a loop or decision structure contains one or more labels. If such a macro
is Invoked more than once In a program, a duplicate label appears, resulting in an
assembly error: This problem can be avoided by using local labels In the macro. To
declare them, we use the LOCAL pseudo-op, whose syntax is:
LOCAL list_of _ labels
Where list_of_labels is a list of labels, separated by commas. Every time the macro is
expanded, MASM assigns different symbols to the lab('!s in the list.
The LOCAL directive must appear on the next line after the MACRO statement.
Exercise:
10 MACROS
mins .MODEL SMALL
PRINT MACRO A,B
MOV CX, A
MOV DL, B
MOV AH, 2
DISP:
INT 21H
LOOP DISP
ENDM
.STACK 100H
.CODE
MAIN PROC
MOV BX, 10
MOV AH, '*'
PRINT BX, AH
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
DISP:
INT 21H
LOOP DISP
ENDM
.STACK 100H
.CODE
MAIN PROC
PRINT 10, '*'
MAIN ENDP
END MAIN
20
mins LABEL CONFLICT (MACROS)
.MODEL SMALL
MAX MACRO WORD1,WORD2
LOCAL EXIT
MOV AX, WORD1
CMP AX, WORD2
JG EXIT
.STACK 100H
.DATA
MSG DB "OK$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
EXIT:
LEA DX, MSG
MOV AH, 9
INT 21H