0% found this document useful (0 votes)
157 views40 pages

Chap 5 - PPT

The document discusses procedures and macros in assembly language programming. It defines procedures as reusable blocks of code that perform a specific task. Procedures can be of two types - near procedures that are in the same code segment, and far procedures that are in a different segment. The CALL and RET instructions are used to call and return from procedures. Macros allow code to be reused without the overhead of procedures.

Uploaded by

Swapnali
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)
157 views40 pages

Chap 5 - PPT

The document discusses procedures and macros in assembly language programming. It defines procedures as reusable blocks of code that perform a specific task. Procedures can be of two types - near procedures that are in the same code segment, and far procedures that are in a different segment. The CALL and RET instructions are used to call and return from procedures. Macros allow code to be reused without the overhead of procedures.

Uploaded by

Swapnali
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/ 40

STES’

Sou. Venutai Chavan Polytechnic, Vadgaon(Bk),Pune-41.

Microprocessor

Mrs. Arati S. Patil


Unit 5

Procedure and Macro in Assembly


Language Program

Mrs. Arati S. Patil


8086 Microprocessor
Procedure and Macro in Assembly Language Program

• Procedures

• The process of splitting a large program into small tasks and designing
them independently is known as modular programming.

• Large problems can be divided into smaller tasks to make them more
manageable.

• A repeated group of instruction in a program can be organized as


subprogram. The subprograms are called as subroutine or procedures in
assembly language programming which allows reuse of program code.

• A procedure is a set of the program statements that can be processed


independently and reuse again and again.

• A procedure is the assembly equivalent of a Java or C function.

• Here are the four steps that need to be accomplished in order to call and
return from a procedure.
1. Save return address
2. Procedure call
3. Execute procedure
4. Return

3
8086 Microprocessor
Procedure and Macro in Assembly Language Program

• Procedures

• A procedure is group of instructions that usually performs one task. It is


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.

4
8086 Microprocessor
Procedures
• Advantages.

1. Simple modular programming.

2. Reduced work load and development time.

3. Debugging of the program and procedure are easier.

4. Reduction in the size of the main program.

5. Reuse of the procedure many times in the same program or in another


program.

6. Library of the procedures can be implemented by combining well designed,


tested and proven procedures.

• Disadvantages.

1. Extra code is required to integrate procedure i.e. CALL and RET instructions.

2. Required extra overhead while establishing a linkage between the calling


program and the called procedure or vice versa, hence execution time is
more.

3. As the run time overhead required for communication between small


procedures is more than the execution time of the procedure and hence the
use of small procedures are not preferable , instead, macro can be used. 5
8086 Microprocessor
Procedure and Macro in Assembly Language Program

Re-entrant Procedure and Recursive Procedure:

Re-entrant Procedure:
• In some situations it may happen that procedure1 is called from main
program, procedure2 is called from procedure1 and procedure1 is again called
from procedure2. In this situation program execution flow reenters in the
procedure1. This type of procedures are called Reentrant Procedures.

Fig. The flow of program execution for reentrant procedure.

6
8086 Microprocessor
Procedure and Macro in Assembly Language Program

Re-entrant Procedure and Recursive Procedure:

Recursive 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 (recursion depth) = 3. Then the n is decremented by one after
each procedure CALL and the procedure is called until n = 0:

• There are two kinds of recursion direct and indirect.

Fig. The flow diagram and pseudo-code for recursive procedure. 7


8086 Microprocessor
Assembly Language Programming

8
8086 Microprocessor
Model of Assembly Language Programming

9
8086 Microprocessor
Procedures
• Difference between Re-entrant Procedure and Recursive Procedure.

• Re-entrant Procedure Recursive Procedure

1. The procedure which can be It is the procedure which


interrupted, used and “reentered” calls itself.
without losing or writing over
anything.

2. The flow of control could be The flow of control could be


interrupted by a hardware caused by CALL instruction
interrupt and transferred to an and transferred to user’s
Interrupt Service Routine (ISR). procedure.

10
8086 Microprocessor
Procedures
Directives of Procedure
• PROC: Procedure
• ENDP: End of procedure

PROC: Procedure

• PROC Indicates the beginning of a procedure

• ENDP End of procedure

• FAR Inter-segment call

• NEAR Intra-segment call

• General form :

Procedure_name PROC {Near/Far}

Example:
FACTORIAL PROC NEAR
……….
……….
{Procedure code}
……….
……….
11
FACTORIAL ENDP
8086 Microprocessor
Procedures

Directives of Procedure
• PROC: Procedure
• ENDP: End of procedure

ENDP: End of procedure

• General form :

Procedure_name ENDP

Example:
FACTORIAL END ; End of procedure
FACTORIAL

12
8086 Microprocessor
Procedures

Procedure Call [CALL Instruction]


• 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.

• There are two types of calls.

1. Inter-segment call or NEAR call 2. Intra-segment call or FAR call

• Syntax :

CALL Procedure_name

• Operation performed for NEAR Call :

• When 8086 executes a near CALL instruction, it decrements the stack pointer
by 2 and copies the IP register contents on to the stack. Then it copies address
of first instruction of called procedure.

If NEAR CALL, then SP SP – 2


Save IP on Stack
IP address of procedure 13
8086 Microprocessor
Procedures

Procedure Call [CALL Instruction]


• Operation performed 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.

If FAR CALL, then SP SP – 2


Save CS on Stack
CS New segment base
address of the called procedure
SP SP – 2
Save IP on stack and
IP New offset address of
the called procedure

14
8086 Microprocessor
Procedures

Difference Between NEAR Call and FAR Call:

Near Call Far Call

1. A near call refers a procedure A Far call refers a procedure


which is in the same code which is in different code
segment. segment.

2. It is also called Intra-segment It is also called Inter-segment


call. call.

3. A Near Call replaces the old IP A FAR replaces CS & IP with new
with new IP. CS & IP.

4. It uses keyword near for calling It uses keyword far for calling
procedure. procedure.

5. Less stack locations are required. More stack locations are required.

15
8086 Microprocessor
Procedures

Procedure Return [RET Instruction]


• 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. The RET instruction are of two types.

1. Near RET or Inter-segment return

2. Far RET or Intra-segment return

• Syntax :

RET

• Operation performed:

1. For NEAR Return, then IP content of top of stack


SP SP + 2

2. For FAR Return, then


IP contents of top of stack SP SP + 2
CS contents of top of stack
SP SP + 2

16
8086 Microprocessor
Procedures
Passing Parameter to procedure and from procedure :
• Parameters are the arguments which are passed between main program and
procedure.

• Two types of parameters:

1. Input parameters: Parameters passed from main program to procedure.

2. Output parameters: Result parameters passed from procedure back to the main
program.

• 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. 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 CS register
contents remains unchanged for main program and procedure. But for far
procedures CALL instruction pushes both IP and CS on the stack.

• We often want a procedure to process some data or address variable from the
main program. For processing, it is necessary to pass these address variables or
data, usually referred as Parameter Passing Techniques. There are three ways to
pass parameters to and from the procedure

17
8086 Microprocessor
Procedures
Passing Parameter to procedure and from procedure :

• Parameters can be passed between procedures in any of three ways

1. In registers
2. In dedicated memory locations accessed by name
3. With stack

• By convention, high-level languages (like C, Pascal, PL/1, ect.) pass


parameters by placing them on the stack.

• Parameter on the stack can be passed by Value or by Reference. Passing by


Value means to put a copy of each parameter value on the stack. Passing by
Reference means to put a copy of each parameter offset (effective address) on
the stack.

• Parameters on the stack can then be accessed by procedures by using


displacements or a stack-frame structure.

18
8086 Microprocessor
Procedures
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.

19
8086 Microprocessor
Procedures
2.Passing Parameters in Dedicated Memory Locations Accessed by Name:

• When large number of parameters is to be passed to the procedure, then these


parameters can be placed in an argument list as dedicated memory locations in
one of the data segment from memory. e.g. consider the program to calculate a
square of given number.

20
8086 Microprocessor
Procedures
3.Passing Parameters with stack :

• A stack is a region of memory used for temporary storage of information.


Memory space should be allocated for stack by the programmer. The last value
placed on the stack is the 1st to be taken off. This is called LIFO (Last In, First
Out) queue. Values placed on the stack are stored from the highest memory
location down to the lowest memory location. SS is used as a segment register
for address calculation together with SP.

• Stack Instructions:

• Flags: Only affected by the popf instruction.

• Addressing Modes: src & dst should be Words and cannot be immediate. dst
cannot be the ip or cs register.
21
8086 Microprocessor
Procedures
3.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.

22
8086 Microprocessor
Macros
Macros
• Defining Macros :

• 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.

• For macros that you want to include in your program, you must first define them.

• A macro definition appears before any defined segment.

• When assembler encounters a macro name in ALP, then the macro is expanded
i.e. the block of code of that macro is substituted.

• The directives MACRO indicates to the assembler the beginning of the macro and
the directive ENDM indicates the end of the macro to the assembler.

23
8086 Microprocessor
Macros
Advantages and disadvantages of MACRO :

• Advantages.

1. Program written with macro is more readable.

2. Macro can be called just writing by its name along with parameters, hence no
extra code is required like CALL & RET.

3. Execution time is less because of no linking and returning.

4. Finding errors during debugging is easier.

• 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.

24
8086 Microprocessor
Macros
Directives of MACROS :

1. MACRO
2. ENDM : END of MACRO
3. LOCAL
4. INCLUDE
5. PURGE

1. MACRO:

• General form :

MACRO-name MACRO [ARGUMENT 1,……….ARGUMENT N]

• Example :
DISPLAY MACRO 12,13
……….
……….
[MACRO code]
……….
……….

ENDM

• The Label prior to MACRO is the macro name which should be used in the actual
program. The ENDM directive marks the end of the instructions. A macro can be
called by quoting its name along with any values to be passed to the macro.12 &
13 are values to be passed with macro. 25
8086 Microprocessor
Macros
Directives of Macros
2. ENDM: End of MACRO

• The directive MACRO and ENDM must enclose the definition, declarations, or a
small part of the code which have to be substituted at the invocation of the macro.

• General form :

ENDM

• Example:
DISPLAY MACRO 12,13
……….
……….
[MACRO code]
……….
……….

ENDM
• The Label prior to MACRO is the macro name which should be used in the actual
program. The ENDM directive marks the end of the instructions. A macro can be
called by quoting its name along with any values to be passed to the macro.12 &
13 are values to be passed with macro.

26
8086 Microprocessor
Macros
Directives of Macros
3. LOCAL :

• Some macro requires that you define data item and instruction labels within
the macro definition.

• If you use the macro more than once in the same program and the assembler
defines the data item or label for each occurrence, the duplicate name would
cause the assembler to generate error messages.

• General form :

LOCAL dummy_1,dummy_2……..

• Example:
;Converts a lowercase letter to uppercase

to_upper MACRO ch
LOCAL done ; case conversion macro
cmp ch,'a’ ; check if ch >= 'a'
jb done
cmp ch,'z’ ; and if ch >= 'z'
ja done
sub ch,32 ; then ch := ch - 32
done:
ENDM
27
8086 Microprocessor
Macros
Directives of Macros
4. INCLUDE :

• The directive INCLUDE informs the assembler to include the statement defined
in the include file.

• The name of the include file follows the statement INCLUDE.

• It is used to place all the data and frequently used macros into a file known as
header file or include file.

• The part of assembler which processes the include file is known as


preprocessor.

• General form :

INCLUDE<file path specification with file name>

• Example:
INCLUDE C:\TASM\MACRO.LIB

INCLUDE C:\TASM\BIN\MYPROC.LIB

28
8086 Microprocessor
Macros
Directives of Macros
5. PURGE:

• Execution of an INCLUDE statement causes the assembler to include all the


macro definitions that are in the specified library.

• Suppose, the library contains the macros DISP, READ_NUM, DISP_8BIT_NUM


etc. but a program requires only READ_NUM.

• The PURGE directive enables you to “delete” the unwanted macros DISP and
DISP_8BIT_NUM from the current assembly.

• Examples:

INCLUDE D:\TASM\MACRO.LIB

PURGE DISP, DISP_8BIT_NUM

29
8086 Microprocessor
Macros
Difference between PROCEDURE & MACRO.

30
8086 Microprocessor
Programming using Procedures

31
8086 Microprocessor
Programming using Procedures

32
8086 Microprocessor
Programming using Procedures

33
8086 Microprocessor
Programming using Procedures

34
8086 Microprocessor
Programming using Procedures

35
8086 Microprocessor
Programming using Macros

36
8086 Microprocessor
Programming using Macros

37
8086 Microprocessor
Programming using Procedures
Write an ALP to multiply two 8 bit numbers using NEAR procedure

.MODEL SMALL
.DATA
NUM1 DB 04H
NUM2 DB 03H
RESULT DW ?
.CODE
MOV AX,@DATA ; INITIALIZE DATA SEGMENTS
MOV DS, AX
CALL MUL_NUM
MOV AH,4CH
INT 21H ; CALLING OF MUL_NUM PROCEDURE
; TERMINETE THE PROGRAMMUL_NUM PROC
; STRAT OF MUL_NUM PROCEDURE
MOV AL, NUM1
MUL NUM2
MOV RESULT, AX
RET ;MOVE NUM1 TO AL
;MULTIPLY AL WITH NUM2
;MOVE OUTPUT FROM AX TO RESULT
;RETURN TO CALLING PROGRAM
ENDP ; END OF PROCEDURE
ENDS ;END OF SEGMENT
END ;END OF PROGRAM

38
8086 Microprocessor
Programming using Procedures
Write an assembly language program to multiply two 8 bits numbers using NEAR
procedure. Also draw the flowchart for the same.

DATA SEGMENT
NUM1 DB 53H
NUM2 DB 33H
RES DW ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START: MOV AX,DATA
MOV DS,AX
CALL MULT
INT 3H
MULT PROC
MOV AL, NUM1
MUL NUM2
MOV RES, AX
RET
MULT ENDP
CODE ENDS
END START

39
8086 Microprocessor
Programming using Procedures
Write an assembly language program to multiply two 8 bits numbers using NEAR
procedure. Also draw the flowchart for the same.

40

You might also like