Procedure and Macros Notes
Procedure and Macros Notes
Procedure and Macros Notes
Define procedure : A procedure is group of instructions that usually performs one task. It is a reusable
section of a software program which is stored in memory once but can be used as often as necessary.
A procedure can be of two types.
1) Near Procedure 2) Far Procedure
Near Procedure: A procedure is known as NEAR procedure if is written(defined) in the same code
segment which is calling that procedure. Only Instruction Pointer(IP register) contents will be changed in
NEAR procedure.
FAR procedure : A procedure is known as FAR procedure if it is written (defined) in the different code
segment than the calling segment. In this case both Instruction Pointer(IP) and the Code Segment(CS)
register content will be changed.
PROC directive: The PROC directive is used to identify the start of a procedure. The PROC directive
follows a name given to the procedure.After that the term FAR and NEAR is used to specify the type of
the procedure.
ENDP Directive: This directive is used along with the name of the procedure to indicate the end of a
procedure to the assembler. The PROC and ENDP directive are used to bracket a procedure.
CALL instruction : The CALL instruction is used to transfer execution to a procedure.It performs two
operation.When it executes,first it stores the address of instruction after the CALL instruction on the
stack.Second it changes the content of IP register in case of Near call and changes the content of IP
register and cs register in case of FAR call.
Operation of FAR CALL: When 8086 executes a far call, it decrements the stack pointer by 2 and copies
the contents of CS register to the stack. It the decrements the stack pointer by 2 again and copies the
content of IP register to the stack.Finally it loads cs register with base address of segment having
procedure and IP with address of first instruction in procedure.
SP sp-2
cs contents stored on stack
SP sp-2
IP contents stored on stack
CS Base address of segment having procedure
IP address of first instruction in procedure.
RET instruction : The RET instruction will return execution from a procedure to the next instruction
after call in the main program. At the end of every procedure RET instruction must be executed.
Operation for Near Procedure : For NEAR procedure ,the return is done by replacing the IP
register with a address popped off from stack and then SP will be incremented by 2.
1) In register
2) In dedicated memory locations accessed by name
3) With pointer passed in register.
4) With the stack
1) Passing parameters in registers :The main program can pass upto 6 parameters to the
procedure through the registers AX,BX,CX,DX,SI & DI before executing the call instruction.
e.g. consider the program to calculate a square of given number.
Main Program
---- Data Segment
Mov bl,num Data segment
Call square Num db 05h
Mov sqr,bl Sqr db ?
---- Data ends
----
square procedure
square PROC NEAR Registers
Mov bl,[SI]
al,bl,cl,dl
Mov al,bl
,si,di
Mul bl ah,bh,ch,dh
Mov [DI],al
Ret
Square endp
4) Passing parameters with stack : 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.
e.g. consider the program to calculate a square of given number.
Main Program
---- Data Segment
Mov bl,num Data segment
PUSH BX Num db 05h
Call square Sqr db ?
POP BX Data ends
Mov sqr,bl
----
----
Disadvantages :
1) CALL and RET instructions are always required to integrate with procedures.
2) Requires the extra time to link procedure and return from it.
3) For small group of instructions, linking and returning back time more than the execution time,
hence for small group of instructions procedures cannot be preffered.
Defining Macro : A MACRO is group of small instructions that usually performs one task. It is a
reusable section of a software program.A macro can be defined anywhere in a program using
directive MACRO &ENDM.
General Form :
Disadvantages :
1) object code is generated every time a macro is called hence object file becomes lengthy.
2) For large group of instructions macro cannot be preferred
Procedure Macro
Procedures are used for large group of instructions to Procedures are used for small group of instructions to
be repeated. be repeated.
Object code is generated only once in memory. Object code is generated everytime the macro is
called.
CALL & RET instructions are used to call procedure and Macro can be called just by writing its name.
return from procedure.
Length of the object file is less Object file becomes lengthy.
Directives PROC & ENDP are used for defining Directives MACRO and ENDM are used for defining
procedure. MACRO
More time is required for it’s execution Less time is required for it’s execution
Procedure can be defined as Macro can be defined as
Procedure_name PROC MACRO-name MACRO [ARGUMENT ,………. ARGUMENT N]
---- ------
------ -------
Procedure_name ENDP ENDM
For Example For Example
Addition PROC near Display MACRO msg
------ ---------
Addition ENDP ENDM