Program control instruction procedure
PROCEDURES
A procedure is a group of instructions (subroutine or function) that usually perform a specific task.
Advantages: Disadvantages:
(a)It is reusable section of the software that It takes the compiler a small amount
is stored in memory once, but used as often of time to link the procedure and
as necessary. return from it.
(b)It saves memory space.
(c)Makes easier to develop software.
PROCEDURES
How procedure links with main program
The CALL instruction links to the procedure and the RET (return) instruction return
from the procedure.
The CALL instruction pushes the address (return address) of the instruction following the CALL on the stack. The RET instruction removes an address from the stack so the
program return to the instruction following the CALL.
A procedure begins with the PROC directive and ends with the ENDP directive. The
PROC directive is followed by the type of procedure: NEAR (intrasegment) or FAR
(intersegment).
Format of Procedure
XXX PROC NEAR/FAR
............................................
............................................
............................................
RET
XXX ENDP
Example:
SUMS PROC NEAR
ADD AX,BX
ADD AX,CX
ADD AX,DX
RET
SUMS ENDP
N.B
XXX is the name of level (both level name should be same)
To call a procedure in main program write: CALL XXX
PROCEDURES (continued)
CALL instruction
The CALL instruction transfer the flow of the program to the procedure. The CALL
instruction differ from a jump instruction because a CALL saves a return address on the
stack.
Whenever a CALL instruction executes it:
Pushes the IP or, CS:IP on the stack.
Changes the value of IP or, CS:IP.
Jumps to the procedure by new IP or, CS:IP address.
Difference between JMP and CALL instruction
JMP CALL
Doesn’t use stack Uses stack
Doesn’t return to the next instruction of Must return to the next instruction of
JMP CALL
PROCEDURES (continued)
Types of CALL
(a)Near CALL (b) Far CALL
Difference between Near CALL and Far Call
Near CALL Far CALL
(1) Procedure located within the same code (1) Procedure located in the entire segment (±32KB) memory (1 MB)
(2) 3-byte instruction (2) 5-byte instruction
(3) Only IP content is replaced by (IP± (3) Both CS and IP contentsare displacement) replaced by new CS and IP address
(4) Stack stores only return IP address (2 byte) (4) Stack stores the return CS and IP address. (4 byte)
RET instruction
The Return (RET) Instruction removes a 16-Bit number (near return) from the stack and places it into IP or removes a 32- Bit number (far return) and places it into IP And CS. The Near and far return instructions are both defined in the procedure’s PROC directive, which automatically selects the proper return instruction.
Comments
Post a Comment