9 - Procedures and Macros
9 - Procedures and Macros
Disadvantages:
1.Extra code may be required to integrate procedures.
2.Liking of procedures may be required.
3.Processor needs to do extra work to save status of current procedure and load status of
called procedure. The queue must be emptied so that instructions of the procedure can be filled in
the queue.
The 8086 Stack
• Section of memory you set
aside for storing return
addresses.
• Also used to store the
contents of the registers for
the calling program while a
procedure executes.
• Hold data or address that will
be acted upon by
procedures.
Using PUSH and POP
• Disadvantage
• assembler copies the macro code into the program at each macro invocation
• if the number of macro invocations within the program is large then the
program will be much larger than when using procedures
Macros and Procedures
1. When you want to use a procedure you should
use CALL instruction, for example:
CALL MyProc
When you want to use a macro, you can just type its name. For
example:
MyMacro
2. You should use stack or any general purpose registers to pass
parameters to procedure.
To pass parameters to macro, you can just type them after the macro
name. For example: MyMacro 1, 2, 3
20
Macros and Procedures
3. Macros end with ENDM directive and procedures end with ENDP
directive.
4. Procedures are located at some specific address in memory but
macros are expanded directly in program's code.
21
Labels in Macros
• Macros are expanded directly in code, therefore if there are labels
inside the macro definition you may get "Duplicate declaration" error
when macro is used for twice or more.
• To avoid such problem, use LOCAL directive followed by names of
variables, labels or procedure names.
22
Example
MyMacro2 MACRO
LOCAL label1, label2, stop
CMP AX, 2
JE label1
CMP AX, 3
JE label2
label1:
INC AX
JMP STOP
label2:
ADD AX, 2
Stop:
ENDM
ORG 100h
Mov Ax, 2
MyMacro2
MyMacro2
RET
23