-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyasm.py
executable file
·133 lines (126 loc) · 3.88 KB
/
myasm.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import sys
f = open(sys.argv[1],'r')
code = f.read()
token = code.split()
def toBin(x,width):
return (str(bin(x))[2:])[:width].zfill(width)
def toHex(bits):
print(bits)
return hex(int(bits,2))[2:].zfill(2)
hexcommands = []
i=0
while i<len(token):
print("i = "+str(i)+" token = "+str(token))
if token[i]=='mov':
i1 = token[i+1]
i2 = token[i+2]
opbits = toBin(0,3)
if i1[:1]!='r':
print('moving '+i2+' to non-register')
break
if i2[:1]=='r':
cbit = '0'
r1 = int(float(i1[-1:]))
r1bits = toBin(r1,2)
r2 = int(float(i2[-1:]))
r2bits = toBin(r2,2)
print(token[i]+' '+token[i+1]+' '+token[i+2])
i+=3
print(cbit+' '+opbits+' '+r2bits+' '+r1bits)
hexcommands.append(toHex(cbit+opbits+r2bits+r1bits))
else:
cbit = '1'
r1 = int(float(i1[-1:]))
r1bits = toBin(r1,2)
const = int(i2)
shiftbit = '0'
if const>15:
const>>4
shiftbit = '1'
constbits = toBin(const,4)
print(token[i]+' '+token[i+1]+' '+str(const))
i+=3
print(cbit+' '+shiftbit+' '+constbits+' '+r1bits)
hexcommands.append(toHex(cbit+shiftbit+constbits+r1bits))
elif token[i]=='add':
i1 = token[i+1]
i2 = token[i+2]
opbits = toBin(1,3)
if i1[:1]!='r' or i2[:1]!='r':
print('adding non-registers')
break
else:
cbit = '0'
r1 = int(float(i1[-1:]))
r1bits = toBin(r1,2)
r2 = int(float(i2[-1:]))
r2bits = toBin(r2,2)
print(token[i]+' '+token[i+1]+' '+token[i+2])
i+=3
print(cbit+' '+opbits+' '+r2bits+' '+r1bits)
hexcommands.append(toHex(cbit+opbits+r2bits+r1bits))
elif token[i]=='sub':
i1 = token[i+1]
i2 = token[i+2]
opbits = toBin(2,3)
if i1[:1]!='r' or i2[:1]!='r':
print('adding non-registers')
break
else:
cbit = '0'
r1 = int(float(i1[-1:]))
r1bits = toBin(r1,2)
r2 = int(float(i2[-1:]))
r2bits = toBin(r2,2)
print(token[i]+' '+token[i+1]+' '+token[i+2])
i+=3
print(cbit+' '+opbits+' '+r2bits+' '+r1bits)
hexcommands.append(toHex(cbit+opbits+r2bits+r1bits))
elif token[i]=='mul':
i1 = token[i+1]
i2 = token[i+2]
opbits = toBin(4,3)
if i1[:1]!='r' or i2[:1]!='r':
print('adding non-registers')
break
else:
cbit = '0'
r1 = int(float(i1[-1:]))
r1bits = toBin(r1,2)
r2 = int(float(i2[-1:]))
r2bits = toBin(r2,2)
print(token[i]+' '+token[i+1]+' '+token[i+2])
i+=3
print(cbit+' '+opbits+' '+r2bits+' '+r1bits)
hexcommands.append(toHex(cbit+opbits+r2bits+r1bits))
elif token[i]=='div':
i1 = token[i+1]
i2 = token[i+2]
opbits = toBin(2,3)
if i1[:1]!='r' or i2[:1]!='r':
print('adding non-registers')
break
else:
cbit = '0'
r1 = int(float(i1[-1:]))
r1bits = toBin(r1,2)
r2 = int(float(i2[-1:]))
r2bits = toBin(r2,2)
print(token[i]+' '+token[i+1]+' '+token[i+2])
i+=3
print(cbit+' '+opbits+' '+r2bits+' '+r1bits)
hexcommands.append(toHex(cbit+opbits+r2bits+r1bits))
else:
print('Invalid Command!')
break;
f = open(sys.argv[1]+".bits", 'w')
commands = ''
count = 0
for c in hexcommands:
if count<4:
commands=c+' '+commands
count=count+1
else:
commands=c+'\n'+commands
count=0
f.write(commands)