Procedure & Macros: Microprocessor (EE-502) Lab Assignment
Procedure & Macros: Microprocessor (EE-502) Lab Assignment
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.
Macro:
A macro is a group of instructions we bracket and give a name to at the
start of our program. Each time we call the macro in our program, the assembler
inserts the defined group of instructions in place of the call.
The MACRO directive is used to identify the start of a macro. The Macro
directive follows a name you give the macro.
MACRONAME MACRO ; START OF MACRO
--------
--------
--------
ENDM ; END OF MACRO
Macro definition:
Advantages:
1. Macro reduces the amount of repetitive coding.
2. Program becomes more readable and simpler.
3. Execution time is less as compared to calling procedures.
4. Reduces errors caused by repetitive coding.
Disadvantage:
Execution Process Every time a procedure is Every time a Macro is called, the
called, the CALL and RET assembler of the microprocessor
instructions are required for places the entire set of
instructions of the Macros in the
shifting the control of mainline program form where
instruction execution. the call to the macro is made.
Execution Time The Procedures execute slower The execution of macros is faster
than the Macros because every as compared to procedures
time a procedure is called, it is because there is no need to
necessary to integrate and link integrate or link the macros with
it with the calling program and the calling program. It is simply
this takes time. loaded into the main memory
every time it is called.
Number of times The machine code (containing The machine code (containing
machine code the instructions within the the instructions within the
Procedure) is generated only Macros) is generated every time
generated
once when the procedure is the macro is called.
defined.
Examples:
Program using procedures:
ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
DATA SEGMENT
NUM1 DB 50H
NUM2 DB 20H
ADD_RES DB ?
SUB_RES DB ?
DATA ENDS
STACK_SEG SEGMENT
DW 40 DUP(0) ; stack of 40 words, all initialized to zero
TOS LABEL WORD
STACK_SEG ENDS
CODE SEGMENT
START: MOV AX, DATA ; initialize data segment
MOV DS, AX
MOV AX, STACK_SEG ; initialize stack segment.
MOV SS, AX
MOV SP, OFFSET TOS ; initialize stack pointer to TOS.
CALL ADDITION
CALL SUBTRACTION
MOV AH, 4CH
INT 21H
ADDITION PROC NEAR
MOV AL, NUM1
MOV BL, NUM2
ADD AL, BL
MOV ADD_RES, AL
RET
ADDITION ENDP
SUBTRACTION PROC
MOV AL, NUM1
MOV BL, NUM2
SUB AL, BL
MOV SUB_RES, AL
RET
SUBTRACTION ENDP
CODE ENDS
END START
Program using Macro:
DISPLAY MACRO MESSAGE
PUSH AX
PUSH DX
MOV AH, 09H
LEA DX, MESSAGE
INT 21H
POP DX
POP AX
ENDM
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
MSG1 DB ‘Microprocessor and programming$’
MSG2 DB 10,13,‘Using macros$’
MSG3 DB 10,13,‘It eliminates repetitive coding$’
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA ; initialize data segment
MOV DS, AX
DISPLAY MSG1
DISPLAY MSG2
DISPLAY MSG3
MOV AH, 4CH
INT 21H
CODE ENDS
END START