Notes-Procedures Ver1
Notes-Procedures Ver1
Definition:
NAME PROC NEAR/FAR
…….
…….
RET
NAME ENDP
Classification of Procedures
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)
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
CODE1 SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AX,NUMBER1
MOV DX,NUMBER2
MOV AH,4CH
INT 21H
CODE1 ENDS
END START
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
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
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
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
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