0% found this document useful (0 votes)
85 views8 pages

Procedure & Macros: Microprocessor (EE-502) Lab Assignment

The document discusses procedures and macros in assembly language. Procedures are used to group instructions together and call them using PROC and ENDP directives. They allow for modularity and code reuse but require overhead for call/return. Macros are similar but the assembler directly copies the macro code where it is called rather than linking. This makes macros faster but uses more memory. When to use procedures versus macros depends on the number of instructions and whether parameters are needed.

Uploaded by

Sajjad Hussain
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)
85 views8 pages

Procedure & Macros: Microprocessor (EE-502) Lab Assignment

The document discusses procedures and macros in assembly language. Procedures are used to group instructions together and call them using PROC and ENDP directives. They allow for modularity and code reuse but require overhead for call/return. Macros are similar but the assembler directly copies the macro code where it is called rather than linking. This makes macros faster but uses more memory. When to use procedures versus macros depends on the number of instructions and whether parameters are needed.

Uploaded by

Sajjad Hussain
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/ 8

PROCEDURE & MACROS

Microprocessor (EE-502) Lab Assignment

Submitted By: Ghulam Yaseen (2018-EE-35)


Procedure:
The PROC directive is used to identify the start of a procedure. The
PROC directive follows a name you give the procedure. After the PROC
directive, the term near or the term far is used to specify the type of the procedure.
Example:
DIVIDE PROC FAR
This identifies the start of a procedure named DIVIDE and
tells the assembler that the procedure is far (in a segment with different name
from the one that contains the instructions which calls the procedure). The PROC
directive is used with the ENDP directive to “bracket” a procedure.
NEAR: the procedure resides in the same code segment. (Local)
FAR: resides at any location in the memory.
Example
Add PROC NEAR
ADD AX,BX
MOV CX,AX
RET
Add ENDP
PROC directive stores the contents of the register in the stack .
ENDP (END PROCEDURE)
The directive is used along with the name of the
procedure to indicate the end of a procedure to the assembler. The directive,
together with the procedure directive, PROC, is used to “bracket” a procedure.
Example:
SQUARE_ROOT PROC; Start of procedure.
SQUARE_ROOT ENDP; End of procedure.
Advantages:
1. Programming becomes simple.
2. Reduced development time – as each module can be implemented by
different persons.
3. Debugging of smaller programs and procedures is easy.
4. Reuse of procedures is possible.
5. A library of procedures can be created to use and distribute.

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

ENDM :-[end macro ]


This directive is used along with the name of the macro to indicate the end
of a macro to the assembler.

Macro definition:

name MACRO [parameters,…]


<instructions >
ENDM

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:

1.Disadvantage of macro is that the memory requirement of a program


becomes more.
2. Linkage associated with them.
3. It sometimes requires more code to program the linkage than is needed
to perform the task. If this is the case, a procedure may not save memory and
execution time is considerably increase.

Differences between Procedures and Macros


Characteristic Procedure Macro
Number of It is better to use Procedures Macros are useful over
Instructions that can for a set of many instructions. Procedures when the number of
be effectively Hence, it is optimal to use instructions in the set is less.
handled by the Procedures when the number Therefore, when the subprogram
microprocessor of instructions is more than 10. contains less than 10
instructions, Macros are more
efficient to use in such cases.

Assembler The assembler directive - PROC The assembler directive- MACRO


Directives used is used to define a Procedure. is used to define a Macro, And
And the assembler directive - to indicate that the body of the
ENDP is used to indicate that procedure has ended, the
the body of the procedure has assembler directive- ENDM is
ended. used.

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.

Overhead time Overhead time occurs while Overhead time is avoided as


calling the procedure and calling and returning does not
returning the control to the take place.
calling program.

Amount of The Procedures require less The Macros require a large


memory amount of memory than the amount of memory because it is
Macros because a Procedure is loaded into the main memory
required
written and loaded into the every time it is called.
main memory only once and is
linked to the calling program
when 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.

Passing of In procedures, we cannot pass The macros are capable of


parameters the parameter to id directly. handling parameters within their
However, the values can be definition and we can pass them
passed to it using registers and in the statement which calls the
via stack. macro.

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

You might also like