Skip to content

Instantly share code, notes, and snippets.

@axiomsofchoice
Created September 8, 2017 09:50
Show Gist options
  • Save axiomsofchoice/69a6f555d94d24e6b524e2108c510875 to your computer and use it in GitHub Desktop.
Save axiomsofchoice/69a6f555d94d24e6b524e2108c510875 to your computer and use it in GitHub Desktop.
"""
Simple EDSAC Assembler.
The MIT Liecense.
Copyright (C) Daniel Hagon, 2017.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
be_verbose = False
import re
f = open("fib.txt", "r")
# Format is:
# LABEL: OPCODE OPERAND ; comment
# Where OPERAND must refer to a comment
p = re.compile('((([A-Z][A-Z0-9_]*):)?\s+([A-Z@])\s+(([A-Z][A-Z0-9_]*)|([0-9]+))\s*)?(;.*)?')
instructions = []
orig_line = 0
# Parse each line in term.
for ml in f.readlines():
m = p.match(ml)
if m is not None:
code, _, label, opcode, operand, sym_op, num_op, comment = m.groups()
if opcode is not None:
isnumeric = num_op is not None
instructions.append((orig_line, label, opcode, operand, isnumeric))
if be_verbose:
print instructions[-1]
else:
raise Exception("Could not parse line %d" % orig_line)
orig_line = orig_line + 1
# Initial offset, assuming initial orders is 36 instructions long
INITIAL_OFFSET = 37
# Create a lookup table for symbolic identifiers.
sym_vals = {}
for s in range(len(instructions)):
_, label, _, _, _ = instructions[s]
if label is not None:
sym_vals[label] = s + INITIAL_OFFSET
if be_verbose:
print sym_vals
print
# Generate the machine code.
for line_num, label, opcode, operand, isnumeric in instructions:
try:
if isnumeric:
print "%s %s" % (opcode, operand)
else:
print "%s %s" % (opcode, sym_vals[operand])
except KeyError as e:
print "Identifier lookup error on line %d %s" % (line_num, e)
exit(0)
except Exception as e:
print "Error on line %d %s" % (line_num, e)
exit(0)
;The MIT Liecense.
;Copyright (C) Daniel Hagon, 2017.
;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
;The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
; simple EDSAC hello world prints the number 7 as ASCII and halts
START: T END ; initial orders requires us to tell it about the end of the tape
O VAL_TO_PRNT ; prints the character at this location
HALT: Z 1 ; halts the program
VAL_TO_PRNT: @ 55 ; this is ASCII code for '7'
END: @ 0 ; sentinel at the end of the tape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment