Skip to content
Browse files

Merge pull request #81 from ghost/master

improve formatting
  • Loading branch information...
2 parents 08e28e2 + ef105d1 commit 5f360f63ce98e68945723e1764237c156624d519 @jtauber committed
Showing with 52 additions and 20 deletions.
  1. +52 −20 asm.py
View
72 asm.py
@@ -69,8 +69,40 @@ def operand_re(prefix):
$""", re.X)
-IDENTIFIERS = {"A": 0x0, "B": 0x1, "C": 0x2, "X": 0x3, "Y": 0x4, "Z": 0x5, "I": 0x6, "J": 0x7, "POP": 0x18, "PEEK": 0x19, "PUSH": 0x1a, "SP":0x1b, "PC": 0x1C, "O": 0x1D}
-OPCODES = {"SET": 0x1, "ADD": 0x2, "SUB": 0x3, "MUL": 0x4, "DIV": 0x5, "MOD": 0x6, "SHL": 0x7, "SHR": 0x8, "AND": 0x9, "BOR": 0xA, "XOR": 0xB, "IFE": 0xC, "IFN": 0xD, "IFG": 0xE, "IFB": 0xF}
+IDENTIFIERS = {
+ "A": 0x0,
+ "B": 0x1,
+ "C": 0x2,
+ "X": 0x3,
+ "Y": 0x4,
+ "Z": 0x5,
+ "I": 0x6,
+ "J": 0x7,
+ "POP": 0x18,
+ "PEEK": 0x19,
+ "PUSH": 0x1a,
+ "SP": 0x1b,
+ "PC": 0x1C,
+ "O": 0x1D
+}
+
+OPCODES = {
+ "SET": 0x1,
+ "ADD": 0x2,
+ "SUB": 0x3,
+ "MUL": 0x4,
+ "DIV": 0x5,
+ "MOD": 0x6,
+ "SHL": 0x7,
+ "SHR": 0x8,
+ "AND": 0x9,
+ "BOR": 0xA,
+ "XOR": 0xB,
+ "IFE": 0xC,
+ "IFN": 0xD,
+ "IFG": 0xE,
+ "IFB": 0xF
+}
def clamped_value(l):
@@ -78,17 +110,17 @@ def clamped_value(l):
ADDR_MAP = {
- "register": lambda t,v: (IDENTIFIERS[t.upper()], None),
- "register_indirect": lambda t,v: (0x08 + IDENTIFIERS[t.upper()], None),
- "hex_indexed_index": lambda t,v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
- "decimal_indexed_index": lambda t,v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
- "label_indexed_index": lambda t,v: (0x10 + IDENTIFIERS[t.upper()], v),
- "hex_indirect": lambda t,v: (0x1E, int(t, 16)),
- "decimal_indirect": lambda t,v: (0x1E, int(t)),
- "hex_literal": lambda t,v: clamped_value(int(t, 16)),
- "decimal_literal": lambda t,v: clamped_value(int(t)),
- "label_indirect": lambda t,v: (0x1E, t),
- "label": lambda t,v: (0x1F, t),
+ "register": lambda t, v: (IDENTIFIERS[t.upper()], None),
+ "register_indirect": lambda t, v: (0x08 + IDENTIFIERS[t.upper()], None),
+ "hex_indexed_index": lambda t, v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
+ "decimal_indexed_index": lambda t, v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
+ "label_indexed_index": lambda t, v: (0x10 + IDENTIFIERS[t.upper()], v),
+ "hex_indirect": lambda t, v: (0x1E, int(t, 16)),
+ "decimal_indirect": lambda t, v: (0x1E, int(t)),
+ "hex_literal": lambda t, v: clamped_value(int(t, 16)),
+ "decimal_literal": lambda t, v: clamped_value(int(t)),
+ "label_indirect": lambda t, v: (0x1E, t),
+ "label": lambda t, v: (0x1F, t),
}
@@ -108,10 +140,10 @@ def report_error(filename, lineno, error):
parser.add_argument("-o", default="a.obj", help="Place the output into FILE", metavar="FILE")
parser.add_argument("input", help="File with DCPU assembly code")
args = parser.parse_args()
-
+
program = []
labels = {}
-
+
for lineno, line in enumerate(open(args.input), start=1):
if lineno == 1:
line = line.lstrip(codecs.BOM_UTF8)
@@ -120,15 +152,15 @@ def report_error(filename, lineno, error):
if mo is None:
report_error(args.input, lineno, "Syntax error: '%s'" % line.strip())
break
-
+
token_dict = mo.groupdict()
if token_dict is None:
report_error(args.input, lineno, "Syntax error: '%s'" % line.strip())
break
-
+
if token_dict["label"] is not None:
labels[token_dict["label"]] = len(program)
-
+
o = x = y = None
if token_dict["basic"] is not None:
o = OPCODES[token_dict["basic"].upper()]
@@ -145,14 +177,14 @@ def report_error(filename, lineno, error):
program.append(int(datum[2:], 16))
else:
program.append(int(datum))
-
+
if o is not None:
program.append(((b << 10) + (a << 4) + o))
if x is not None:
program.append(x)
if y is not None:
program.append(y)
-
+
try:
with open(args.o, "wb") as f:
for word in program:

0 comments on commit 5f360f6

Please sign in to comment.
Something went wrong with that request. Please try again.