Procedures and Macros PROCEDURES AND MACROS When we
- Slides: 19
Procedures and Macros
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 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 name> ENDP
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 PROC FAR : : RET B ENDP CODE_SEG 1 ENDS : B :
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 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 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
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 �Flow diagram �Pseudo code
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 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 name> Code ends
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 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 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.
- Cnc macro programming software
- Macros in ms project
- Para que sirve una macro en word
- Pcos macros
- Dragon naturally speaking macros
- Nasm macro
- Macros in word 2016
- Macros in computer architecture
- What is modular programming in microprocessor
- Macros assembly
- Fldigi macros
- Macros in dbms
- Macros rayleigh
- Macros
- Verilog parameterized bit width
- Presentaciones interactivas macros
- Elixir while loop
- Presentaciones interactivas macros
- Spoilersync
- Macros computer science