2. Introduction
Procedure Definitions
Procedure Declaration
Call and Return
Uses of procedure
Saving and Restoring Register
Procedure Communication
Recursive Procedure
3. The procedure is an important part of any
computer system's architecture.
A procedure is a group of instructions that
performs one task.
This saves memory space and makes it easier to
develop software.
It is a reusable section of the software that is stored
in memory once, but used as often as necessary.
4. Procedure is a set of code that can be branched and
returned.
The branch to a procedure is referred to as the call and
corresponding branch back is known as the return.
Procedure provides the primary means of breaking
code in a program into modules.
The disadvantage is small amount of time to link to the
procedure and return from it.
5. name - is the procedure name should be in the top and
the bottom. It is used to check correct closing of
procedures.
The RET instruction is used to return to the operating
system and the same instruction is used to return from
procedure.
PROC and ENDP are compiler directives. So, they are
not assembled into any real machine code. The
Compiler just remembers the address of procedure.
name PROC
; The procedure code
; goes here
RET
name ENDP
Syntax Example
SEGX SEGMENT
:
SUBT PROC FAR
:
RET
SUBT ENDP
:
CALL FAR PTR SUBT
:
SEGX ENDS
6. The CALL instruction links to the procedure and the
RET instruction returns from the procedure.
The Stack stores the return address whenever a
procedure is called during the execution of a
programme.
The CALL instruction pushes the address of the
instruction following it on the Stack.
The RET instruction removes an address from the
Stack so the programme returns to the instruction
following the CALL.
8. Both the calling program and procedure share the
same set of register.
Save the registers when entering a procedure and
to restore them before returning to the calling
program.
(DX) (DX)
(CX) (CX)
(BX) (BX)
(AX) (AX)
Return
address
Return
address
TOS
TOS
0208
0200SP
SP
(a)After storing register (b)After restoring register
9. SUBT PROC NEAR
PUSH AX
PUSH BX
PUSH CX
PUSH DX
:
:
POP DX
POP CX
POP BX
POP AX
RET
SUBT ENDP
10. The Procedure communication has two types.
They are,
One it is always operate on the same set of data.
Another it is may process a different set of data each
time.
If a procedure is of the former type and is in the same
source module as the calling program.
Then the procedure can refer to the variable directly as
indicated in the program
11. DATA SEGMENT
ARY DW 100 DUP(?)
COUNT DW ?
SUM DW ?
DATA ENDS
CODE SEGMENT
:
:
CALL NEAR PTR PROADD
:
:
PROADD PROC NEAR
PUSH AX
PUSH CX
PUSH SI
LEA SI,ARY
MOV CX,COUNT
XOR AX,AX
NEXT: ADD AX,[SI]
ADD SI,2
LOOP NEXT
MOV SUM,AX
POP SI
POP CX
POP AX
PROADD ENDP
:
:
CODE ENDS
Example: Procedure that refer directly to the
variables on which it operates
12. Recursive algorithm may be implemented by having
procedure call by itself.
Each successive call does not destroy the parameters and
results generated by the previous call and to make sure the
procedure does not modify itself.
A trivial example of recursive procedure is one of the
evaluating factorials.
BEGIN FACT(N,RESULT)
SAVE register on stack
IF N=0 THEN
RESULT 1
ELSE
PUSH RESULT
PUSH N-1 onto stack
CALL FACT(N-1,RESULT)
RESULT N*RESULT
ENDIF
RESTORE register from stack
DELETE parameters from stack
RETURN
13. :
:
Parameters or parameter
addresses
Parameters or parameter
addresses
Save area for registers
Temporary results
Save area for registers
Temporary results
Frame for
second call
Frame for
first call
FIGURE: Use of a stack to dynamically provide
storage during recursive call