Pass 1 code:
def Pass1(assembly_code):
# Initialize the Location Counter (LC) and Symbol Table
LC = 0
SymbolTable = {}
# Iterate over each line of the assembly code
for line in assembly_code:
# Check if the line contains a label (a label is followed by ':')
if ":" in line:
# Extract label from the line
label = line.split(":")[0].strip() # Label is before the ':'
# Add the label and its corresponding LC to the Symbol Table
SymbolTable[label] = LC
# Continue to the next line without incrementing LC for labels
continue
# If the line contains data or instruction, determine its type
# (e.g., if it's a directive like `.word`, `.space`, or an instruction like `ADD`)
if line.startswith("."): # If it's a directive (e.g., .word, .data)
LC += size_of_data(line) # Update LC based on data size (e.g., 4 bytes for `.word`)
else: # If it's an instruction (e.g., ADD, LDR, etc.)
LC += 4 # ARM instructions are usually 4 bytes in length
# Return the Symbol Table with labels and their resolved addresses
return SymbolTable