Python: Script for data discovery. The dangers

I am working on a school project to write a program that detects data dependencies after reading in assembly instructions. I have a list of lists containing instructions.

One such example is a list, for example, line1 = [[ld a8,0x8910] [mul a3, a2,8] [shl a3, a3,4] [Add a3, a3, a8]]

Here, the last add command depends on the result of the left (shl) shift, which in turn depends on the result of mul intsruction.

I want my code to output the dependency as {mul → sh → add}

Another example: line2 = [[add a3, a2, a1] [sub a4, a5, a6] [add a9, a2, a4]] output: {add → add} {sub → add}

I want to delete the command codes to get the string1 = [[ a8 , 0x8910] [ a3 , a2,8] [ a3 , a3,4] [ a3 , a3, a8]] and then extract the destination operands in dst_list = [ a8, a3, a3, a3 } and the source operands to another list as src_list = [0x8910, [a2,8], [a3,4], [a3, a8]]. I am going to take the nth element from src_list and compare 0 through n-1 elements of dst_list and output the indices when they match. Finally, use a dictionary to display instructions that match my indexes. Is my approach right? Can someone help me on how to achieve this in python?

So far I have tried:

 dest = re.findall( r'\[(?=([a-z0-9.]+))',str(line))
 src = re.findall( r'\,(?=([a-z0-9]+))', str(line))
 for i in dest:
                    dst_list.append([i])
 for j in src:
                    src_list.append(j)

#psuedo code to find hazards
for nth src_item in src_list:
     for 0 to n-1 dst_items in dst_list:
          if src_list[src_item] == dst_list[dst_item]
              OUTPUT dst_item -> src_item

re.findall - ( 2 src ).

?

+4
1

, , , .

[op_code dest,src1,src2,...,srcn]

:

line1 = ["ld a8,0x8910","mul a3,a2,8","shl a3,a3,4","add a3,a3,a8"]

.

def instruction_reader(op_string):
    opcode,values = op_string.split(" ")
    values_list = values.split(',')
    dest = values_list[0]
    src = values_list[1:]

    return (opcode,dest,src)

list_of_all = [instruction_reader(item) for item in line1]
opcodes = [op[0] for op in list_of_all]
dests = [dest[1] for dest in list_of_all]
srcs = [src[2] for src in list_of_all]

srcs dests .

dep_list = []
for i,dest in enumerate(dests):
    for src in srcs:
        if dest in src:
            dep_list.append(opcodes[i])

dep_list = [opcodes[i] for i,dest in enumerate(dests) for src in srcs if dest in src]

: , python , , /.

+1

All Articles