Difference between Macro and Procedure
Last Updated :
03 Sep, 2024
While programming, particularly in languages such as C or assembly, you come across such terms as macro and procedure (or function). The two are vital that assist in the creation of good, easy to manage code but are not used in the same manner and are not the same. You may come across macros and procedures and the differences between those two tools will help you decide which one should be used in which situation, making the code more efficient and easily readable. In this article, the reader will be able to find out what macros and procedures are, their strengths and weaknesses and the general comparison of the two.
Assembly language is a common intermediate level programming language which is used for microprocessor programming. This macro and procedure are two concepts in assembly by which modular programming is implemented. So now let's understand how macro and procedure are different from each other.
Macro
Macro is a set of instruction and the programmer can use it anywhere in the program by using its name. It is mainly used to achieve modular programming. So same set of instructions can be used multiple times when ever required by the help of macro. Wherever macro's identifier is used, it is replaced by the actual defined instructions during compilation thereby no calling and return occurs.
Syntax of Macro
%macro macro_name number_of_parameters
<macro body>
%endmacro
Advantages of Macros
- Speed: Macros are evaluated during compilation and as such they do not have any runtime processing which makes them fast.
- Reusability: This type enables the code reuse keywords, but do not entitle a function call overhead.
- Simplicity: Macros can help to make the code more readable as compared to the conventional coding since this reduces on repetition of the same code.
Disadvantages of Macros
- Debugging Difficulty: Another disadvantage of macros is that they remain expanded before the compilation process and that makes the process of debugging more complicated.
- No Type Checking: We can’t perform type checking for macros that may lead to more problems if used in a wrong way.
- Code Bloat: If macros are used inappropriately, the size of the code will be large because the repeated codes will be written each time the macro is employed.
Procedure
Procedures are also like macro, but they are used for large set of instruction when macro is useful for small set of instructions. It contains a set of instructions which performs a specific task. It contains three main parts i.e., Procedure name to identify the procedure, procedure body which contains set of instructions, and RET statement which denotes return statement. Unlike macros, procedures follow call-return method thereby achieving true modularity.
Syntax of Procedure
procedure_name :
procedure body
….......................
RET
To call a procedure
CALL procedure_name
After execution of procedure control passes to the calling procedure using RET statement.
Advantages of Procedures
- Modularity: They make writing of code easier since you are able to divide a large program into small functions that are easy to manage.
- Reusability: They may be used in one program or part of one program as well as in another program or another part of the same Program.
- Type Safety: It is crucial when thinking about procedures because these make the type safe allowing the user to specify input and outputs.
- Easier Debugging: Procedures are easily debuggable because if there is an error it can be contained and found in one function only.
Disadvantages of Procedures
- Runtime Overhead: Whenever calling a procedure incurs a runtime overhead due to the stack that is followed to execute the respective procedure.
- Slower than Macros: In certain circumstances, procedures are usually slower than macros due to the of overhead time required in the function call.
Difference between Macro and Procedure
S.No | MACRO | PROCEDURE |
---|
01. | Macro definition contains a set of instruction to support modular programming. | Procedure contains a set of instructions which can be called repetitively which can perform a specific task. |
---|
02. | It is used for small set of instructions mostly less than ten instructions. | It is used for large set of instructions mostly more than ten instructions. |
---|
03. | In case of macro memory requirement is high. | In case of procedure memory requirement is less. |
---|
04. | CALL and RET instruction/statements are not required in macro. | CALL and RET instruction/statements are required in procedure. |
---|
05. | Assembler directive MACRO is used to define macro and assembler directive ENDM is used to indicate the body is over. | Assembler directive PROC is used to define procedure and assembler directive ENDP is used to indicate the body is over. |
---|
06. | Execution time of macro is less as it executes faster than procedure. | Execution time of procedures is high as it executes slower than macro. |
---|
07. | Here machine code is created multiple times as each time machine code is generated when macro is called. | Here machine code is created only once, it is generated only once when the procedure is defined. |
---|
08. | In a macro parameter is passed as part of statement that calls macro. | In a procedure parameters are passed in registers and memory locations of stack. |
---|
09. | Overhead time does not take place as there is no calling and returning. | Overhead time takes place during calling procedure and returning control to calling program. |
---|
Conclusion
As we have discovered macros and procedures are both important tools in programming but each of them has a different function. Macros are perfect for storing single and multiple lines of code which are to be run fairly often and for which it is crucial to save time, while procedures work best with longer code that can be divided into segments for easier organization and that require type checking. Knowing the distinctions and times when to use each will serve to produce cleaner and easier to manage code.
Also read:- Assembly language
Microprocessor