Experiment No4
Experiment No4
Experiment No. 04
Roll No. 66
program.
Types of Macros
1. Keyboard Macros – Automate sequences of keystrokes or mouse
actions.
2. Programming Macros – Shortcuts in languages like C, C++,
Python, and Assembly to expand complex code.
3. Excel & Word Macros – Used in Microsoft Office (VBA) to
automate document editing or data processing.
Examples of Macros
1. C Programming Macro (Preprocessor Directive)
#include <stdio.h>
#define PI 3.14159 // Macro definition
int main() {
printf("The value of PI is %f\n", PI);
return 0;
}
Advantage: PI is replaced at compile time, improving performance.
Advantages of Macros
Automation – Reduces repetitive work.
Faster Execution – Macros expand at compile time in programming.
Reduces Errors – Minimizes human mistakes.
Disadvantages of Macros
Hard to Debug – Errors in macros can be difficult to track.
Security Risks – Malicious macros (e.g., in VBA) can execute
harmful code.
Not Flexible – Limited compared to full-fledged functions in some
cases.
if line.startswith('MACRO'):
parts = re.findall(r'\w+', line)
macro_flag = True
macro_name = parts[1]
argument_list = parts[2:] # Extract arguments if any
argument_list_array[macro_name] = argument_list
macro_name_table[macro_name] =
len(macro_definition_table) # Store MDT index
macro_definition = []
elif line.startswith('MEND'):
if not macro_flag:
print("Error: Found MEND without a MACRO definition")
return
macro_flag = False
macro_definition_table[macro_name] = macro_definition
elif macro_flag:
macro_definition.append(line)
else:
# Check if the line contains a macro invocation
for macro, arguments in argument_list_array.items():
if macro in line:
parts = line.split()
macro_index = parts.index(macro)
arguments_values = parts[macro_index + 1:] # Get
arguments passed
expanded_code.extend(expanded_lines)
break
else:
expanded_code.append(line)
# Example usage
file_path = 'assembly_code.txt'
output_file = 'output.txt'
process_macros(file_path, output_file)
MULTIPLY 5 10 RESULT1
SQUARE 4 RESULT2
END
Output of the
program:
Outcome of the
Experiment:
References: