Chap 5 - PPT
Chap 5 - PPT
Microprocessor
• 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.
• 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
4
8086 Microprocessor
Procedures
• Advantages.
• Disadvantages.
1. Extra code is required to integrate procedure i.e. CALL and RET instructions.
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.
6
8086 Microprocessor
Procedure and Macro in Assembly Language Program
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:
8
8086 Microprocessor
Model of Assembly Language Programming
9
8086 Microprocessor
Procedures
• Difference between Re-entrant Procedure and Recursive Procedure.
10
8086 Microprocessor
Procedures
Directives of Procedure
• PROC: Procedure
• ENDP: End of procedure
PROC: Procedure
• General form :
Example:
FACTORIAL PROC NEAR
……….
……….
{Procedure code}
……….
……….
11
FACTORIAL ENDP
8086 Microprocessor
Procedures
Directives of Procedure
• PROC: Procedure
• ENDP: End of procedure
• General form :
Procedure_name ENDP
Example:
FACTORIAL END ; End of procedure
FACTORIAL
12
8086 Microprocessor
Procedures
• Syntax :
CALL Procedure_name
• 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.
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.
14
8086 Microprocessor
Procedures
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
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.
• Syntax :
RET
• Operation performed:
16
8086 Microprocessor
Procedures
Passing Parameter to procedure and from procedure :
• Parameters are the arguments which are passed between main program and
procedure.
2. Output parameters: Result parameters passed from procedure back to the main
program.
• 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 :
1. In registers
2. In dedicated memory locations accessed by name
3. With stack
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:
20
8086 Microprocessor
Procedures
3.Passing Parameters with stack :
• Stack Instructions:
• 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 :
22
8086 Microprocessor
Macros
Macros
• Defining Macros :
• For macros that you want to include in your program, you must first define them.
• 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.
2. Macro can be called just writing by its name along with parameters, hence no
extra code is required like CALL & RET.
• Disadvantages.
1. Object code is generated every time a macro is called hence object file
becomes lengthy.
24
8086 Microprocessor
Macros
Directives of MACROS :
1. MACRO
2. ENDM : END of MACRO
3. LOCAL
4. INCLUDE
5. PURGE
1. MACRO:
• General form :
• 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.
• It is used to place all the data and frequently used macros into a file known as
header file or include file.
• General form :
• Example:
INCLUDE C:\TASM\MACRO.LIB
INCLUDE C:\TASM\BIN\MYPROC.LIB
28
8086 Microprocessor
Macros
Directives of Macros
5. PURGE:
• 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
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