Professional Documents
Culture Documents
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology
Procedures - Introduction
Procedures in assembly language program are part of
main program written separately from the main program.
This part of the program separately written usually has
some specific functionality.
There are two important reasons for using procedure in
ALP.
1. To avoid repetitive group instructions in the main
program.
2. To make the program modular.
There are two types of procedure FAR and NEAR procedure
depending location of procedure
Microprocessors & Microcontrollers – Unit-I: Topic 1.4
from
Dr.R.D.Chintamani
call. (Outside
Department of Information Technology
4
Contd..
• A RET instruction at the end of the procedure returns execution to the next
instruction in the main line
MAINLINE OR CALLING
PROGRAM
PROCEDURE
INSTRUCTIONS
CALL
NEXT MAINLINE
INSTRUCTIONS
RET
Lower level
Procedure Procedure
CALL CALL
Next Main Line
Instructions
RET
RET
• First, it stores the address of the instruction after the CALL instruction on the
stack
• This address is called the return address because it is the address that
execution will return to after the procedure executes
10
Contd..
• If the CALL is to a procedure in the same code segment,
then the call is near, and only the instruction pointer
contents will be saved on the stack
Contd..
Defining procedures
• For example,
Factorial PROC NEAR
. . .. . .. . .
Factorial ENDP
21
Types of CALL
• WITHIN-SEGMENT NEAR CALL: produce the starting address of the procedure by
adding a 16-bit signed displacement to the contents of the instruction pointer.
• INTERSEGMENT FAR CALL: used when the called procedure is in different
segment.
22
• When the 8086 does a far call, it saves the contents of both the instruction
pointer and the code segment register on the stack
24
Contd..
• A RET instruction at the end of the procedure copies these values from the
stack back into the IP and CS register to return execution to the mainline
program
25
8086 Stack
Contd..
• SP register is automatically decremented by 2 before a word is written to the
stack
• This means that at the start of your program you must initialize the SP register
to point to the top of the memory you set aside as a stack rather than
initializing it to point to the bottom location
28
Contd..
STACK_SEG SEGMENT STACK
DW 40 DUP(0)
STACK_TOP LABEL WORD
STACK_SEG ENDS
CODE SEGMENT
ASSUME CS:CODE, SS:STACK_SEG
29
Contd..
START:
MOV AX, STACK_SEG
MOV SS, AX
LEA SP, STACK_TOP
.
.
CODE ENDS
END START
30
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
DATA SEGMENT MOV AL, 01H
N DB 05H MOV BL, N
FACT DW ? CALL FACTORIAL
DATA ENDS MOV FACT, AX
MOV AH, 4CH
INT 21H
AGAIN:
MUL BL
DEC BL
JNZ AGAIN
RET
FACTORIAL ENDP
CODE ENDS
END START
32
Contd..
• The 4 major ways of passing parameters to and from a procedure are:
1. Using global declared variable
2. Using registers of CPU architecture
3. Using memory location
4. Using stack
5. Using public and extern
33
Contd..
PROCEDURE FACTO
IF N = 1
FACTORIAL = 1
RET
ELSE
REPEAT
DECREMENT N
CALL FACTO
UNTIL N=1
MULTIPLY (N-1)! X PREVIOUS N
RET
37
Disadvantage
• Processor needs to do extra work to save status of current procedure and load
status of called procedure.
39
Contd..
Contd..
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STCK_SEG
:
CALL MULTIPLY_32
:
CODE ENDS
41
Contd..
PROCEDURES SEGMENT
MULTIPLY_32 PROC FAR
ASSUME CS:PROCEDURES
:
:
MULTIPLY_32 ENDP
PROCEDURES ENDS
Defining a NEAR Procedure
A NEAR type of the procedure is the procedure which is in
the same segment as its call.
Assembler directives PROC and ENDP are used to
define procedures.
Last instruction in every procedure is always RET
instruction.
Skeleton
AVERAGE
NEAR
of a PROC
procedure
;Instructions to find
average
……….
……….
AVERAG RET
E ENDP
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology
Defining a NEAR Procedure
;Main Program ;Procedure
DATASEG1 SEGMENT AVERAGE PROC NEAR
;Data definition ;Instructions to find average
………. ……….
………. ……….
DATASEG1 END RET
CODESEG1 SEGMENT AVERAGE ENDP
;Program instructions logic
……….
CALL ;1st
call
AVERAGE
………. ;2nd call
CALL
AVERAGE
CODESEG1 ……….
c
END
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology
Passing parameter to the procedures
There are different ways to pass the parameters to a
procedure from main program.
1. Using Register
2. Using Memory
3. Using Stack
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology
Topic Summary – Near and Far Procedures Part-I
Procedure - Introduction
Defining a near procedure
Use of stack in procedure
Passing parameters to and from
procedure
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology
Next Topic – Near and Far Procedures Part - II
Defining FAR Procedure
Use of EXTERN and PUBLIC directive to define FAR procedures
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology
Ask Questions & Share Responses
You can ask your questions and share your responses at:
Email: chintamanirameshwarit@Sanjivani.org.in
Google Class room
Lecture Presentation and Literature
To refer presentation of this lecture and literature visit:
Google Classroom: Microprocessors & Microcontrollers
References:
1 Peter Abel, Niyaz Nizamuddin, "IBM PC Assembly Language and Programming", Pearson Education.
2 Ray Duncan, "Advanced MS DOS Programming", 2nd Edition, BPB Publications.
3 Douglas Hall, “Microprocessors and Interfacing”, 2nd Edition, 1992, MGH, ISBN-0-07-100462-9.
Microprocessors & Microcontrollers – Unit-I: Topic 1.4 Dr.R.D.Chintamani Department of Information Technology