4
4
5
5
def createInstructions (splitinst ):
6
6
instructionlist = []
7
+ i = 0
7
8
for inst in splitinst :
8
-
9
9
if (inst [0 ] == 'ADD' ):
10
10
if (inst [3 ][0 ].isdigit ()): #imm or reg check if the first letter is a reg (L)
11
11
imm = inst [3 ]
@@ -20,6 +20,7 @@ def createInstructions(splitinst):
20
20
targetreg = inst [1 ][1 :]
21
21
targetreg = bin (int (targetreg ))[2 :].zfill (3 )
22
22
instructionlist .append ('00' + str (imm ) + str (sourcereg ) + str (targetreg ) + str (alusrc ))
23
+ i += 1
23
24
24
25
elif (inst [0 ] == 'SUB' ):
25
26
if (inst [3 ][0 ].isdigit ()): #imm or reg
@@ -34,6 +35,7 @@ def createInstructions(splitinst):
34
35
targetreg = inst [1 ][1 :]
35
36
targetreg = bin (int (targetreg ))[2 :].zfill (3 )
36
37
instructionlist .append ('01' + str (imm ) + str (sourcereg ) + str (targetreg ) + str (alusrc ))
38
+ i += 1
37
39
38
40
elif (inst [0 ] == 'LOAD' ): #takes 3 cycles and three different instrcutjons
39
41
instruction1 = '00' + bin (int (inst [1 ][1 :]))[2 :].zfill (8 ) + '001010' #point
@@ -42,13 +44,29 @@ def createInstructions(splitinst):
42
44
targetreg = bin (int (targetreg ))[2 :].zfill (3 )
43
45
instruction3 = '100000000000' + str (targetreg ) + '0' #load
44
46
instructionlist .append ([instruction1 , instruction2 , instruction3 ])
47
+ i += 1
45
48
46
49
elif (inst [0 ] == 'STORE' ): # takes 2 cycles and 2 instrucitons
47
50
sourcereg = inst [1 ][1 :]
48
51
sourcereg = bin (int (sourcereg ))[2 :].zfill (2 )
49
52
instruction1 = '00' + bin (int (inst [2 ]))[2 :].zfill (8 ) + '001010'
50
53
instruction2 = '1100000000' + str (sourcereg ) + '0000'
51
54
instructionlist .append ([instruction1 , instruction2 ])
55
+ i += 1
56
+
57
+ elif (inst [0 ] == '.data' ): #handing .data we dont do non with .text
58
+ data = splitinst [i + 2 :]
59
+ print (data )
60
+ for addys in data :
61
+ location = addys [1 ]
62
+ if (int (location ) > 0 ):
63
+ counter = 0
64
+ while (int (location ) != counter ):
65
+ datafile .write ("0 " )
66
+ counter += 1
67
+ print (addys [0 ])
68
+ datafile .write (addys [0 ] + " " )
69
+
52
70
return instructionlist
53
71
54
72
if (__name__ == "__main__" ):
@@ -60,36 +78,41 @@ def createInstructions(splitinst):
60
78
61
79
program = open ('program.txt' , 'r' ).readlines ()
62
80
instructionfile = open ("inputfile.txt" , 'w' ) # write to the instruction mem file
63
-
64
- instructionfile .write ("v3.0 hex words addressed\n " ) #have to write the header
81
+ datafile = open ('datafile.txt' , 'w' )#data .....data..
65
82
83
+ instructionfile .write ("v3.0 hex words addressed\n " ) #have to write the header
84
+ datafile .write ("v3.0 hex words addressed\n " )
85
+ datastart = 00
86
+ datafile .write (str (datastart ).zfill (2 ) + ": " )
66
87
baseaddress = 0000 #line starts here in the instruction file, each line has 16 spots(0-15), next address is 0010
67
88
# print(program)
68
89
instlist = []
69
90
for line in program : # putting all instructions ina list
70
-
91
+
71
92
if (line .strip () == "" ):# skip empty lines
72
93
continue
73
94
line_content = line .split ("//" )[0 ].strip ()#remove comments and any whitespace
74
95
75
96
if line_content : # only addcontent to the list
76
97
instlist .append (line_content )
77
98
78
- # print(instlist)
99
+ print (instlist )
79
100
80
101
splitinsts = []
81
102
for inst in instlist : #seperates each isntructions into its individual parts
82
103
inst = inst .split ()
83
104
splitinsts .append (inst )
84
105
85
- # print(splitinsts)
106
+ print (splitinsts )
86
107
instructions = createInstructions (splitinsts )
87
108
#print(instructions)
88
109
i = 0
89
110
instructionfile .write (str (baseaddress ).zfill (4 ) + ": " )
90
111
for instruction in instructions :
91
112
if (i == 16 ):
92
113
instructionfile .write ("\n " + str (baseaddress + 10 ).zfill (4 ) + ": " )
114
+ elif (instruction == '.data' ):
115
+ pass
93
116
elif (type (instruction ) == list ):
94
117
for x in instruction :
95
118
print (hex (int (x , 2 ))[2 :].zfill (4 ), x )
0 commit comments