Procedures & Macros
Procedures & Macros
Stack Segment
Stack is a piece of memory reserved for an EXE
program which stays in memory.
DOS executable of .EXE format must have a stack of
sufficient size to operate normally.
The definition of the stack inside the stack segment is
DW <size of stack> dup (<initial value>)
For example,
: :
CALL A
: :
A PROC NEAR
: :
: :
RET
A ENDP
: :
CODE_SEG ENDS
Far Procedure Example
CODE_SEG SEGMENT
: :
CALL B
: :
CODE_SEG ENDS
CODE_SEG1 SEGMENT
B PROC FAR
: :
RET
B ENDP
CODE_SEG1 ENDS
CALL Instruction
CALL Instruction is used to call a procedure from the
main program.
The CALL instructions take the same forms as the JMP
instructions.
There are two types of CALL Instructions.
i) Near CALL :- Procedure in same Code Segment
Pushes the 16-bit offset of the next instruction following
the call onto the stack.
Copies the 16-bit effective address of procedure into the
IP register.
Execution continues at the first instruction of the
procedure.
CALL Instruction (Contd..)
ii) Far CALL :- Procedure in different Code
Segment
Pushes the CS register onto the stack.
Pushes the 16-bit offset of the next instruction
following the call onto the stack.
Copies the 32-bit effective address into the CS:IP
register.
Execution continues at the first instruction of the
Procedure.
CALL Instruction can also have
Direct and Indirect Near (Intrasegment) CALL
Direct and Indirect Far (Intersegment) CALL.
RET Instruction
The RET (Return) Instruction returns control to the
next instruction of the Main Program from the
Called Procedure.
RET Instruction for Near Call
Pops 16-bit return address off the stack into the IP register
Transfer the control to the instruction at the return
address.
RET Instruction for far call
Pops 16-bit offset into the IP register.
Pops 16-bit segment value into the CS register.
Transfer the control to the instruction at the return
address.
Passing Parameters to and from
Procedures