Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rumple
Created August 22, 2013 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rumple/6313789 to your computer and use it in GitHub Desktop.
Save rumple/6313789 to your computer and use it in GitHub Desktop.
Solution to Reddit's r/dailyprogrammer challenge #132. http://redd.it/1kqxz9
#!/usr/bin/env python
import sys
idx = 0
lit = 1
ops = {
("and", (idx, idx)): 0x0,
("and", (idx, lit)): 0x1,
("or", (idx, idx)): 0x2,
("or", (idx, lit)): 0x3,
("xor", (idx, idx)): 0x4,
("xor", (idx, lit)): 0x5,
("not", (idx)): 0x6,
("mov", (idx, idx)): 0x7,
("mov", (idx, lit)): 0x8,
("random", (idx,)): 0x9,
("add", (idx, idx)): 0xA,
("add", (idx, lit)): 0xB,
("sub", (idx, idx)): 0xC,
("sub", (idx, lit)): 0xD,
("jmp", (idx,)): 0xE,
("jmp", (lit,)): 0xF,
("jz", (idx, idx)): 0x10,
("jz", (idx, lit)): 0x11,
("jz", (lit, idx)): 0x12,
("jz", (lit, lit)): 0x13,
("jeq", (idx, idx, idx)): 0x14,
("jeq", (lit, idx, idx)): 0x15,
("jeq", (idx, idx, lit)): 0x16,
("jeq", (lit, idx, lit)): 0x17,
("jls", (idx, idx, idx)): 0x18,
("jls", (lit, idx, idx)): 0x19,
("jls", (idx, idx, lit)): 0x1A,
("jls", (lit, idx, lit)): 0x1B,
("jgt", (idx, idx, idx)): 0x1C,
("jgt", (lit, idx, idx)): 0x1D,
("jgt", (idx, idx, lit)): 0x1E,
("jgt", (lit, idx, lit)): 0x1F,
("halt", ()): 0xFF,
("aprint", (idx,)): 0x20,
("aprint", (lit,)): 0x21,
("dprint", (idx,)): 0x22,
("dprint", (lit,)): 0x23,
}
def assemble(op, *args):
arg_type = lambda x: idx if "[" in x else lit
val = lambda x: int(x[1:-1], 0) if "[" in x else int(x, 0)
arg_types = [arg_type(t) for t in args]
vals = [ops[(op, tuple(arg_types))]]
vals += [val(x) for x in args]
return " ".join(["0x%02X" % x for x in vals])
def main():
with file(sys.argv[1]) as f:
lines = f.readlines()
for line in lines:
args = [x.lower().strip() for x in line.split()]
print assemble(*args)
if __name__ == '__main__':
main()
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# 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 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.
# For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment