Unit - V Procedure and Macro: (12 - Marks)
Unit - V Procedure and Macro: (12 - Marks)
Unit - V Procedure and Macro: (12 - Marks)
UNIT – V
PROCEDURE AND MACRO
(12 – Marks)
Defining Procedure: -
1) When we write a program, we will find that we need to use a particular
sequence of instructions at several different points in a program.
2) To avoid writing the sequence of instructions in the program each time we
need them, we can write the sequence as a separate “subprogram” called a
“procedure”.
3) Each time we need to execute the sequence of instructions contained in the
procedure, we use the CALL instruction to send the 8086 to the starting
address of the procedure in memory.
4) Following figure shows a diagrammatic form how a CALL instruction
causes execution to go from the calling (mainline) program to a procedure.
5) A RET instruction at the end of the procedure returns execution to the next
instruction in the calling program.
6) The procedures can even be “nested”, this means that one procedure calls
another procedure as part of its instruction sequence.
Calling Procedure: -
1) A procedure can be called from the calling (mainline) program using CALL
and RET instructions.
2) The CALL instruction transfers the control of execution from the calling
program to the called program known as procedure, from where the CPU
fetches and executes the next instruction.
3) At the end of the procedure, a RET instruction is used to transfer the
control back to the calling program at the instruction after the CALL
instruction.
4) A procedure can be of two types:
A) Near Procedure
B) Far procedure
A) Near Procedure: -
1) A procedure is known as NEAR procedure if it is written (defined) in the
same code segment which is calling that procedure.
2) Only Instruction Pointer (IP register) contents will be changed in NEAR
procedure.
B) Far Procedure: -
1) A procedure is known as FAR procedure if it is written (defined) in the
different code segment than the calling segment.
2) In this case both Instruction Pointer (IP) and the Code Segment (CS)
register content will be changed.
3) The term NEAR indicates a procedure which resides in the same segment
as the calling segment.
4) The term FAR indicates that the procedure resides in a code segment other
than the calling segment.
5) The general form of the PROC directive is:
Syntax: - Procedure_Name PROC [NEAR/FAR]
6) The term NEAR and FAR is optional. If any of the term not used, then
assembler assumes that the procedure is a NEAR procedure.
B) ENDP: -
1) The ENDP directive is used along with the name of the procedure to
indicate the end of a procedure to the assembler.
2) The PROC and ENDP directive are used to bracket a procedure. Thus, the
PROC and ENDP directive informs the assembler about the start and end of
a procedure.
3) The general form of the ENDP directive is:
Syntax: - Procedure_Name ENDP
Examples: -
1. ABC PROC ; Start of ABC Procedure
-------------- ; Procedure Instructions
-------------- ; Procedure Instructions
ABC ENDP ; End of ABC Procedure
2. ABC PROC NEAR ; Start of ABC Procedure
-------------- ; Which resides in the Same
-------------- ; Segment as the calling segment
ABC ENDP ; End of ABC Procedure
3. ABC PROC FAR ; Start of ABC Procedure
-------------- ; Which resides in the Different
-------------- ; Segment than the calling segment
ABC ENDP ; End of ABC Procedure
2) This CALL loads the IP and the CS register with the starting address of the
procedure obtained from four memory locations specified in the CALL
instruction.
Example: -
CALL DWORD_PTR [BX]
3) In the above example, the CALL instruction will load IP with the contents of
[BX] and [BX+1] locations, and CS with the contents of [BX+2] and [BX+3]
locations.
Recursive Procedure: -
1) Recursive procedure is a procedure which calls itself.
2) Recursive procedures are used to work with complex data structure like
trees.
3) If procedure is called with N (recursive depth) then N is decremented by
one after each procedure CALL and the procedure is called until n=0.
4) Recursive procedure takes less time to implement a particular task. But it
needs certain condition for its termination.
Reentrant Procedure: -
1) In some situation, it may happen that procedure 1 is called from main
program and procedure 2 is called from procedure 1, and again procedure
1 is called from procedure 2.
2) In this situation, program execution flow re-enters in the procedure 1 (first
time when procedure 1 was called from main program and second time
when procedure 1 was called from procedure 2). Hence this type of
procedure is called as Reentrant procedure.
B) Disadvantages: -
1) CALL and RET instructions are always required to integrate with
procedures.
2) For procedure, one needs to use a stack to save the return addresses and
values of the program.
3) Requires extra time to link procedure and return from it.
4) For small group of instructions, linking and returning back time more than
the execution time, hence for small group of instructions, procedures
cannot be preferred.
Defining Macros: -
1) Macros can be used to name a group of instructions being used several
times in a program.
2) Macros are defined at the start of a program. Each time the macro name
appears in a program, the assembler replaces it with the group of
instructions defined as that macro at the start of the program.
3) Replacing a macro name is also known as macro expansion. Macro can be
called as open subroutine.
4) Macro can be called by using MACRO and ENDM assembler directives
respectively.
5) The MACRO assembler directive indicates the start of a Macro. The MACRO
directive followed by the name of MACRO.
For e.g.: - Addition MACRO
6) The ENDM assembler directive indicates the end of a MACRO. The ENDM
directive is used with the MACRO directive to bracket a macro definition.
For e.g.: - ENDM
7) The general form of defining a macro is as follows: -
Syntax: -
MACRO_NAME MACRO ; Start of Macro
------------------------------- ; Instructions
------------------------------- ; Instructions
ENDM ; End of Macro
8) Suppose, a group of instructions to be used frequently is,
PUSHF
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH BP
PUSH SI
PUSH DI
9) Then, to define the instructions as a macro, write the above code as,
PUSH_ALL MACRO
PUSHF
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH BP
PUSH SI
PUSH DI
ENDM
10) The PUSH_ALL MACRO statement identifies the start of the macro and gives
the macro a name. The ENDM identifies the end of the macro.
END START
B) Disadvantages: -
1) Object code generated every time a macro is called hence object file
becomes lengthy.
2) For large group of instructions macro cannot be preferred.
References: The contents and diagrams for notes preparation are taken from
1) Douglas V. Hall, Microprocessors and Interfacing, Second Edition, Tata McGraw Hill publications.
2) A. K. Ray, K. M. Bhurchandi, Advanced Microprocessors and Peripherals, Second Edition, Tata McGraw
Hill Publications.