-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.py
108 lines (104 loc) · 5.3 KB
/
assembler.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
import array as arr
#! python Nagmat Nazarov 1002186972
# (c) DL, UTA, 2009 - 2018
import sys, string
wordsize = 24 # everything is a word
numregbits = 3 # actually +1, msb is indirect bit
opcodesize = 5
memloadsize = 1024 # change this for larger programs
numregs = 2**numregbits
opcposition = wordsize - (opcodesize + 1) # shift value to position opcode
reg1position = opcposition - (numregbits + 1) # first register position
reg2position = reg1position - (numregbits + 1)
memaddrimmedposition = reg2position # mem address or immediate same place as reg2
startexecptr = 0;
filename = 'in.asm'
sb = [0,2,3,4,5,6,7,8]
print("sb = ",sb)
def regval (rstr): # help with reg or indirect addressing
if rstr.isdigit():
return (int(rstr))
elif rstr[0] == '*':
return (int(rstr[1:]) + (1<<numregbits))
else:
return 0 # should not happen
mem = [0] * memloadsize # this is the memory load executable
# instruction mnemonic, type: (1 reg, 2 reg, reg+addr, immed, pseudoop), opcode
opcodes = {'add': (2, 1),'sub': (2, 2), # ie, "add" is a type 2 instruction, opcode = 1
'dec': ( 1, 3), 'inc': ( 1, 4 ),
'ld': (3, 7), 'st': (3, 8), 'ldi': (3, 9),
'bnz': (3, 12), 'brl': (3, 13),
'ret': ( 1, 14 ),
'int': (3, 16), 'sys': (3, 16), # syscalls are same as interrupts
'dw': (4, 0), 'go':(3, 0), 'end': (0, 0) } # pseudo ops
curaddr = 0 # start assembling to location 0
#for line in open(sys.argv[1], 'r').readlines(): # command line
infile = open(filename, 'r')
# Build Symbol Table
symboltable = {}
for line in infile.readlines(): # read our asm code
tokens = line.lower().split( )# tokens on each line
print("Tokens = {} = ".format(tokens))
firsttoken = tokens[0]
print("First token ={}=".format(firsttoken))
print(tokens)
if firsttoken.isdigit(): # if line starts with an address
curaddr =int(tokens[0]) # assemble to here
print("Cur addr = {}".format(curaddr))
tokens = tokens[1:]
print("Tokens1:0 = {} = ".format(tokens))
if firsttoken==';': # skip comments
continue
if firsttoken=='go': # start execution here
startexecptr = ( int(tokens[1]) & ((2**wordsize)-1)) # data
continue
if firsttoken[0]=='.':
print("Adding element to symboltable")
symboltable[firsttoken] = curaddr
curaddr = curaddr + 1
infile.close()
infile = open(filename, 'r')
for line in infile.readlines(): # read our asm code
tokens = line.lower().split( ) # tokens on each line
firsttoken = tokens[0]
if firsttoken.isdigit(): # if line starts with an address
curaddr = int(tokens[0]) # assemble to here
tokens = tokens[1:]
if firsttoken == ';': # skip comments
continue
if firsttoken == 'go': # start execution here
startexecptr = ( int(tokens[1]) & ((2**wordsize)-1)) # data
continue
if firsttoken[0]=='.':
symaddr = symboltable[firsttoken]
tokens = tokens[1:]
memdata = 0 # build instruction step by step
instype = opcodes[tokens[0]][0]
memdata = (opcodes[tokens[0]][1]) << opcposition # put in opcode
#print("\ntokens1 ={}, instype={} memdata = {} ".format(tokens[1], instype,memdata)) # DEBUG
if instype == 4: # dw type
memdata = (int(tokens[1]) & ((2**wordsize)-1)) # data is wordsize long
elif instype == 0: # end type
memdata = memdata
elif instype == 1: # dec,inc type, one reg
memdata = memdata + (regval(tokens[1] ) << reg1position)
elif instype == 2: # add, sub type, two regs
memdata = memdata + (regval(tokens[1] ) << reg1position ) + ( regval( tokens[2] ) << reg2position)
elif instype == 3: # ld,st type
token2 = tokens[2]
if token2.isdigit():
memaddr=int(tokens[2])
else:
memaddr=symboltable[token2]
memdata = memdata + (regval(tokens[1])<<reg1position)+memaddr
#if opcodes == 8:
#print("Tokens1 ={}, tokens2= {} , memdata={},memaddr = {} , symboltable={}, ".format(tokens[1],tokens[2],memdata,memaddr,symboltable))
mem[curaddr]=memdata # memory image at the current location
curaddr=curaddr + 1
outfile = open("a.out", 'w') # done, write it out
outfile.write( 'go ' + '%d' % startexecptr ) # start execution here
outfile.write( "\n" )
for i in range(memloadsize): # write memory image
outfile.write(hex(mem[i])+" " + '%d'%i)
outfile.write( "\n" )
outfile.close()