Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

H. M.

Faisal (03455275108) Computer Organization and Assembly Language (CS-530)

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.

Assignment: Give them a home assignment on programming practices.


Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
 Macro Definition and Invocation
20
mins
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.

The syntax of macro definition is:


Macro_name MACRO d1, d2, . . . dn
Statements
ENDM

Example:
Define a macro to move a word into a word.
Solution:
MOVW WORD1, WORD2
PUSH WORD2
POP WORD1
ENDM

To use a macro in a program, we invoke it. The syntax is


Macro_name a1, a2, … an

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.

 Macros and Procedures:


1. Procedures are called while Macros are expanded.
2. Procedure call generates a jump statement. (Branch with saving return
address), while there is no jump in Macros.
3. In Macros, program size may increase while Procedure this is not the case.
It is recommended that small codes should be handled by Macro while large code may
by handled by Procedures.

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

Lesson 12 -3- Computer Organization and Assembly Language (CS-530)


Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
MACROS 1
20 .MODEL SMALL
mins PRINT MACRO A,B
MOV CX, A
MOV DL, B
MOV AH, 2

DISP:
INT 21H
LOOP DISP
ENDM
.STACK 100H
.CODE
MAIN PROC
PRINT 10, '*'

MOV AH, 4CH


INT 21H

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

MOV AX, WORD2


EXIT:
ENDM

.STACK 100H
.DATA

MSG DB "OK$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

Lesson 12 -4- Computer Organization and Assembly Language (CS-530)


Time Lecture
Plan Contents of Lecture Strategy
(min) (Remarks)
MOV BX, 10
MOV AH, '*'
MAX 30, 25
JMP EXIT

EXIT:
LEA DX, MSG
MOV AH, 9
INT 21H

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

Lesson 12 -5- Computer Organization and Assembly Language (CS-530)

You might also like