0% found this document useful (0 votes)
81 views

Unit-V Procedure & Macro

Procedures and macros allow repetitive code to be reused. Procedures store code in memory and are called using CALL/RET, while macros define code that is copied each time they are used. The document discusses the differences between procedures and macros, including how parameters are passed, whether stacks are used, and how memory is impacted. Procedures only store code once but have overhead from CALL/RET, while macros avoid this overhead but increase code size.

Uploaded by

Aditya Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Unit-V Procedure & Macro

Procedures and macros allow repetitive code to be reused. Procedures store code in memory and are called using CALL/RET, while macros define code that is copied each time they are used. The document discusses the differences between procedures and macros, including how parameters are passed, whether stacks are used, and how memory is impacted. Procedures only store code once but have overhead from CALL/RET, while macros avoid this overhead but increase code size.

Uploaded by

Aditya Badgujar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT-IV Procedure & Macro Prepared By SHS

UNIT-IV
Procedure & Macro

Contents at a glance:

 Procedures and macros.

Procedures and Macros:

 When we need to use a group of instructions several times throughout a program there are two ways we can avoid
having to write the group of instructions each time we want to use them.
1. One way is to write the group of instructions as a separate procedure.
2. Another way we can use macros.

Procedures:

 The procedure is a group of instructions stored as a separate program in the memory and it is called from the
main program whenever required using CALL instruction.
 For calling the procedure we have to store the return address (next instruction address followed by CALL) onto
the stack.
 At the end of the procedure RET instruction used to return the execution to the next instruction in the main
program by retrieving the address from the top of the stack.

 Machine codes for the procedure instructions put only once in memory.
 The procedure can be defined anywhere in the program using assembly directives PROC and ENDP.

 The four major ways of passing parameters to and from a procedure are:
1. In registers
2. In dedicated memory location accessed by name
3 .With pointers passed in registers
4. With the stack
 The type of procedure depends on where the procedure is stored in the memory.
 If it is in the same code segment where the main program is stored the it is called near procedure otherwise it is
referred to as far procedure.
 For near procedure CALL instruction pushes only the IP register contents on the stack, since CS register contents
remains unchanged for main program.
 But for Far procedure CALL instruction pushes both IP and CS on the stack.

MICROPROCESSORS-22415 Page 1
UNIT-IV Procedure & Macro Prepared By SHS

Syntax:

Procedure name PROC near


---
---
RET
Procedure name ENDP
Example:
near procedure:
ADD2 PROC near
ADD AX,BX
RET
ADD2 ENDP

MICROPROCESSORS-22415 Page 2
UNIT-IV Procedure & Macro Prepared By SHS

 Depending on the characteristics the procedures are two types


1. Re-entrant Procedures
2. Recursive Procedures
Reentrant Procedures
 The procedure which can be interrupted, used and “reentered” without losing or writing over anything.

Recursive Procedure

 A recursive procedure is procedure which calls itself.



MICROPROCESSORS-22415 Page 3
UNIT-IV Procedure & Macro Prepared By SHS

ALP for Finding Factorial of number using procedures


CODE SEGMENT
ASSUME
CS:CODE
START:
MOV AX,Data
Mov ds,AX
CALL FACT
MOV AH,4CH
INT 21H

FACT PROC NEAR


MOV BX,AX
DEC BX
BACK:
MUL BX
DEC BX
JNZ BACK
RET
ENDP
CODE ENDS
END START

MICROPROCESSORS-22415 Page 4
UNIT-IV Procedure & Macro Prepared By SHS

Macros:
 A macro is a group of repetitive instructions in a program which are codified only once and can be used as many
times as necessary.
 A macro can be defined anywhere in program using the directives MACRO and ENDM
 Each time we call the macro in a program, the assembler will insert the defined group of instructions in place of the
call.
 The assembler generates machine codes for the group of instructions each time the macro is called.
 Using a macro avoids the overhead time involved in calling and returning from a procedure.
Syntax of macro:
macroname MACRO Arg

ENDM

 Example:

Read MACRO Display MACRO


mov ah,01h mov dl,al
int 21h Mov ah,02h
ENDM int 21h
ENDM

MICROPROCESSORS-22415 Page 5
UNIT-IV Procedure & Macro Prepared By SHS

ALP for Finding Factorial of number using procedures


CODE SEGMENT
ASSUME CS:CODE
FACT MACRO
MOV BX,AX
DEC BX
BACK: MUL BX
DEC BX
JNZ BACK
ENDM
START: MOV AX,7
FACT
MOV AH,4CH
INT 21H
CODE ENDS
END START

Advantage of Procedure and Macros:


Procedures:
Advantages
⚫ The machine codes for the group of instructions in the procedure only have to be put once.

Disadvantages
⚫ Need for stack
⚫ Overhead time required to call the procedure and return to the calling program.
Macros:
Advantages
⚫ Macro avoids overhead time involving in calling and returning from a procedure.
Disadvantages
⚫ Generating in line code each time a macro is called is that this will make the program take up more memory
than using a procedure.

MICROPROCESSORS-22415 Page 6
UNIT-IV Procedure & Macro Prepared By SHS

Differences between Procedures and Macros:


PROCEDURES MACROS

Accessed by CALL and RET mechanism during program Accessed by name given to macro when defined during
execution assembly

Machine code generated for instructions each time


Machine code for instructions only put in memory once
called

Parameters are passed in registers, memory locations Parameters passed as part of statement which calls
or stack macro

Procedures uses stack Macro does not utilize stack

A procedure can be defined anywhere in program using A macro can be defined anywhere in program using the
the directives PROC and ENDP directives MACRO and ENDM

Procedures takes huge memory for CALL (3 bytes each Length of code is very huge if macro’s are called for more
time CALL is used) instruction number of times

MICROPROCESSORS-22415 Page 7

You might also like