Skip to content

Commit 9f45caa

Browse files
committed
first commit
0 parents  commit 9f45caa

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/inputfile

assembler.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#Dominic Catena
2+
# I Pledge My Honor That I Have Abided By The Stevens Honor System
3+
import os
4+
5+
def convertInstruction(instructionName, imm_reg2, reg1, targetreg, alusrc):
6+
#machinecode = '000000000000000' 15 bits op|immediate/reg2|reg1|targetreg|alusrc
7+
opcodes = {'ADD' : '00', 'SUB' : '01', 'LOAD' : '10', 'STORE' : '11'}
8+
return opcodes[instructionName] + imm_reg2 + reg1 + targetreg + alusrc
9+
10+
11+
if(__name__ == "__main__"):
12+
if(os.path.exists("inputfile")): #remove files if the already exist
13+
os.remove("inputfile")
14+
if(os.path.exists("datafile")):
15+
os.remove('datafile')
16+
17+
18+
program = open('program.txt', 'r').readlines()
19+
instructionfile=open("inputfile", 'w') #write to the instruction mem file
20+
21+
instructionfile.write("v3.0 hex words addressed\n") #have to write the header
22+
23+
baseaddress = 0000 #line starts here in the instruction file, each line has 16 spots(0-15), next address is 0010
24+
# print(program)
25+
instlist = []
26+
for line in program:
27+
# Skip empty lines or lines with only a newline character
28+
if(line.strip() == ""):
29+
continue
30+
31+
# Remove comments and any leading/trailing whitespace
32+
line_content = line.split("//")[0].strip()
33+
34+
# Only add non-empty content to the list
35+
if line_content:
36+
instlist.append(line_content)
37+
38+
print(instlist)
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+

program.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
LOAD L0, 0x00 //loading the registers with values stored in the data memory
3+
LOAD L1, 0x01
4+
5+
ADD L0, L0, 2 //adding with an immediate value
6+
ADD L0, L0, L1 //adding with a register value
7+
8+
SUB L0, L0, 1 //subbing with an immediate value
9+
SUB L1, L1, L0 //subbing with a register value
10+
11+
STORE L0, 0x02 //storing data in specified memory location
12+
STORE L1, 0x03
13+
14+
15+
.data
16+
4, 0x00
17+
6, 0x01
18+

0 commit comments

Comments
 (0)