-
Notifications
You must be signed in to change notification settings - Fork 1
/
disasm.py
56 lines (45 loc) · 1.35 KB
/
disasm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import sys
import re
def op(v):
m = {
'0000' : 'NOP',
'0001' : 'LRI',
'0100' : 'ADD',
'0101' : 'SUB',
'0110' : 'OR',
'0111' : 'XOR',
'1000' : 'BRA', # branch to address in memory location RB
'1001' : 'BRANZ', # branch to address in memory location RB, if RA is zero
'1010' : 'BRAL', # branch to literal address RB
'1011' : 'BRALNZ', # branch to literal address RB, if RA is zero
'1100' : 'CALL',
'1111' : 'HALT',
}
return m[v]
def b(v,bits=16):
o = str()
for i in range(bits-1,-1,-1):
s = 1<<i
if v & s:
o += "1"
else:
o += "0"
if i and not i%8:
o += " "
return o
def main(filename):
data = open(filename,"r").read().split("\n")
for idx,line in enumerate(data):
if re.compile("^[abcdefABCDEF0-9]{4,8}$").search(line):
val = int(line,16)
bin = b(val,28)
RA = (val >> 8) & 0x0F
RB = (val >> 4) & 0x0F
RD = (val) & 0x0F
RA = (val >> 16) & 0xff
RB = (val >> 8) & 0xff
RD = (val) & 0xff
print "0x%02x %s %s %-6s 0x%02x 0x%02x 0x%02x" % (idx, line, bin,op(bin[:4]),RA,RB,RD)
if __name__ == '__main__':
main(sys.argv[1])