Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone solve this problems using Python and Regex expresion all the time .. below and the Tasks Description of the problem Inicial Code assamly

can someone solve this problems using Python and Regex expresion all the time ..
below and the Tasks
Description of the problem
image text in transcribed
Inicial Code
image text in transcribed
image text in transcribed
image text in transcribed
assamly code file example.asm
image text in transcribed
QUESTIONS TO ANWSER
image text in transcribed
Task 3
remove empty lines
image text in transcribed
image text in transcribed
In this labi 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 (fask 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: import CSV \#\# Function to read the assembly code file \#\# def read(filename): " rread 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". reg_abi =\{"zero": 0, "ra" : 1, "sp": 2, "gp": 3, "tp": 4, , "te": 5, "t1": 6," t2":7, "so": 8 ," 51:9,"a0:10,"a1:11,"a2":12,"a3":13,"a4:14, "a5": 15 , "a6": 16,"a7": 17,"52:18,"53:19,"54":20,"55 ": :21, "s6": 22, "s7": 23, "s8": 24, , 59 ": 25,"510:26,"s11":27,"t3:28,"t4:29, "t 5: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(line) \#\# \#\# 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 ( label ) for label in labels ])) for label, val in labels.items(): print(f"\{label:}}{val:>5}) inst_asm =[] \# List to store instructions labels =[]# list to store labels \#\# reads assembly code and stores it in list of lists 'inst_asm' where axis o (rows) corresponds \#\# to each line in the file and axis 1 (columns) corresponds to each argument in that instruction filename = "example asm" inst_asm = read(filename) print_asm_inst(inst_asm) Implement a function to split each line (instruction or label) into separate arguments. Take a screenshot of the output and include it in your report. Using inst_asen list as input, split each line into separate arguments. Possible delimiters can be space, comma, parantheses. (Hint. Use regular expressions) Example: addi a1, a2, 10 changes to: ['addi,'a1;, 'a2', '10'] def split_arg(1nst_asm): \# . enter your code here *... end your code here uf . check your output by wheonenting the tines betowin. we inst_ash - split_arg(that_ase) print_asn_Inst (Inst_asn) ['lts", ae, (s1) ] HH *Task 3*t cspan style-"background-color:ac5EeB4; border; 1px solid; padding: 5px; "sinplenent a function to recove empty lines. Take a sereenshot of the outp and include it in your report, \&/spany Using "inst_ose' as input, whith is now a tist_, renove all of the enpty lists. def renove_enoty(inst_asm): au... check your output by unconenting the Lines betow ... Ae inst_asn - renove_enpty(inst_ase) print_asn_inst(inst_asa) Task 8 Implement a function to replace all the register names with their equivalent valuess, Take a screenshot of the output and include it in your report. In your own words, explain the purpose of the replace_reg function. Use the function get_reg_value() , already implemented for you to replace register names with their equivalent values

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

What is the environment we are trying to create?

Answered: 1 week ago

Question

How can we visually describe our goals?

Answered: 1 week ago