i wrote this code where i have to read instructions from a file and have to recognise every instruction, and at the end i want to put the instructions in a list.
i created a list of objects to describe each instruction (the opcode,the source ..)
my problem is that when i read each line(instruction) from the file, i wanna get the opcode of the instruction to know how to work and extract the other information from the instruction line i tried to do switch case like this :
def OpcodeSwitcher(inst_opcode) :
switcher = {
'ADD': add_sub_branch(line), #function I call when the instruction is ADD
'SUB': print(1)
}
and then called this function which the opcode I extracted from my line but I got struck in infinity loop !
i just can't figure it out , any help ?
here is an example of a very small file :
ADD $3, $0, 4
SUB $7, $0, 1
# Program ends with infinite loop (avoid fall-through to undefined data following the end of the program)
# Data for this program. Any other location that is not explicitly set is implicitly set to zero.
D@0x000032A0
0x12345678
0xFFFFFFFF
here is the main file:
import sys
import memory as Memory
# main
file_name = sys.argv[1]
print("Loading memory image file:", file_name)
Memory.sim_mem_reset(file_name)
and here is the memory file :
import sys
instructionsList = []
class Sim_cmd(object) :
def __init__(self, opcode, src1, src2,isSrc2Imm,dst) :
self.opcode=opcode
self.src1=src1
self.src2=src2
self.isSrc2Imm=isSrc2Imm
self.dst=dst
def get_inst(line,inst_num) :
inst_opcode=line.split(' ', 1)[0]
# here I am trying to do a case switch statement on the inst_opcode
# for example if the opcode is "ADD" I would like to call a function to handle
# this line to fill the instructionsList which means to do something like this
# instructionsList.append(Sim_cmd(inst_opcode,0,0,0,0))
def sim_mem_reset(memFileName):
with open(memFileName, 'r+') as fileName:
for line in fileName:
if line[0] == '#' or len(line.strip()) == 0:
continue
inst = 0
while len(line.strip()) != 0 and line[0] != '#' and line[0] != 'D':
get_inst(line, inst)
inst += 1
# here the data memory begin
if line[0] == 'D' and line[1] == '@':
#some code
User contributions licensed under CC BY-SA 3.0