-
Notifications
You must be signed in to change notification settings - Fork 6
/
assemble.py
executable file
·60 lines (48 loc) · 1.82 KB
/
assemble.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
#!/usr/bin/env python
import argparse
import sys
import re
import mips
from register import Register
from instruction import Instruction
import instruction
parser = argparse.ArgumentParser(description="A very small MIPS assembler.")
parser.add_argument('filename')
parser.add_argument('-t', '--text_base', default=0,
help="Base location of code", metavar="addr")
parser.add_argument('-d', '--data_base', default=0x4000,
help="Base location of data", metavar="addr")
parser.add_argument('-o', '--output', default=argparse.SUPPRESS,
help="Output file", metavar="filename")
parser.add_argument('-p', '--data_out', default=argparse.SUPPRESS,
help="Output file for data section", metavar="filename")
parser.add_argument('-l', '--littleendian', default=False, action='store_true',
help="Output as little endian")
parser.add_argument('-v', '--verbose', default=False, action='store_true',
help="Be verbose")
args = vars(parser.parse_args())
f = open(args['filename'])
lines = f.readlines()
lines = [x.replace("\n", "") for x in lines]
f.close()
mp = mips.MIPSProgram(text_base=args['text_base'], data_base=args['data_base'])
mp.AddLines(lines)
output = open(args['output'], 'w') if 'output' in args else None
endianness = "little" if args['littleendian'] else "top"
if 'output' in args:
with open(args['output'], 'w') as out:
print "Writing text to '%s'..."%(args['output']),
bytes = mp.Bytes(endian=endianness)
for b in bytes:
out.write("%c"%(b,))
print "done!"
if 'data_out' in args:
with open(args['data_out'], 'w') as out:
print "Writing data to '%s'..."%(args['data_out']),
for s in mp.data:
out.write(s)
print "done!"
if 'verbose' in args or 'output' not in args:
binary = mp.Bytes(endian=endianness)
for j in range(len(binary)/4):
print "%02x %02x %02x %02x"%tuple(binary[j*4:j*4+4])