Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

improve formatting #81

Merged
merged 3 commits into from over 1 year ago

2 participants

James Tauber Check your git settings!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 52 additions and 20 deletions. Show diff stats Hide diff stats

  1. +52 20 asm.py
72 asm.py
@@ -68,8 +68,40 @@ def operand_re(prefix):
68 68 $""", re.X)
69 69
70 70
71   -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}
72   -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}
  71 +IDENTIFIERS = {
  72 + "A": 0x0,
  73 + "B": 0x1,
  74 + "C": 0x2,
  75 + "X": 0x3,
  76 + "Y": 0x4,
  77 + "Z": 0x5,
  78 + "I": 0x6,
  79 + "J": 0x7,
  80 + "POP": 0x18,
  81 + "PEEK": 0x19,
  82 + "PUSH": 0x1a,
  83 + "SP": 0x1b,
  84 + "PC": 0x1C,
  85 + "O": 0x1D
  86 +}
  87 +
  88 +OPCODES = {
  89 + "SET": 0x1,
  90 + "ADD": 0x2,
  91 + "SUB": 0x3,
  92 + "MUL": 0x4,
  93 + "DIV": 0x5,
  94 + "MOD": 0x6,
  95 + "SHL": 0x7,
  96 + "SHR": 0x8,
  97 + "AND": 0x9,
  98 + "BOR": 0xA,
  99 + "XOR": 0xB,
  100 + "IFE": 0xC,
  101 + "IFN": 0xD,
  102 + "IFG": 0xE,
  103 + "IFB": 0xF
  104 +}
73 105
74 106
75 107 def clamped_value(l):
@@ -77,17 +109,17 @@ def clamped_value(l):
77 109
78 110
79 111 ADDR_MAP = {
80   - "register": lambda t,v: (IDENTIFIERS[t.upper()], None),
81   - "register_indirect": lambda t,v: (0x08 + IDENTIFIERS[t.upper()], None),
82   - "hex_indexed_index": lambda t,v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
83   - "decimal_indexed_index": lambda t,v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
84   - "label_indexed_index": lambda t,v: (0x10 + IDENTIFIERS[t.upper()], v),
85   - "hex_indirect": lambda t,v: (0x1E, int(t, 16)),
86   - "decimal_indirect": lambda t,v: (0x1E, int(t)),
87   - "hex_literal": lambda t,v: clamped_value(int(t, 16)),
88   - "decimal_literal": lambda t,v: clamped_value(int(t)),
89   - "label_indirect": lambda t,v: (0x1E, t),
90   - "label": lambda t,v: (0x1F, t),
  112 + "register": lambda t, v: (IDENTIFIERS[t.upper()], None),
  113 + "register_indirect": lambda t, v: (0x08 + IDENTIFIERS[t.upper()], None),
  114 + "hex_indexed_index": lambda t, v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
  115 + "decimal_indexed_index": lambda t, v: (0x10 + IDENTIFIERS[t.upper()], int(v, 16)),
  116 + "label_indexed_index": lambda t, v: (0x10 + IDENTIFIERS[t.upper()], v),
  117 + "hex_indirect": lambda t, v: (0x1E, int(t, 16)),
  118 + "decimal_indirect": lambda t, v: (0x1E, int(t)),
  119 + "hex_literal": lambda t, v: clamped_value(int(t, 16)),
  120 + "decimal_literal": lambda t, v: clamped_value(int(t)),
  121 + "label_indirect": lambda t, v: (0x1E, t),
  122 + "label": lambda t, v: (0x1F, t),
91 123 }
92 124
93 125
@@ -107,24 +139,24 @@ def report_error(filename, lineno, error):
107 139 parser.add_argument("-o", default="a.obj", help="Place the output into FILE", metavar="FILE")
108 140 parser.add_argument("input", help="File with DCPU assembly code")
109 141 args = parser.parse_args()
110   -
  142 +
111 143 program = []
112 144 labels = {}
113   -
  145 +
114 146 for lineno, line in enumerate(open(args.input), start=1):
115 147 mo = line_regex.match(line)
116 148 if mo is None:
117 149 report_error(args.input, lineno, "Syntax error")
118 150 break
119   -
  151 +
120 152 token_dict = mo.groupdict()
121 153 if token_dict is None:
122 154 report_error(args.input, lineno, "Syntax error")
123 155 break
124   -
  156 +
125 157 if token_dict["label"] is not None:
126 158 labels[token_dict["label"]] = len(program)
127   -
  159 +
128 160 o = x = y = None
129 161 if token_dict["basic"] is not None:
130 162 o = OPCODES[token_dict["basic"].upper()]
@@ -141,14 +173,14 @@ def report_error(filename, lineno, error):
141 173 program.append(int(datum[2:], 16))
142 174 else:
143 175 program.append(int(datum))
144   -
  176 +
145 177 if o is not None:
146 178 program.append(((b << 10) + (a << 4) + o))
147 179 if x is not None:
148 180 program.append(x)
149 181 if y is not None:
150 182 program.append(y)
151   -
  183 +
152 184 try:
153 185 with open(args.o, "wb") as f:
154 186 for word in program:

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.