Permalink
Browse files

More work, on lots of stuff

  • Loading branch information...
Mause committed Apr 19, 2012
1 parent f3c11c0 commit 490e40f2ef08d026d319e0405081580356de4fb8
Showing with 67 additions and 32 deletions.
  1. +2 −2 Mausembler.py
  2. +62 −28 mausembler/assembler.py
  3. +2 −1 mausembler/custom_errors.py
  4. +1 −1 test.bat
View
@@ -3,5 +3,5 @@
from mausembler import Assembler
test = Assembler(sys.argv)
test.load()
test = Assembler()
test.load(sys.argv[1], sys.argv[2])
View
@@ -2,14 +2,16 @@
# This is a Quick & Dirty assembler :P
import sys
import os
import io
from custom_errors import *
from mausembler.custom_errors import DuplicateLabelError
from mausembler.custom_errors import FileNonExistantError
from mausembler.custom_errors import FileExistsError
class Assembler():
def __init__(self, filename='null.txt', data=[]):
def __init__(self): #, data=[]):
self.ops = {'SET': 0x1, 'ADD': 0x2, 'SUB': 0x3, 'MUL': 0x4,
'DIV': 0x5, 'MOD': 0x6, 'SHL': 0x7, 'SHR': 0x8,
'AND': 0x9, 'BOR': 0xa, 'XOR': 0xb, 'IFE': 0xc,
@@ -18,40 +20,72 @@ def __init__(self, filename='null.txt', data=[]):
'C': 0x2, 'X': 0x3,
'Y': 0x4, 'Z': 0x5,
'I': 0x6, 'J': 0x7}
self.input_filename = filename[1]
self.output_filename = filename[2]
self.input_filename = ''
self.output_filename = ''
self.dependencies = []
self.dep_path=[]
self.labels = {}
self.data=''
print "Mausembler; self titled!\n"
def determine_dependencies(self, data):
self.dependencies=[]
for line in data:
if line[0] == '.':
if line.split()[0] == '.include':
self.dependencies.append(''.join(line.split()[1:]))
def load(self):
if os.path.exists(self.input_filename):
fh = open(self.input_filename, 'rb')
self.data = fh.readlines()
fh.close()
else:
print 'are you sure that file exists?'
raise FileNonExistantError(self.input_filename)
if os.path.exists(self.output_filename):
self.dependencies.append(
((''.join(line.split()[1:])).strip('"')).strip("'"))
for dep in self.dependencies:
self.load(dep, ''.join(dep.split('.')[:-1])+'bin')
#self.dep_path.append('\\'.join(input_filename.split('\\')[:-1]))
def load(self, input_filename='null.txt', output_filename='null.bin'):
self.dep_path.append('\\'.join(input_filename.split('\\')[:-1]))
print 'self.dep_path:', str(self.dep_path)
cur_input_filename = input_filename
print 'manual:',os.getcwd()+input_filename
if os.path.exists(os.getcwd()+'\\'+input_filename):
input_filename = os.getcwd()+'\\'+input_filename
while not os.path.exists(cur_input_filename):
for x in range(len(self.dep_path)):
print 'Poss'
possibles=[(self.dep_path[x]+'\\'+cur_input_filename),
(self.dep_path[x]+'\\'+cur_input_filename.split('\\')[-1]),
(os.getcwd()+'\\'+self.dep_path[x]+'\\'+cur_input_filename),
(os.getcwd()+'\\'+self.dep_path[x]+'\\'+cur_input_filename.split('\\')[-1])]
for poss in possibles:
print 'Poss:',poss
print os.path.exists(poss)
if os.path.exists(poss):
input_filename = poss
break
# if os.path.exists(self.dep_path[x]+'\\'+cur_input_filename.split('\\')[-1]):
# input_filename = self.dep_path[x]+'\\'+cur_input_filename
# break
# if os.path.exists(os.getcwd()+'\\'+self.dep_path[x]+'\\'+cur_input_filename):
# input_filename = self.dep_path[x]+'\\'+cur_input_filename
# break
# if os.path.exists(os.getcwd()+'\\'+self.dep_path[x]+'\\'+cur_input_filename.split('\\')[-1]):
# input_filename = self.dep_path[x]+'\\'+cur_input_filename
# break
print 'are you sure that file exists?\n\n'
raise FileNonExistantError(input_filename)
if os.path.exists(cur_input_filename):
FH = open(input_filename, 'rb')
self.data = FH.readlines()
FH.close()
if os.path.exists(output_filename):
cont = raw_input('Output file exists. Overwrite? ')
if cont.lower() in ['yes', 'y']:
self.output_file = io.open(self.output_filename, 'wb')
self.output_file = io.open(output_filename, 'wb')
else:
print 'Exiting...'
raise FileExistsError(self.input_filename)
raise FileExistsError(self.output_filename)
else:
file = open(self.output_filename, 'w')
file.write('')
file.close()
del file
self.output_file = io.open(self.output_filename, 'wb')
FH = open(output_filename, 'w')
FH.write('')
FH.close()
del FH
self.output_file = io.open(output_filename, 'wb')
self.determine_dependencies(self.data)
self.labels = {}
print '\nThe cpu will be told to;'
for self.line_number in range(len(self.data)):
line = self.data[self.line_number]
@@ -70,7 +104,7 @@ def parse(self, opcode):
if opcode[0][0] == ':':
if opcode[0][1:] not in self.labels:
print '* remember line', str(self.line_number),
print 'as label "', opcode[0][1:], '"'
print 'as label "' + opcode[0][1:] + '"'
self.labels[opcode[0][1:]] = self.line_number
else:
raise DuplicateLabelError(opcode[0][1:])
@@ -82,7 +116,7 @@ def parse(self, opcode):
self.output_file.write(str(self.ops[opcode[0]]))
if opcode[0] == 'SUB':
print '* set', opcode[1], 'to', opcode[1], '-', opcode[2]
self.output_file.write(str(self.ops[opcode[0]]))
self.output_file.write(str(self.ops[opcode[0]]))
if opcode[0] == 'MUL':
print '* set', opcode[1], 'to', opcode[1], '*', opcode[2]
self.output_file.write(str(self.ops[opcode[0]]))
@@ -91,7 +125,7 @@ def parse(self, opcode):
self.output_file.write(str(self.ops[opcode[0]]))
if opcode[0] == 'MOD':
print '* set', opcode[1], 'to', opcode[1], '%', opcode[2]
self.output_file.write(str(self.ops[opcode[0]]))
self.output_file.write(str(self.ops[opcode[0]]))
return processed
else:
print '* do nothing for this line'
@@ -1,3 +1,4 @@
import os
class DuplicateLabelError(Exception):
def __init__(self, value):
self.value = value
@@ -8,7 +9,7 @@ class FileNonExistantError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return 'The file "'+self.value+'" cannot be found'
return 'The file "'+self.value+'" cannot be found in the directory '+os.getcwd()
class FileExistsError(Exception):
def __init__(self, value):
View
@@ -2,4 +2,4 @@ cls
rem python Mausembler.py test.dasm test.out
rem python Mausembler.py ilog.dasm ilog.bin
python Mausembler.py 0x42c.dasm16 0x42c.bin
python Mausembler.py src\0x42c.dasm16 0x42c.bin

0 comments on commit 490e40f

Please sign in to comment.