0% found this document useful (0 votes)
43 views29 pages

CH 5 Procedures and Macros

The document discusses procedures and macros in assembly language. It defines procedures as groups of instructions stored separately that can be called, while macros insert code directly during assembly. Parameters can be passed to procedures using registers, memory, pointers or the stack. Controlled expansion allows conditional assembly of macro code.

Uploaded by

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

CH 5 Procedures and Macros

The document discusses procedures and macros in assembly language. It defines procedures as groups of instructions stored separately that can be called, while macros insert code directly during assembly. Parameters can be passed to procedures using registers, memory, pointers or the stack. Controlled expansion allows conditional assembly of macro code.

Uploaded by

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

CHAPTER 5 : PROCEDURE AND MACRO

Topics Covered:
1. Introduction
2. Procedure
3. Macros

Total Marks – Min 12 marks.


TOPIC 1 : INTRODUCTION.
• Whenever 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.
• One way is to write the group of instructions in a separate procedure.
• We can just CALL the procedure whenever we need to execute the group of instructions.
• For calling the procedure we have to store the return address onto the stack.
• This procedure takes some time.
• If the group of instruction is big enough then the overhead time is negligible with respect to the execution time.
• But if the group of instruction is to short, the overhead time and execution time are comparable.
• In such case, it is not desirable to write procedure.
• For these cases, we can use macros.
• Macro is also a group of instructions.
• Each time a Macro is CALL , the assembler will insert the defined group of instruction in place of the “CALL”.
TOPIC 1 : INTRODUCTION.
• An important point here is that the assembler generates machine codes for the group of instructions each
time a Macro is called.
• So there is not overhead time involved in calling and returning from a procedure.
• The disadvantage of Macro is that it generates in-line code each time when the Macro is called which
takes more memory.
TOPIC 2 : PROCEDURE.
• We can define a Procedure as a group of instructions stored as a separate program in the memory and it is
called from the main program whenever required.
• 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 then 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 the CS
register contents remains unchanged for main program and procedure.
• But for the far procedure CALL instruction pushes both IP and CS on the stack.
TOPIC 2 : PROCEDURE.
.model small
.code
---I1 CS: address Instructions
---I2 0000 I1
CALL PRO1 0001 I2
---I3 0002 0005
---I4
0003 I3
PRO1 PROC NEAR
0004 I4
---I5
0005 I5
---I6
0006 I6
RET
ENDP 0007 RET
End
ends
TOPIC 2 : PROCEDURE.
Program 1 Program 2
CS: address Instructions CS: address Instructions
.model small .model small 0000 I1 0000 P1
.code .code 0001 I2 0001 P2
---I1 ----P1 0002 CS:0002 0002 P3
---I2 ----P2 0003 I3 0003 P4
CALL PRO1 PRO1 PROC FAR 0004 I4 0004 RET
---I3 ---P3 0005 0005
---I4 ---P4 0006 0006
End RET 0007 0007
Ends ENDP
end
ends
: PROCEDURE: REENTRANT
TOPIC 2
PROCEDURE
• In some situations it may happen that procedure 1 is called from main program, procedure 2 is called from
procedure 1 and procedure 1 is again called from procedure 2.
• In this situation program execution flow reenters in the procedure 1.
• This type of procedure are called reentrant procedures.
• The flow of program execution for reentrant procedure is as:
: PROCEDURE: RECURSIVE
TOPIC 2
PROCEDURE
• A recursive procedure is a procedure which calls itself.
• Recursive procedures are used to work with complex data
structures called trees.
• If the procedures is called with N=3 , then the N is
decremented by 1 after each procedure CALL and the
procedure is called until N=0.
• When a procedure is called within another procedure it called
recursive procedure. To make sure that the procedure does not
modify itself, each call must store its set of parameters, registers, and
all temporary results in a different place in memory

• Figure shows the flow diagram and pseudo-code for


recursive procedures.
: PROCEDURE: PASSING
TOPIC 2
PARAMETERS
• we often want a procedure to process some data or address variables from the main program.
• For processing, it is necessary to pass these address variable or data, usually referred as passing
parameters to the procedure.
• There are four ways to pass parameters to and from the procedure.
1. Using registers
2. Using general memory
3. Using pointers
4. Using stack
TOPIC 2 : PROCEDURE: PASSING
PARAMETERS
1. Using registers :
• The data, to be passed is stored in the register and these registers are accessed in the procedure to process
data.
• Example :
.data
Num1 db 11h
Num2 db 22h
Res db ?
.code
Mov al,num1 // data to be passed is loaded in the AL register
Mov bl,num2
---
- --
CALL PRO1
---
---
PRO1 PROC NEAR
Add al,bl // Procedure PRO1 access the data from al register
---
---
RET
PRO1 ENDP
End
ends
TOPIC 2 : PROCEDURE: PASSING
PARAMETERS
2. Using memory : For the cases where we have to pass few parameters to and from a procedure, registers are a
convenient way to do it. However, in cases where we need to pass a large number of parameters to procedure we use
memory. This memory may be dedicated section of general memory or a part of stack.
• Example :
.data
Num1 db 11h
Num2 db 22h
Res db ?
.code
---
- --
CALL PRO1
---
---
PRO1 PROC NEAR
Mov al,num1 // Procedure PRO1 access the data from memory
Mov bl,num2
Add al,bl
Mov res,al
---
---
RET
PRO1 ENDP
End
ends
TOPIC 2 : PROCEDURE: PASSING
PARAMETERS
3. Using Pointers : In some cases we pass parameters to procedures by using index registers.
• Example :
.data
Num1 db 11h,22h
Res db ?
.code
---
- --
Mov si,offset num1
CALL PRO1
---
---
PRO1 PROC NEAR
Mov al,[si] // Procedure PRO1 access the data pointed by source index SI
Mov bl,[si+1]
Add al,bl
Mov res,al
---
---
RET
PRO1 ENDP
End
ends
TOPIC 2 : PROCEDURE: PASSING PARAMETERS
4. Using Stack : In some cases we pass parameters to procedures by using index registers.
• Example :
.data
Num1 db 11h
Num2 db 22h
Res db ?
.code
---
- --
PUSH num1
PUSH num2
CALL PRO1
---
---
PRO1 PROC NEAR
Mov al,[sp] // Procedure PRO1 access the data pointed by stack pointer SP
Mov bl,[sp+1]
Add al,bl
Mov res,al
---
---
RET
PRO1 ENDP
End
ends
Write an ALP for addition, subtraction, multiplication and division using
procedure

model small .code Add1 proc mul1 proc


Mov ax,@data Mov al,num1 Mov al,num1
Mov ds,ax Mov bl,num2 Mov bl,num2
.data Add al,bl
num1 db 44h Mul bl
Call add1 RET RET
num2 db 22h Mov res_add,al ENDP ENDP

res_add db ? Call sub1 sub1 proc div1 proc


Mov res_sub,al Mov al,num1 Mov al,num1
res_sub db ? Mov bl,num2 Mov bl,num2
Call mul1 sub al,bl Div bl
Mov res_mul,ax RET RET
res_mul dw ?
ENDP ENDP
Call div1
quo db ? Mov quo,al
Mov rem,ah
rem db ?
TOPIC 3 : MACROS:
• macro is a group of instructions.
• In macro assembler generates the code in the program each time where the macro is called.
• Macros can be defined by MACRO and ENDM assembler directives.
• Creating macro is very similar to creating a new opcode that can be used in the program,
M1 MACRO
----
----
ENDM
• In macro the sequences execute faster than procedures because there are no CALL and RET instructions to
execute.
• The assembler place the macro instructions in the program each time when it is invoked.
• This procedure is known as MACRO EXPANSION.
TOPIC 3 : MACROS: PARAMETER PASSING
In macro, parameter are passed as a part of statement which calls macro.

• Example:

PROMPT MACRO MESSAGE ; Defines macro PROMPT with MESSAGE as a parameter


mov ah,09h
Lea MESSAGE
int 21h
ENDM
.data
msg1 db ‘hello$’
msg2 db ‘good morning$’
.code
mov ax,@data
mov ds,ax
PROMPT msg1
PROMPT msg2
end
ends
TOPIC 3 : MACROS:
• Nested Macros :
• When macro call appears within a macro definition, the macros are called nested macros.
• The only limitation to nested macro is that macro within a macro definition (inside macro) must be defined before the
macro definition (outside macro) .
• Example :
INSIDE MACRO
MOV AH,09H
ENDM
OUTSIDE MACRO
MOV CH,CL
INSIDE
MOV BH,AH
ENDM
: MACROS: CONTROLLED
TOPIC 3
EXPANSION
• In Macros, using parameters passed, it is
possible to determine whether or not
some part of the macro to be ignored or Statement Function
not.
IF IF the expression is true
• Such ability of an assembler to select IFB IF the argument is blank
the code that is to be assembled is called
controlled expansion or conditional IFE IF the expression is not true
assembly. IFDEF IF label has been defined
• IF-ELSE-ENDIF statement : IFNB IF the argument is not blank
IFNDEF IF label has not been defined
• The IF-ELSE-ENDIF statement can be
used in varies form as: IFIDN IF argument 1 equals argument 2
IFDIF IF argument 1 not equals argument 2
: MACROS: CONTROLLED
TOPIC 3
EXPANSION
• Example :
SUB MACRO num1,num2,res
IFIDN <num1> <num2>
mov res,00H
ELSE
mov al,num1
sub al,num2
mov res,al
ENDIF
ENDM
: MACROS: CONTROLLED
TOPIC 3
EXPANSION
• REPEAT statement :
• In macro the repeat statement is used to repeat macro sequence for a fix number of times.
• The sequence of instructions to be repeated will be packed between REPEAT and first ENDM directives.
• Example :
ALPHABET MACRO
mov dl,’A’ ; Load dl with the ASCII value of character A
REPEAT 26
INT 21H
INC DL
ENDM
ENDM
: MACROS: CONTROLLED
TOPIC 3
EXPANSION
• WHILE statement :
• In macro the while statement is used to repeat macro sequence until the expression specified with it is true.
• Like REPEAT the end of while loop is specified by ENDM directives.
OPERATO FUNCTION
• Following relational operators can be used for while statement : R
EQ Equal
NE Not Equal
LE Less than or equal
LT Less Than
GE Greater than or equal
GT Greater than
NOT Logical Inversion
AND Logical AND
OR Logical OR
: MACROS: CONTROLLED
TOPIC 3
EXPANSION
• Example :
DISP MACRO NO
mov al,no
WHILE al NE 00h
INT 21H
dec al
ENDM
ENDM
: MACROS: CONTROLLED
TOPIC 3
EXPANSION
• FOR statement :
• A FOR statement in the macro repeats the macro sequence for a list of data.
• If we pass two arguments to the macro then in the first iteration the For statement gives the macro sequence using first
argument and in the second iteration it gives the macro sequence using second argument.
• The sequence of instructions to be repeated will be packed between FOR and first ENDM directives.
• Example :
DISP MACRO num1,num2,num3
mov al,num1
FOR num2,num3
add al,ARG
INT 21H
ENDM
ENDM
TOPIC 3 : MACROS:
add1 MACRO n1,n2 div bl mov r_sub,al
mov al,n1 ENDM mul1 num1,num2
mov bl,n2 mov r_mul,ax
add al,bl .model small div1 num1,num2
ENDM .data mov quo,al
sub1 MACRO n1,n2 num1 db 44h mov rem,ah
mov al,n1 num2 db 22h end
mov bl,n2 r_add db ? ends
sub al,bl r_sub db ?
ENDM r_mul dw ?
mul1 MACRO n1,n2 quo db ?
mov al,n1 rem db ?
mov bl,n2 .code
mul bl mov ax,@data
ENDM mov ds,ax
div1 MACRO n1,n2 add1 num1,num2
mov al,n1 mov r_add,al
mov bl,n2 sub1 num1,num2
TOPIC 3 : PROCEDURE:
• ALP to perform sum of series of 10
numbers Next: call proadd
Inc si
.model small Loop next
Mov sum,al
.data
Mov ah.4ch
Arr1 db Int21h
11h,22h,33h,03h,04h,05h,55h,88h,12h,24h
Proadd proc near
Sum db ?
Add al,[si]
Count db 0Ah Adc ah,00h
RET
.code
Proadd ENDP
Mov ax,@data
Ends
Mov ds,ax end
Mov si, offset str1
Mov cx,count
TOPIC 3 : MACROS:
• ALP to perform X=(A+B)*(C+D)
Add1 MACRO n1,n2 X dw ?
Mov al,n1 .code
Mov bl,n2 mov ax,@data
Add al,bl mov ds,ax
ENDM Add1 A,B
.model small mov cl,al
.data Add1 C,D
A db 11h mul cl
B db 22h mov X,ax
C db 33h ends
D db 44h end
Difference between Procedure and
Procedure
Macro Macro
Procedure contains a set of instructions which can be called Macro definition contains a set of instruction to support
repetitively which can perform a specific task. modular programming.

It is used for large set of instructions mostly more than ten It is used for small set of instructions mostly less than ten
instructions. instructions.
In case of procedure memory requirement is less. In case of macro memory requirement is high.
CALL and RET instruction/statements are required in procedure. CALL and RET instruction/statements are not required
in macro.
Assembler directive PROC is used to define procedure and Assembler directive MACRO is used to define macro
assembler directive ENDP is used to indicate the body is over. and assembler directive ENDM to indicate body is over.

Execution time of procedures is high as it executes slower than Execution time of macro is less as it executes faster than
macro. procedure.
Here machine code is created only once, it is generated only once Here machine code is created multiple times as each
when the procedure is defined. time machine code is generated when macro is called.

Overhead time takes place during calling procedure and returning Overhead time does not take place as there is no calling
control to calling program. and returning.
Difference between Far and Near

Far Near
Within Different CS Within Same CS

Replace old pair CS:IP with new pair Replace old IP with new IP

Value of pair CS:IP is Pushed on the Value of IP is Pushed on the stack


stack
Also called as Intersegment call Also called as Intra-segment call
ADVANTAGES AND
DISADVANTAGES OF
PROCEDURE
Procedures:
AND MACRO
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 make the program take
up more memory than using a Procedure.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy