Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures and Macros.

Similar presentations


Presentation on theme: "Procedures and Macros."— Presentation transcript:

1 Procedures and Macros

2 PROCEDURES AND MACROS When we need to use a group of instructions several times throughout a program there are two ways we can avoid having to write the group of instructions each time we want to use them. 1 One way is to write the group of instructions as a separate procedure. Another way we can use macros.

3 Procedures The procedure is a group of instructions stored as a separate program in the memory and it is called from the main program whenever required. 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.

4 Procedure Block Syntax
<procedure name> PROC (FAR | NEAR) ………………..Instructions…………… RET <procedure name> ENDP

5 Near Procedure Example
CODE_SEG SEGMENT : : CALL A A PROC NEAR RET A ENDP CODE_SEG ENDS

6 Far Procedure Example CODE_SEG SEGMENT : : CALL B CODE_SEG ENDS
: : CALL B CODE_SEG ENDS CODE_SEG1 SEGMENT B PROC FAR RET B ENDP CODE_SEG1 ENDS

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

8 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.

9 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.

10 Reentrant Procedure Diagram

11 Recursive Procedures A Recursive procedure calls the procedure itself.
Recursive procedures are used in complex data structures like TREES. Ex: Factorial procedure

12 Recursive Procedure A recursive procedure is procedure which calls itself.

13 Recursive Procedure Flow diagram Pseudo code

14 Factorial program ALP for Factorial of number using recursive procedures CODE SEGMENT ASSUME CS:CODE START: MOV AX,7 CALL FACT MOV AH,4CH INT 21H FACT PROC NEAR MOV BX,AX DEC BX BACK: MUL BX JNZ BACK RET ENDP CODE ENDS END START

15 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.

16 Macro Block without Parameters
<Macro name> MACRO ………………..Instructions…………… ENDM Code Segment <Macro name> Code ends

17 Macro Block with Parameters
<Macro name> MACRO (arg1,arg2,....) ………………..Instructions…………… ENDM Code Segment <Macro name> (arg1,arg2,..) Code ends

18 Advantage of Procedure and Macros:
Procedures: Advantages The machine codes for the group of instructions in the procedure only have to be put once. Disadvantages Need for stack Overhead time required to call the procedure and return to the calling program. Macros: Advantages Macro avoids overhead time involving in calling and returning from a procedure. Generating in line code each time a macro is called is that this will make the program take up more memory than using a procedure.

19 Procedures and Macros - Comparison
Code of procedure is once loaded in memory. Procedures will use CALL instruction for accessing. More Time taken for CALL and RET. Stack is needed. More no. of Instructions Code of macro have to be loaded repeatedly in memory. No CALL Instruction No-over head time for CALL and RET. No Need of Stack. Less no. of Instructions.


Download ppt "Procedures and Macros."

Similar presentations


Ads by Google