Skip to content

Instantly share code, notes, and snippets.

@justinrixx
Created January 10, 2017 21:16
Show Gist options
  • Save justinrixx/cc08874956f8387798992151f72badba to your computer and use it in GitHub Desktop.
Save justinrixx/cc08874956f8387798992151f72badba to your computer and use it in GitHub Desktop.
import sys
"""
These are some look up tables used in translating the words into
hex. They are hash tables that correspond from one string to another
"""
commands = {
'NOP' : '0',
'MOVIR0' : '1',
'MOVIR1' : '2',
'DISPLAY': '3',
'MOVAR0' : '4',
'MOVAR1' : '5',
'ADD' : '6',
'AND' : '7',
'OR' : '8',
'XOR' : '9'
}
def main(argv):
# make sure to provide a filename
if len(argv) < 2:
print("Error: Please specify an input filename")
return
# open the input and output files
infile = open(argv[1], 'r')
outfile = open('a.out', 'w')
# do one line at a time
line = infile.readline()
while line != '':
# split into the command and the argument
parts = line.split()
# get the hex for the command given
outfile.write(commands[parts[0]])
# add the arg or a 0
if len(parts) > 1:
# this fancy dancy stuff converts the string into binary
# http://stackoverflow.com/a/5092707/3861623
outfile.write(hex(int(parts[1], 2))[2])
else:
outfile.write('0')
outfile.write('\n')
# continue to the next line
line = infile.readline()
# This is here to ensure main is only called when
# this file is run, not just loaded
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment