Unit_5_Proc_Macro
Unit_5_Proc_Macro
Stack operations:
A stack is a separate section of memory set aside to store return addresses &
data whenever a subroutine is executed.
3F 3FFFF SP
48 (Top of the stack)
42
37
:
:
:
21
43 3000 Start of the stack
8086 allows the user to set aside an entire 64K byte segment as stack. The
upper 16 bits of starting address of stack are stored in Stack segment (SS) register.
The stack pointer (SP) register is used to hold the 16 bit offset from the start of the
segment to memory location where a word was almost recently stored on stack. The
memory location where a word is most recently stored is called the Top of the stack.
The 20 bit physical address for a stack operation is produced by shifting the
stack segment register (SS) by 4 bit positions and adding the contents of the stack
point (SP) contents to it.
If SS = 3000h, SP=FFFFH, p.a. = 3 0 0 0 0 +
FFFF
-----------
3FFFF
The stack can be accessed by PUSH & POP instructions.
PUSH instruction:
A PUSH reg. instruction is used to save the 16 bit data of the specified register
on the stack.
Example:
PUSH BX ; BX = 1234H
PUSH AX ; AX = ABCDH
If SP is initialized with 3FFF, whenever a PUSH instruction is encountered,
the SP is decremented by two and the contents of the specified register are saved on
the stack with lower order register contents stored at SP– 2 location and higher order
stored at SP-1 location.
SP 3FFF
3FFE 12
3FFD 34
3FFC AB
SP 3FFB CD
:
:
:
3001
3002
A POP reg. instruction is used to retrieve data back to the contents of the
specified 16 bit register. Whenever a POP instruction is executed, the contents of the
SP & SP+1 locations are stored in the lower order and higher order register
respectively and stack pointer is incremented by 2. The last word PUSHed in the stack
if POPed out first. Thus stack operation is on LIFO basis.
Example:
POP BX
POP AX
3FFF
3FFE 12
3FFD 34
3FFC AB
SP 3FFB CD
:
:
:
3001
3002
After execution, BX = ABCD H & AX = 1234 H. Thus the contents of the registers
are exchanged due to the order in which the contents are POPed back from the stack.
Declaring PROCEDURES:
RET
name ENDP
Example:
Delay PROC
AGAIN: MOV CX,COUNT
LOOP AGAIN
RET
Delay ENDP
The above procedure introduces a delay in between the program statements, when it is
called from the main program.
RET instruction: When 8086 executes a CALL instruction, it stores the return
address of the CALLing routine on the stack. A RET instruction at the end of the
procedure copies the return address stored on the stack back into the CS and IP
registers and then returns execution to the main program.
CODE SEGMENT
START: MOV AX, 5555H
MOV BX,7272h
:
:
CALL PROC1
:
:
PROCEDURE PROC1
:
:
ADD AX,BX
:
:
RET
PROC1 ENDP
CODE ENDS
END START
Re-entrant Procedures :
A procedure is said to be re-entrant, if it can be interrupted, used and re-
entered without losing or writing over anything. To be a re-entrant,
Procedure must first push all the flags and registers used in the procedure.
It should also use only registers or stack to pass parameters.
The flow of re-entrant procedure for a multiply procedure when interrupt procedure is
executed, as shown below.
Multiply
procedure Interrupt
Main Line Procedure
Call
Call Interrupt
Multiply
Multiply Here
Return to
Interrupt
Recursive Procedures:
A recursive procedure is a procedure which calls itself. Here, the program sets
aside a few locations in stack for the storage of the parameters whicha re passed each
time the computation is done and the value is returned. Each value returned is then
obtained by popping back from the stack at every RET instruction when executed at
the end of the procedure.
Example :
CODE SEGMENT
START : MOV AX, DATA
MOV DS,AX
MOV AX, STACK
MOV SS, AX
LEA SP, STACK_TOP
MOV AX, NUMBER
PUSH AX
CALL FACTO
:
:
PROCEDURE FACTO
PUSH AX
:
:
:
;Code for checking the number in AX =1 or not
;If AX <>1
CALL FACTO ; calling FACTO within FACTO.
If AX =1
:
:
POP AX
RET ; ret to calling program
FACTO ENDP
CODE ENDS
END START
Advantages of Procedures:
1. Simple modular programming
2. Reduced workload and development time
3. Debugging of program and procedure is easier
4. Reduction in the size of the main program
5. Reuse of procedures in the same program many times or in another program.
Macros :
Advantages
Simplify and reduce the amount of repetitive coding
Reduce errors caused by repetitive coding
Makes program more readable
Execution time is less as compared to procedures as no extra instructions
required
Defining Macros
The Directive MACRO indicates the beginning of a MACRO
Name of the Macro followed by MACRO and arguments if any are specified.
ENDM is always associated with MACRO which ends the macro.
General Form :
Example:
PRINT MACRO MES
MOV AX,09H
LEA DX, MES
INT 21H
ENDM
The above macro is used to display a string specified in the argument MES on
the screen, when evoked by the main program as given below
DATA SEGEMENT
STR DB 0DH,0AH,”Hello World$”
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START: MOV AX, DATA
MOV DS, AX
PRINT STR ; Calls Macro PRINT to display STR
; STR is the parameter passed which is
;taken as MES in the Macro PRINT.
:
:
CODE ENDS
END START
Note : Main difference between Macro and Procedure is that A call to Macro
will be replaced with its body during assembly time, whereas the call to procedure is
explicit transfer of control during run-time.