Professional Documents
Culture Documents
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.
(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.
PUSH AX
PUSH BX
PUSH CX
PUSH DX
@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.
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.
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:
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
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.
Second example:
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
@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
;@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:
The purpose of Macro @write is that any sentence assigned to the main
program mactest1.asm will display these particular sentence.
Statement
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.