0% found this document useful (0 votes)
57 views5 pages

Assignment No.1

1. A procedure is a reusable block of code that can be called multiple times from different parts of a program to perform a specific task. This avoids writing duplicate code. 2. Macros are similar to procedures but instead of calling the code, the assembler directly replaces the macro with the block of code, improving efficiency but increasing code size. 3. The document provides examples of programs using procedures and macros to demonstrate their implementation and advantages/disadvantages.

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)
57 views5 pages

Assignment No.1

1. A procedure is a reusable block of code that can be called multiple times from different parts of a program to perform a specific task. This avoids writing duplicate code. 2. Macros are similar to procedures but instead of calling the code, the assembler directly replaces the macro with the block of code, improving efficiency but increasing code size. 3. The document provides examples of programs using procedures and macros to demonstrate their implementation and advantages/disadvantages.

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/ 5

Microprocessor (EE-502)

PROCEDURE & MACROS


Lab Assignment

Submitted By: Sajjad Hussain


( 2018-EE-37 )
Submitted By: Engr. Zonain

Department of Electrical Engineering


BZU Multan
Procedure:
A procedure is a reusable section of the software that is stored in memory once but used
as often as necessary. While writing programs, it may be the case that a particular sequence of
instructions is used several times. To avoid writing the sequence of instructions again and again
in the program, the same sequence can be written as a separate subprogram called a procedure.
Each time the sequence of instructions needs to be executed; CALL instruction can be used.
CALL instruction transfers the execution control to procedure. In the procedure, a RET
instruction is used at the end. This will cause the execution to be transferred to caller program.
Often large programs are split into number of independent tasks which can be easily designed
and implemented. This is known as modular programming. The procedure (subroutine,
method, or function) is an important part of any computer system’s architecture. A procedure
is a group of instructions that usually performs one task. This saves memory space and makes
it easier to develop software. The only disadvantage of a procedure is that it takes the computer
a small amount of time to link to the procedure and return from it. The CALL instruction links
to the procedure, and the RET (return) instruction returns from the procedure. With the
assembler, there are specific rules for storing procedures. A procedure begins with the PROC
directive and ends with the ENDP directive. Each directive appears with the name of the
procedure. This programming structure makes it easy to locate the procedure in a program
listing.
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.

Macros:
Whenever it is required to use a group of instructions several times in a program, there
are two ways to use that group of instructions: One way is to write the group of instructions as
a separate procedure. We can call the procedure whenever it is required to execute that group
of instructions. But disadvantage of using a procedure is we need stack. Another disadvantage
is that time is required to call procedures and return to calling program. When the repeated
group of instruction is too short or not suitable to be implemented as a procedure, we use a
MACRO. A macro is a group of instructions to which a name is given. Each time a macro is
called in a program, the assembler will replace the macro name with the group of instructions.
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:

Disadvantage of macro is that the memory requirement of a program becomes more.

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