Skip to content

Commit 99b52a2

Browse files
committed
done?
1 parent 2dbf5aa commit 99b52a2

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

assembler.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
def createInstructions(splitinst):
66
instructionlist = []
7+
i = 0
78
for inst in splitinst:
8-
99
if(inst[0] == 'ADD'):
1010
if(inst[3][0].isdigit()): #imm or reg check if the first letter is a reg (L)
1111
imm = inst[3]
@@ -20,6 +20,7 @@ def createInstructions(splitinst):
2020
targetreg = inst[1][1:]
2121
targetreg = bin(int(targetreg))[2:].zfill(3)
2222
instructionlist.append('00' + str(imm) + str(sourcereg) + str(targetreg) + str(alusrc))
23+
i += 1
2324

2425
elif(inst[0] == 'SUB'):
2526
if(inst[3][0].isdigit()): #imm or reg
@@ -34,6 +35,7 @@ def createInstructions(splitinst):
3435
targetreg = inst[1][1:]
3536
targetreg = bin(int(targetreg))[2:].zfill(3)
3637
instructionlist.append('01' + str(imm) + str(sourcereg) + str(targetreg) + str(alusrc))
38+
i += 1
3739

3840
elif(inst[0] == 'LOAD'): #takes 3 cycles and three different instrcutjons
3941
instruction1 = '00' + bin(int(inst[1][1:]))[2:].zfill(8) + '001010'#point
@@ -42,13 +44,29 @@ def createInstructions(splitinst):
4244
targetreg = bin(int(targetreg))[2:].zfill(3)
4345
instruction3 = '100000000000' + str(targetreg) + '0' #load
4446
instructionlist.append([instruction1, instruction2, instruction3])
47+
i += 1
4548

4649
elif(inst[0] == 'STORE'): # takes 2 cycles and 2 instrucitons
4750
sourcereg = inst[1][1:]
4851
sourcereg = bin(int(sourcereg))[2:].zfill(2)
4952
instruction1 = '00' + bin(int(inst[2]))[2:].zfill(8) + '001010'
5053
instruction2 = '1100000000' + str(sourcereg) + '0000'
5154
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+
5270
return instructionlist
5371

5472
if(__name__ == "__main__"):
@@ -60,36 +78,41 @@ def createInstructions(splitinst):
6078

6179
program = open('program.txt', 'r').readlines()
6280
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..
6582

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) + ": ")
6687
baseaddress = 0000 #line starts here in the instruction file, each line has 16 spots(0-15), next address is 0010
6788
# print(program)
6889
instlist = []
6990
for line in program: # putting all instructions ina list
70-
91+
7192
if(line.strip() == ""):# skip empty lines
7293
continue
7394
line_content = line.split("//")[0].strip()#remove comments and any whitespace
7495

7596
if line_content: # only addcontent to the list
7697
instlist.append(line_content)
7798

78-
#print(instlist)
99+
print(instlist)
79100

80101
splitinsts = []
81102
for inst in instlist: #seperates each isntructions into its individual parts
82103
inst = inst.split()
83104
splitinsts.append(inst)
84105

85-
#print(splitinsts)
106+
print(splitinsts)
86107
instructions = createInstructions(splitinsts)
87108
#print(instructions)
88109
i = 0
89110
instructionfile.write(str(baseaddress).zfill(4) + ": ")
90111
for instruction in instructions:
91112
if(i == 16):
92113
instructionfile.write("\n" + str(baseaddress + 10).zfill(4) + ": ")
114+
elif(instruction == '.data'):
115+
pass
93116
elif(type(instruction) == list):
94117
for x in instruction:
95118
print(hex(int(x, 2))[2:].zfill(4), x)

datafile.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
v3.0 hex words addressed
2+
00: 0 6

program.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.text
12

23
LOAD L0 0 //loading the registers with values stored in the data memory
34
LOAD L1 1
@@ -12,6 +13,6 @@ STORE L0 2 //storing data in specified memory location
1213
STORE L1 3
1314

1415
.data
15-
4, 0 //4 is stored at 0 in data mem ...
16-
6, 1
16+
4 0 //4 is stored at 0 in data mem ...
17+
6 1
1718

0 commit comments

Comments
 (0)