Unit - V Procedure and Macro: (12 - Marks)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

I – Scheme Microprocessors (22415) SEM - IV

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.

Fig. 5.1: - (a) Single Procedure (b) Nested Procedures

Prof. Ratna S. Patil Page No. 1


I – Scheme Microprocessors (22415) SEM - IV

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.

Directives Used for Procedure: -


A) PROC: -
1) The PROC directive is used to identify the start of a procedure.
2) The PROC directive follows the name of the procedure and is followed by
the term NEAR or FAR which specifies the type of the procedure.

Prof. Ratna S. Patil Page No. 2


I – Scheme Microprocessors (22415) SEM - IV

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

Prof. Ratna S. Patil Page No. 3


I – Scheme Microprocessors (22415) SEM - IV

CALL and RET Instructions of 8086: -


1) As shown in fig. 5.1, a CALL instruction in the calling program loads the
instruction pointer and in some cases also the code segment register with
the starting address of the procedure.
2) The next instruction fetched will be the first instruction of the procedure.
At the end of the procedure, a RET instruction sends execution back to the
next instruction after the CALL in the calling program.
3) The RET instruction does this by loading the instruction pointer and if
necessary, the code segment register with the address of the next
instruction after the call instruction.
4) The question arises, if a procedure can be called from anywhere in a
program, how does a RET instruction know, where to return execution to?
5) The answer is, when a CALL instruction executes, it automatically stores
the return address in a special section of memory called the stack.

A) The CALL Instruction: -


1) The 8086 CALL instruction performs two operations when it executes.
2) First, it stores the address of the instruction after the CALL instruction on
the stack. This address is called the return address, because it is the
address that execution will return to after the procedure executes.
3) If the CALL is to a procedure in the same code segment, then the call is
NEAR, and only the instruction pointer (IP) contents will be saved (pushed)
on the stack. Stack pointer is decremented by 2 before push.
SP SP-2
IP Stores on to stack
IP Starting address of a procedure
4) If the CALL is to a procedure in another code segment, the call is FAR. In
this case, both the instruction pointer (IP) and the code segment register
contents will be saved (pushed) on the stack.

Prof. Ratna S. Patil Page No. 4


I – Scheme Microprocessors (22415) SEM - IV

5) Before push, stack pointer is decremented by 2 and copies the contents of


CS register to the stack. Stack pointer is again decremented by 2 and copies
the contents of IP register to the stack.
SP SP-2
CS contents Stored on stack
SP SP-2
IP contents Stores on to stack
6) Finally, it loads the CS register with base address of segment having
procedure and IP with address of first instruction in procedure.
CS Base address of segment having procedure
IP Address of first instruction procedure
7) The second operation of the CALL instruction is to change the contents of
the instruction pointer and in some cases, the contents of the code segment
register to contain the starting address of the procedure.
8) There are four different types of CALL as:
a) Direct within segment NEAR call ; Near Call or
b) Indirect within segment NEAR call ; Intrasegment Call
c) Direct intersegment FAR call ; Far Call or
d) Indirect intersegment FAR call ; Intersegment Call

a) Direct within segment NEAR call: -


1) The first form, direct within segment NEAR call tells the 8086 to produce
the starting address of the procedure by adding a 16 bit signed
displacement contained in the instruction to the contents of the IP.
2) With 16 bit displacement, the starting address of the procedure can be
anywhere in the range of -32768 bytes to +32767 bytes from the address of
the instruction after the CALL.
3) If an assembler is used, it will automatically calculate the displacement
from the instruction after the CALL to a label at the start of the procedure.

Prof. Ratna S. Patil Page No. 5


I – Scheme Microprocessors (22415) SEM - IV

b) Indirect within segment NEAR call: -


1) The indirect within segment CALL instruction is also a NEAR call. When this
form of CALL executes, the instruction pointer (IP) is replaced with a 16 bit
value from a specified register or memory location.
Example: -
CALL BX
IP BX
2) In the above example, IP will be loaded with contents of BX pointer with
the starting address of the procedure obtained from the memory location
pointed by BX.
3) This form of CALL instruction can be used to choose one of several
procedures based on a computed value.
4) The instruction CALL BP, for e.g., will do near call to the offset contained in
BP. In other words, the value in BP will be put in the instruction pointer
(IP).

c) Direct intersegment FAR call: -


1) The direct intersegment FAR call is used when the procedure is in a
segment with a different name from that where the CALL is located.
2) If the procedure is in another segment, both the IP and the CS register have
to be loaded with new values i.e. the starting address of the procedure.
3) For this form of CALL instruction, the new value of IP is written as bytes 2
and 3 of the instruction code. Note that the low byte of the new IP value is
written before the high byte.
4) The new value for the code segment register is written as bytes 4 and 5 of
the instruction code. Again the low byte is written before the high byte.

d) Indirect intersegment FAR call: -


1) This form of the CALL instruction replaces the IP and the CS register
contents with two 16 bit values from memory. Since two 16 bit values are
needed, the values cannot come from a register.

Prof. Ratna S. Patil Page No. 6


I – Scheme Microprocessors (22415) SEM - IV

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.

B) The RET Instruction: -


1) When the 8086 does a NEAR call, it saves the IP value for the instruction
after the CALL on the stack.
2) A RET at the end of the procedure copies this value from the stack back to
the IP to return execution to the calling program.
3) When the 8086 does a FAR call, it saves the contents of both the IP and the
CS register on the stack.
4) A RET instruction at the end of the procedure copies these values from the
stack back into the IP and CS registers to return execution to the calling
program.
5) A procedure can call another procedure and the called procedure can in
turn call another procedure. This is called nesting of procedures.
6) In case of nested procedures, the RET instruction at the end of a lower level
procedure returns execution to the higher level procedure. The RET of that
higher level procedure returns execution to still higher level procedure and
continues till the final highest procedure. The RET at that highest
procedure returns execution to the main (calling) program.

Prof. Ratna S. Patil Page No. 7


I – Scheme Microprocessors (22415) SEM - IV

C) Difference Between NEAR and FAR Procedure (CALL) : -


Sr.
NEAR Procedure (CALL) Far Procedure (CALL)
No.
A near procedure refers to a A far procedure refers to a
procedure which is in the same procedure which is in the
1
code segment from that of the different code segment from
call instruction. that of the call instruction.
A near call replaces the old IP A far call replaces the old CS:IP
2
with new IP. pairs with new CS:IP pairs.
It is also called intra-segment It is also called inter-segment
3
procedure (call). procedure (call).
The value of old IP is pushed on The value of the old CS:IP pair
to the stack. are pushed on to the stack.

4 SP=SP-2; Save IP on stack SP=SP-2; Save CS on stack


(address of procedure). SP=SP-2; Save IP (new offset
address of called procedure).
Less stack locations are More stack locations are
5
required. required.
It uses keyword near for calling It uses keyword far for calling
6
procedure. procedure.
Example: - Call ABC Near. Example: - Call ABC FAR.
7

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.

Prof. Ratna S. Patil Page No. 8


I – Scheme Microprocessors (22415) SEM - IV

Fig. 5.2: - Recursive Procedure

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.

Fig. 5.3: - Reentrant Procedure

Prof. Ratna S. Patil Page No. 9


I – Scheme Microprocessors (22415) SEM - IV

Passing Parameters to and from Procedures: -


1) When we call a procedure, we want to make some data values or addresses
available to the procedure.
2) Likewise, we often want a procedure to make some processed data values
or addresses available to the main program.
3) These data values or addresses passed back and forth between the
mainline and the procedure are commonly called as parameters.
4) The four major ways of passing parameters to and from a procedure are
A) In registers
B) In dedicated memory locations accessed by name
C) With pointers passed in registers
D) With the stack

A) Passing Parameters in Registers: -

1) The main program can pass up to 6 parameters to the procedure through


the registers AX, BX, CX, DX, SI, and DI before executing the call instruction.
2) For e.g.: - Consider the program to calculate a square of given number.

Fig. 5.4: - Passing Parameters in Registers

Prof. Ratna S. Patil Page No. 10


I – Scheme Microprocessors (22415) SEM - IV

B) Passing Parameters in Dedicated Memory Locations Accessed by


Name: -

1) When large number of parameters is to be passed to the procedure, then


these parameters can be placed in an argument list as dedicated memory
locations in one of the data segment from memory.
2) For e.g.: - Consider the program to calculate a square of given number.

Fig. 5.5: - Passing Parameters in Dedicated Memory Locations

C) Passing Parameters with Pointers Passed in Registers: -

1) In the main program, before we call the procedure we can set up SI as a


pointer to pass values to procedures and set DI as pointer to receive values
from procedures.
2) For e.g.: - Consider the program to calculate a square of given number.

Fig. 5.6: - Passing Parameters with Pointers Passed in Registers

Prof. Ratna S. Patil Page No. 11


I – Scheme Microprocessors (22415) SEM - IV

D) Passing Parameters with Stack: -

1) Alternate method of passing large number of parameters is to push the


parameters on the stack in the main program before we call the procedure.
2) For e.g.: - Consider the program to calculate a square of given number.

Fig. 5.7: - Passing Parameters with Stack

Advantages and Disadvantages of Using Procedure: -


A) Advantages: -
1) Use of procedure allows saving of memory space, because machine codes
for the group of instructions in the procedure only have to be put in
memory once.
2) Using procedure, program development becomes easier.
3) Using procedure, debugging of errors in program become easy.
4) Program size is reduced due to use of procedure.
5) Procedures can be reusable.

Prof. Ratna S. Patil Page No. 12


I – Scheme Microprocessors (22415) SEM - IV

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.

Assembly Language Program using Procedure: -


1) Assembly Language Program for Addition of two 8-bit Numbers: -
C) Program: -
DATA SEGMENT
NO1 DB 24H ; Define 1st Operand
NO2 DB 56H ; Define 2nd Operand
RES DB 1 DUP(0) ; Declare array to store result
CRY DB 1 DUP(0) ; Declare array to store carry
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ; Initialize Data Segment
MOV DS, AX ;
CALL ADDITION ; Call to a procedure ADDITION
MOV AH, 4CH ; Terminate the program and
INT 21H ; return to DOS prompt
ADDITION PROC NEAR ; Procedure definition starts here
MOV AL, NO1 ; Load first operand in AL
MOV BL, NO2 ; Load second operand in BL
ADD AL, BL ; Add BL with AL, result is in AL
MOV RES, AL ; Store result in memory in RES

Prof. Ratna S. Patil Page No. 13


I – Scheme Microprocessors (22415) SEM - IV

JNC FINISH ; If no carry, then jump to FINISH


MOV CRY, 01H ; Store 01H (carry) in CRY
FINISH: RET ; Transfer control to main prog.
ADDITION ENDP ; End of ADDITION procedure
CODE ENDS
END START

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

Prof. Ratna S. Patil Page No. 14


I – Scheme Microprocessors (22415) SEM - IV

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.

Calling the Macro: -


1) To call the macro in the program, simply put the name of the macro just as
any other instruction mnemonic.
For example: -
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
MOV AX, NO1
-----------------
PUSH_ALL ; Macro Call
-----------------

Prof. Ratna S. Patil Page No. 15


I – Scheme Microprocessors (22415) SEM - IV

2) When the assembler assembles this program, it will replace PUSH_ALL


(Macro Name) with the instructions that it represents and insert the
machine codes for these machine instructions in the object code version of
program.
3) Thus a macro can be used as simple shorthand for a series of instructions.

Assembly Language Program using Macro: -


1) Assembly Language Program for Addition of two 8-bit Numbers: -
C) Program: -
ADDITION MACRO ; Macro Definition starts here
MOV AL, NO1 ; Load first operand in AL
MOV BL, NO2 ; Load second operand in BL
ADD AL, BL ; Add BL with AL, result is in AL
MOV RES, AL ; Store result in memory in RES
ENDM ; End of Macro Definition
DATA SEGMENT
NO1 DB 24H ; Define 1st Operand
NO2 DB 56H ; Define 2nd Operand
RES DB 1 DUP(0) ; Declare array to store result
CRY DB 1 DUP(0) ; Declare array to store carry
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ; Initialize Data Segment
MOV DS, AX ;
ADDITION ; Call to a Macro ADDITION
JNC FINISH ; If no carry, then jump to FINISH
MOV CRY, 01H ; Store 01H (carry) in CRY
FINISH: MOV AH, 4CH ; Terminate the program and
INT 21H ; return to DOS prompt
CODE ENDS

Prof. Ratna S. Patil Page No. 16


I – Scheme Microprocessors (22415) SEM - IV

END START

Advantages and Disadvantages of Macro: -


A) Advantages: -
1) Program written with macro is more readable.
2) Macro can be called just writing by its name along with parameters; hence
no extra code is required like CALL and RET.
3) Execution time is less because of no linking and returning.
4) Finding errors during debugging is easier.

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.

Difference Between Procedure and Macro: -


Sr.
Procedure Macro
No.
Procedure is a series of Macro is a small sequence of
instructions to be executed code of the same pattern,
several times in a program and repeated frequently at different
1 called whenever required. places, which perform the same
operation on different data of
the same data type.
Program control is transferred The Macro code is inserted into
to the procedure, when CALL the program, whenever MACRO
2 instruction is executed at run is called, by the assembler.
time.
Memory required is less, as the Memory required is more, as
3 program control is transferred the code is inserted at each
to procedure. MACRO call.
Stack is required at Procedure Stack is not required at the
4 CALL. MACRO call.

Prof. Ratna S. Patil Page No. 17


I – Scheme Microprocessors (22415) SEM - IV

Extra overhead time is required No overhead time is required.


5 for linkage between the calling
program and called procedure.

Parameters passed in registers, Parameter passed as the part of


6 memory locations, or stack. statement which calls macro.

RET is required at the end of RET is not used.


7 the procedure.
Procedure is called using: Macro is called using:
8 CALL <Procedure_Name> <Macro_Name> [argument list]
Directives used are PROC, Directives used are MACRO,
9 ENDP, FAR, NEAR. ENDM, LOCAL.
Syntax: - Syntax: -
Procedure_Name PROC Macro_Name MACRO
--------- ---------
--------- ---------
10 Procedure Statements Instructions
--------- ---------
--------- ---------
Procedure_Name ENDP ENDM

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.

3) MSBTE model answer papers from summer 2014 to winter 2018.

Prof. Ratna S. Patil Page No. 18

You might also like