Question: any help with this Task using python and Regex expresionts ( the first part of the codes and the description of the problem is below








and include it in your report. Using inst_asm list as input, remove all lines that contain just labels to get the resulting list of only instructions. For the removed labels, calculate their equivalent address and store them in a dictionary called labels. where key will be the "label name' and value will be its equivalent 'address'. Assume that first instruction is stored at address 0 and every instruction requires 4 bytes, therefore, the second instruction will be at address 4 , third instruction will be at address 8 and so on. Also, a labels address is same as the address of its first instruction. labels = dict() A dictionary to store the label name and its equivatent address as a key-value potr Implement a function to replace all integers in string format to int data types. Take a screenshot of the output and include it in your report. Using inst_asm list as input, find integers in each instruction and change their data type from to Using inst_asm list as input, find labels in each instruction and replace them with their equivalent value. Be sure to take into consideration the current address location. Example: If there is an instruction ['bge', 'a0', 'a1;; 'done'] at address 8 and label is at address 20 (as calculated from Task 5), then the resulting instruction will be ['bge', 'a0', 'a1', 12] def replace_labels(inst_asm, labels): \#... enter your code here \#.. end your code here \#\# .. check your output by uncommenting the lines below .. If \# Lnst_asm - replace_tabels(inst_asm, labels) A print_asm_inst(inst_asm) print (+3) \# print_ase_tabets(labels) Implement a function to save the processed assembly code to a .txt files. Take a screenshot of the output and include it in your report. def save_asm(inst_asm, filename): \# . enter your code here \#.. end your code here \#\#A. .. save your final output to a, txt file by unconnenting the lines below .. MII \# save_asm(inst_asm, filename[:-4]+"_out1, txt") In this lab, you will be desgning an assembler to pre-process an assembly code. TODO - Pre-process assembly code ('example.asm') by completing Tasks 1 - 8 - Save the pre-processed code to a 'txt' file (Task 9) Some portions of the code have already been implemented for you such as reading the assembly code file, converting the register names to their equivalent values and printing the processed instructions and labels. Also, assume that inputs for a task are outputs from the previous task. \# Function to read the assembly code file \#\# ef read(filename): "'read each line from a file" asm_inst = list () with open(filename, ' r ') as f : for line in f: asm inst. append (line) return asm_inst \#\# Function to get the equivalent register's value def get_reg_value(reg_name): " "gets the equivalen value for the respective register name". "s8": 24,"59:25,"510:26,"511:27,"t3":28,"t4:29,"t5:30,"t6:31} return int(reg_name [1:]) elif reg_name in reg_abi: return reg_abi[reg_name] elif reg_name.isdecimal(): return int(reg_name) else: raise ValueError(f "Invalid register name/value: \( \{\text { reg_name }\}^{\prime \prime} \) ) \#\# FOR TESTING: Function to print the instructions def print_asm_inst(inst_asm): " 'prints list of instructions". print("Assembly Instructions:") if len(inst_asm) ==0 : print(None) else: for line in inst_asm: print(1ine) \#\# \#\# FOR TESTING: Function to print the labels def print_asm_labels(labels): " 'prints list of labels". print("Assembly Labels: ") if len(labels) ==0 : print(None) else: max len =max(5,max([len(1abe1) for label in labels ])) print(f" { 'LABEL': 5}) for label, val in labels.items(): print (f{ label: { max_len }}{val:>5}) inst_asm = [] \# Hist to store instructions labels =[] \# list to store labels \#\# reads assembly code and stores it in List of lists 'inst_asm' where axis a (rows) corresponds \#A to each Line in the file and axis 1 (columns) corresponds to each argument in that instruction filename = "example, asm" inst asm = read(filename) def remove_comments(inst_asm): \# - enter your code here rem = r'\#.*' empty=[] for i in inst_asm: new_line= re.sub(rem," ", i) empty.append(new_line) return empty \# - end your code here \#\# _. check your_output by uncommenting the lines below -. \#\# inst_asm = remove_comments(inst_asm) print_asm_inst(inst_asm)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
