0% found this document useful (0 votes)
31 views18 pages

Procedures & Macros

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views18 pages

Procedures & Macros

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Procedures and 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,

STACK_SEG SEGMENT STACK


DW 1024 dup (?)
STACK_SEG ENDS
PUSH and POP Operation
 Stack Segment register (SS) Push Direction
initializes the stack segment. ss
 Stack Pointer (SP) will point to
bp
the top of the stack.
 If you push the values onto the sp

stack, CPU stores them into


Push Direction
successively lower memory
ss
locations.
next element
 PUSH will decrease the stack sp
to be placed

pointer (SP) by 2 and then


top of stack

places the data word in the


stack.
PUSH and POP Operation
(Contd...)
 POP is an operation which copies the top
element to the register and then increase
the stack pointer by 2
 All pushes and pops are 16-bit operations.
 There is no way to push a single 8-bit value
onto the stack.
 To push an 8-bit value you should load it
into 16-bit register first, with the higher
order byte being 00h.
Procedures
 Procedures are also called as ‘Sub-programs’
or
‘Sub-routines’.
 A Procedure is a separate group of instructions
apart from the main program.
 Procedures are accessed by CALL Instruction
during the execution of the program.
 A RET Instruction at the end of the procedure
returns execution to the main program.
 So, Procedures represent Top-Down Approach.
Procedure Block Syntax
 <procedure name> PROC (FAR | NEAR)
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
RET
<procedure name> ENDP
Near Procedure Example

CODE_SEG SEGMENT

: :
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

 Parameters represent the Address or


Data passed back and forth between
main program and Procedure.
 The types for passing parameters are
 Registers.
 Memory Locations.
 Pointers accessed by Registers.
 Stack.
Recursive Procedures
 A Recursive procedure calls the
procedure itself.
 Recursive procedures are used in
complex data structures like
TREES.
 Ex: Factorial procedure
Re-entrant Procedures
 A Procedure which can be Interrupted,
Used and Re-entered is known as
Re-entrant Procedure.
 Push the values used in procedure into
stack.
 Passing parameters through registers
and stack are only allowed in re-entrant
procedures.
Macros
 Macro is a group of Instructions,
which when CALLed, inserts those
group of Instructions in the place
of CALL.
 Macro should have less no. of
instructions.
 There is no need of transferring
the execution like a procedure.
Macro Block without
Parameters
 <Macro name> MACRO
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
ENDM
Code Segment
<Macro name>
Code ends
Macro Block with
Parameters
 <Macro name> MACRO (arg1,arg2,....)
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
ENDM
Code Segment
<Macro name> (arg1,arg2,..)
Code ends
Procedures and Macros -
Comparison
 Code of procedure is  Code of macro have to
once loaded in be loaded repeatedly
memory. in memory.
 No CALL Instruction
 Procedures will use
CALL instruction for
accessing.  No-over head time for
 More Time taken for CALL and RET.
CALL and RET.  No Need of Stack.
 Stack is needed.  Less no. of
 More no. of Instructions.
Instructions

You might also like