You are on page 1of 6

Topic Macro

20 Usages

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. List down and explain two types of macro components; and
2. Write macro codes.

INTRODUCTION
The previous topic explained the techniques of writing codes in assembly
language. In this topic, we will discuss one more technique that is writing codes
with macro.

20.1 MACRO CONCEPTS


Macro is name given to the group of one or more statements in assembly
language. When a macro is created, a programmer is able to use the particular
macro repeatedly.

20.1.1 Is Macro the Same as Procedure?


Macro is not the same as subroutines, procedure or functions. The execution of
procedures involves changes in the program counter and after execution, the
program counter will return to the original value. A Macro call causes the
assembler to enter macro codes into a program.
TOPIC 20 MACRO USAGES 173

20.1.2 Macro Components


Macros can be divided into two important divisions, such as:

(a) Declaration:
Macro declaration can be put at the beginning of codes in the assembly
language or in a separate storage known as macro library.

Macros for assembly language are stored in a different file from the main
program. The calling of these different file is very important when the
programmer wants use the functions in the macros.

(b) Implementation:
When a macro external file consists of the particular macro name is called
in the main program, all existing macro names will be recognised by the
assembler. Once the assembler has identified the macro names, it will
directly refer to the particular macro declaration.

20.2 HOW TO WRITE MACROS


Lets say, there are the four following instructions in an assembly language
program which need to be used frequently:

PUSH AX
PUSH BX
PUSH CX
PUSH DX

We can write these four instructions in macro form as below:

@macroname MACRO
PUSH AX
PUSH BX
PUSH CX
PUSH DX
ENDM
174 TOPIC 20 MACRO USAGES

Explanation:

(a) @macroname
Is used to name and declare a macro. The name that can be used for a
macro can be used for a label. To differentiate a macro from a label, it is
better if the programmer adds the @ sign in front of each macro.

(b) MACRO.. ENDM:


Is used to indicate the beginning and ending of a macro.

A macro is usually created separately from the main assembly language


program. When the programmer wants to use the particular macro, the macro
call must be in the correct format as declared by the particular macro.

If the programmer calls the macros @macroname thus, all four instructions in a
particular macro will automatically be referred directly by source program.

Each macro has its own name. A macro can be called by the source program
repeatedly as and when required by programmer.

Thus, it is better to create a macro with a suitable name based on the functions
that will be created. A macro file can be stored with the MAC file or any
extension file. However, the way of the macro call must be suitable with
particular macro.

20.3 EXAMPLES OF MACROS

ACTIVITY 20.1
In your opinion, why are macros separated from the assembly
language program?

Two macros are created and given name as @macrodata and @macroend in
this section.

First Example:

The main program is stored as conmacro.asm:


TOPIC 20 MACRO USAGES 175

page ,132
title callingmacro

if1
include callmac.txt endif

data segment
message db Welcome $
data ends

code segment
assume cs: code,ds: data start:
@macrodata
@macroend

code

end start

Explanation about the program above:

if1
include callmac.txt endif

This block is to inform the assembler about the library that will be used in the
first level assembly. The Assembler requires this macro definition in the first
level assembly.

The other lines in the program are same as other examples of EXE files shown
before. However, the macro call is performed based on the macro name. The
usage of the @ symbol in front of macro is just for the programmer to identify
that @macrodata is a macro.

The book Assembly Language Techniques IBM-PC provides a detail explanation on


macro usage in assembly language program.
176 TOPIC 20 MACRO USAGES

Second example:

The main program file ( mactest1.asm )

page ,132
title macro testing

comment*
write welcome on the screen
program is executed as com file

*
if1
include macro1. mac endif

code segment
assume cs:code,ds:code org 100h
start:
@write Welcome
@goto_dos code ends
end start

The macro program stored in file is named callmac.txt:

@macrodata macro
mov ax,data mov ds,ax mov ah,9 endm
@macroends macro
mov dx,offset message int 21h
mov ah,4ch int 21h endm

ACTIVITY 20.3
In your opinion, what is the difference between a program that has
macro instructions and one without macro instructions?
TOPIC 20 MACRO USAGES 177

The Macro File(makro1.mac)

;@goto_dos MACRO
;@write MACRO text?
@goto_dos
int 20h endm
@write MACRO text?
local out,message push ax
push dx mov ah,9
mov dx,offset message
int 21h pop dx pop ax
jmp short out
message db text?$
out:
endm

Explanation:

Macro @goto_dos refers to the program to go to dos.

The purpose of Macro @write is that any sentence assigned to the main
program mactest1.asm will display these particular sentence.

Statement

jmp short out

when a program completes an execution, the execution of the COM file will end.
Take note that jmp short is only suitable for jumps in the range of 128 until +127
byte from the current location program.

In short, Macros involve changes in the program counter value. You should
be able to understand the differences between macros and procedures,
functions or subroutines once completing this topic.
After writing a program, errors or mistakes are inevitable as varieties of
possibilities exist, some which might have not thought of by the
programmer. Thus, the following topic will focus on discussions on how to
handle this issue.

You might also like