Procedures and Macros PROCEDURES AND MACROS When we

  • Slides: 19
Download presentation
Procedures and Macros

Procedures and Macros

PROCEDURES AND MACROS �When we need to use a group of instructions several times

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.

Procedures �The procedure is a group of instructions stored as a separate program in

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.

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

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

Near Procedure Example � CODE_SEG SEGMENT A : CALL : : A : PROC

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

Far Procedure Example � CODE_SEG SEGMENT : CALL : ENDS CODE_SEG 1 SEGMENT B

Far Procedure Example � CODE_SEG SEGMENT : CALL : ENDS CODE_SEG 1 SEGMENT B PROC FAR : : RET B ENDP CODE_SEG 1 ENDS : B :

CALL Instruction � � � CALL Instruction is used to call a procedure from

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.

Passing Parameters to and from Procedures �Parameters represent the Address or Data passed back

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.

Re-entrant Procedures �A Procedure which can be Interrupted, Used and Re-entered is known as

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.

Reentrant Procedure �Diagram

Reentrant Procedure �Diagram

Recursive Procedures �A Recursive procedure calls the procedure itself. �Recursive procedures are used in

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

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

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

Recursive Procedure �Flow diagram �Pseudo code

Recursive Procedure �Flow diagram �Pseudo code

Factorial program ALP for Factorial of number using recursive procedures CODE SEGMENT ASSUME CS:

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

Macros �Macro is a group of Instructions, which when CALLed, inserts those group of

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…………… ENDM Code Segment <Macro

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

Macro Block with Parameters �<Macro name> MACRO (arg 1, arg 2, . . )

Macro Block with Parameters �<Macro name> MACRO (arg 1, arg 2, . . ) ………………. . Instructions…………… ENDM Code Segment <Macro name> (arg 1, arg 2, . . ) Code ends

Advantage of Procedure and Macros: Procedures: Advantages �The machine codes for the group of

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

Procedures and Macros - Comparison �Code of procedure is once loaded in memory. �Procedures

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.