You are on page 1of 9

Procedures….

Definition:
NAME PROC NEAR/FAR
…….
…….
RET
NAME ENDP

-Unlike macros, RET must be the last instruction of procedures

Classification of Procedures

1. Near Procedures (Intra Segment procedures)


2. Far Procedures (Inter Segment procedures)

Near procedures are located in the same segment, where CALL


instruction is located, FAR procedures are located in other segments.
Based on the keyword NEAR or FAR used while defining the procedures,
opcode/machine code of the RET instruction is selected by the
assembler.

Difference between RET & RETF: RET is used at the end of Near
procedures, while RETF is used at the end of Far procedures (generally
assembler does this automatically)

Invoking the Procedure:

CALL NEAR PTR PROCEDURE-NAME , for near procedures


CALL FAR PTR PROCEDURE-NAME, for far procedures

Types of CALL
1. Intra Segment, Here CALL instruction and Procedure are located in
the same segment
a. Direct Addressing:
Ex: CALL Near Ptr small, small is a procedure name, here
procedure address is part of instruction

b. Indirect Addressing:
- Using Register: ex: MOV BX,offset small
CALL BX
- Using memory reference:
ex: MOV BX,offset LOC1
CALL word ptr [BX] ; BX contains adds of LOC1, which
holds address of procedure
Or, CALL word ptr LOC1, calls the procedure whose adds
is at LOC1

;//Program for Testing Call & its Variations-Intra Segment(use p2.asm)


;machine code for Call(direct) : 11101000,disp-low,disp-high
;machine code for Call(indirect): 11111111,mod010r/m
;//-------------------end-----------------------------
ASSUME CS:CODE1,DS:DATA
DATA SEGMENT
NUMBER1 EQU 77H ; GLOBAL
NUMBER2 EQU 88H ; GLOBAL
SMALL_ADDS DW OFFSET SMALL
DATA ENDS

CODE1 SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AX,NUMBER1
MOV DX,NUMBER2

;-----------Calling IntraSegProcedure Directly


CALL near ptr SMALL

;-----------Calling IntraSegProcedure Indirectly through register


MOV BX,OFFSET SMALL
CALL BX

;-----------Calling the IntraSegProcedure Indirectly through memory


MOV BX,OFFSET SMALL_ADDS
CALL [BX]

MOV AH,4CH
INT 21H

SMALL PROC far


CMP AX,DX
JL SKIP
MOV AX,DX
SKIP: NOP
RET
SMALL ENDP

CODE1 ENDS
END START

2. Inter Segment; CALL and Procedure are located in different


segments
a. Direct Addressing: ex: CALL far ptr small, here offset and
segment address of procedure are part of the instruction.
b. Indirect Addressing:
Ex: CALL DWORD PTR LOC1, or
MOV BX,offset LOC1
CALL DWORD PTR [BX], or any other type mem addng
mode can be used. Here segment and offset address
procedure are stored in memory location, LOC1 (4 bytes).

;//Program for Testing Call & its Variations-Inter Segment ( P3.asm )


;code for Call(direct) : 11101000,disp-low,disp-high
;code for Call(indirect): 11111111,mod010r/m
;//-------------------end-----------------------------

DATA SEGMENT
NUMBER1 EQU 77H ; GLOBAL
NUMBER2 EQU 88H ; GLOBAL
SMALL_ADDS DW OFFSET SMALL,SEG SMALL
DATA ENDS

ASSUME CS:CODE1,DS:DATA

CODE1 SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AX,NUMBER1
MOV DX,NUMBER2

;-----------Calling InterSegProcedure Directly


CALL FAR PTR SMALL ; 9A,IP-LSB,IP-MSB,SEG-LSB,SEG-MSB

;-----------Calling the InterSegProcedure Indirectly through memory


MOV BX,OFFSET SMALL_ADDS
CALL DWORD PTR [BX] ; ff,1Fh
MOV AH,4CH
INT 21H

CODE1 ENDS

ASSUME CS:CODE2,DS:DATA

CODE2 SEGMENT
SMALL PROC FAR
CMP AX,DX
JL SKIP
MOV AX,DX
SKIP: NOP
RET ; ret of far type --RETF
SMALL ENDP
CODE2 ENDS
END START

Note1: Intra segment, direct call is also referred as IP relative


addressing, as offset address of procedure is indicated as signed
displacement with respect to IP contents, i.e address of instruction next
to CALL.

Note2: In Intra segment CALL only offset address (IP contents) of next
instruction of call is pushed to Stack, (hence SP is decremented by 2),
on RET, only one pop is executed to restore IP contents (SP is
incremented by 2)
In Inter-segment call, segment and offset address (CS &IP) of instruction
next to call is pushed to stack, SP is decremented by 2, CS is pushed, SP
is decremented by 2, IP is pushed (SP is effectively decremented by 4).
On RET or RETF of far procedure, restore IP & CS from stack, pop IP,
SP is incremented by 2, pop CS, SP is incremented by 2. (effectively SP is
incremented by 4)

Note3:
8086 supports:
a) Nesting of procedures: It refers one procedure calling another
procedure.
b) Recursive procedure: A procedure calling itself to implement
certain logics like factorial computation.
c) Re-entrant procedures: A procedure execution can be
interrupted, and can be re-entered/called again in ISR without
losing any data. Reentrant procedures must be written is such a
way: - flags, registers used to be pushed and popped, parameter
passing only through registers and stack

Note4: It is a good practice, to save all registers in the beginning of


procedure, and restore them from stack before RET

Passing of Parameters to Procedures:

a) Using Registers : input to procedure is passed through registers


and result is returned through register, very simple and effective
MOV AX,NUMBER1
MOV DX,NUMBER2
CALL SMALL
MOV ANS,AX
….
SMALL proc
;i/p in AX & DX
;o/p is comuted and returned in AX
….
RET
SMALL endp
b) Using memory locations: Not suitable for re-entrant procedure,
but useful when multiple procedures access some common
memory locations to achieve certain logic; here main program
and procedure know about common memory locations.
.data
NUM1 DB 75h
NUM2 DB 60h
.code
CALL SMALL
….
SMALL proc
MOV AX,NUM1
MOV DX,NUM2
….
MOV ANS,AX
RET
CALL endp
c) Using Pointers; Very useful when complex data structures are to
be passed to procedures, pointers are passed in registers.
.data
NUMS DB 6,10,11,12,13,14,15 ;lets assume, its an array and
;first no. indicates its size
.code
MOV BX,offset NUMS
CALL SMALL

SMALL proc
;i/p – BX points to an array
;o/p- answer in AX
MOV CX,[BX]
INC BX
…..logic
….
RET
SMALL endp
d) Using Stack: Useful for integrating Assembly programs with High
level language programs and this method is commonly used to
implement re-entrant procedures.
MOV AX, NUM1
PUSH AX
MOV DX,NUM2
PUSH AX
CALL SMALL
POP AX
POP AX ; AX give the smallest number
….
SMALL proc near
MOV BP,SP
MOV DX,[BP+2]
MOV AX,[BP+4]
…..logic
….
MOV [BP+4],AX ; store the answer to the stack
RET
SMALL endp
Note: Instead of 2 POPs in the main program to access result,
one POP can be used, if RET 2 is used instead of RET in the
procedure.
(write stack images, i.e how stack contents are changed for every
push,pop,ret in the above program)
e) Using EXTRN and PUBLIC
When procedures to be available to other modules (i.e in other
files): a)Procedure to be made ‘PUBLIC’ in the file, where it is
defined b) Use ‘EXTRN’ keyword and procedure name and type
(far/near) in the file, where it is to be invoked /used.
1. ASM - first file
EXTRN DELAY:NEAR
CODE SEGMENT PUBLIC
ASSUME CS:CODE
…..
CALL DELAY
….
CODE ENDS
END

2.ASM - second file

PUBLIC DELAY
CODE SEGMENT PUBLIC

DELAY PROC NEAR
….
RET
DELAY ENDP
CODE ENDS
END
Note: To access variable declared in other file, use
EXTRN variable-name: type of storage
Example: EXTRN count:WORD ; (it informs the assembler, count is
declared as DW in data segment of other file)
To access constant declared in other module, use
EXTRN constant-name:ABS
Example: EXTRN flag:ABS

PROCEDURES AND MACROS- Comparision

Procedures
1. Definition: name proc

…..
Ret
name endp
2. Invoking: Call procedure-name (both direct & indirect
addressing supported, so at run time, which procedure is
executed can be decided)
3. Many methods of passing parameters, including stack
4. Only one copy of procedure is stored in memory, but multiple
times we can call (less memory space)
5. Stack overhead, CALL & RET uses stack, (extra execution time)
6. Recursive and Reentrant procedures are possible
7. Write one example

Macros

1.Definition: name MACRO


….logic
Name ENDM
2.lnvoking: No separate CALL instruction is required, just indicate the
name of the macro, so no indirect addressing is supported.
3. It is assembly time process, code of macro is get substituted at
assembly time only.
4. Limited type of passing parameters, pass along with the name of
the macro.
5. since macro works based on substitution, many copies of macro
occupies space (more memory space)
6. No stack overhead (no extra execution time, hence faster)
7. No support for recursive and re-entrant procedures.
8. Write one example.

You might also like