Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views2 pages

Pass 2

The document outlines a Python implementation of a Macro Pass 2 Simulator, which processes a list of assembly language program lines. It includes a first pass to read and store macro definitions and a second pass to expand these macros in the program. The output consists of the expanded program and an Argument List Array (ALA) showing the arguments used in macro calls.

Uploaded by

Prem Dubay 3229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Pass 2

The document outlines a Python implementation of a Macro Pass 2 Simulator, which processes a list of assembly language program lines. It includes a first pass to read and store macro definitions and a second pass to expand these macros in the program. The output consists of the expanded program and an Argument List Array (ALA) showing the arguments used in macro calls.

Uploaded by

Prem Dubay 3229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Macro Pass 2 Simulator (Python version)

# Input program (as list of lines)


program = [
"MACRO",
"NEERAJ1 &FIRST,&SECOND=DATA9",
"B 1,&FIRST",
"H 2,&SECOND",
"MEND",
"MACRO",
"NEERAJ2 &ARG1,&ARG2=DATA5",
"N 4,&ARG1",
"OH 5,&ARG2",
"MEND",
"EXP2 START 89",
"USING *,BASE",
"NEERAJ1 DATA1,DATA9",
"NEERAJ2 DATA3,DATA4",
"FOUR DC F'6'",
"FIVE DC F'8'",
"BASE EQU 8",
"TEMP DS 1F",
"DROP 8",
"END"
]

# Structures to hold macro definitions


macro_definitions = {}
recording_macro = False
current_macro = None # <=== THIS LINE WAS MISSING!

# First Pass: Read macro definitions


for line in program:
if line.strip() == "MACRO":
recording_macro = True
macro_content = []
continue
if line.strip() == "MEND":
recording_macro = False
macro_definitions[current_macro['name']] = {
'params': current_macro['params'],
'body': macro_content
}
current_macro = None
continue
if recording_macro:
if not current_macro:
parts = line.strip().split()
macro_name = parts[0]
params = parts[1].split(',')
current_macro = {
'name': macro_name,
'params': [p.split('=')[0] for p in params]
}
else:
macro_content.append(line.strip())

# Second Pass: Expand macros


expanded_program = []
ala_table = []

for line in program:


words = line.strip().split()
if not words:
continue
first_word = words[0]

if first_word in macro_definitions:
# This is a macro call
args = words[1].split(',')
macro_info = macro_definitions[first_word]
param_names = macro_info['params']

# Build ALA (Argument List Array)


ala_table.append(args)

# Expand the macro


for macro_line in macro_info['body']:
expanded_line = macro_line
for idx, arg in enumerate(args):
expanded_line = expanded_line.replace(f"&{param_names[idx][1:]}",
arg)
expanded_program.append(expanded_line)
elif first_word not in ["MACRO", "MEND"]:
expanded_program.append(line.strip())

# Output
print("===== PASS 2 =====")
for line in expanded_program:
print(line)

print("\nALA:")
for ala in ala_table:
print(ala)

You might also like