+/TECS
git clone https://git.julia.blue/+/TECS
8cdba98
master
1# ASM to HACK assembler by Joseph Ryan 2import sys 3 4if len (sys .argv )> 1 : 5# Opens file 6fileName = sys .argv [- 1 ] 7if not fileName .endswith (".asm" ): 8fileName += ".asm" 9f = open (fileName ) 10 11if '-m' not in sys .argv : 12"Input file opened, parsing..." 13 14# Initializes symbol table 15symbols = {"SP" :0 ,"LCL" :1 ,"ARG" :2 ,"THIS" :3 ,"THAT" :4 ,"SCREEN" :16384 ,"KBD" :24576 ,"R0" :0 ,"R1" :1 ,"R2" :2 ,"R3" :3 ,"R4" :4 ,"R5" :5 ,"R6" :6 ,"R7" :7 ,"R8" :8 ,"R9" :9 ,"R10" :10 ,"R11" :11 ,"R12" :12 ,"R13" :13 ,"R14" :14 ,"R15" :15 } 16 17# Reads file into list 18program = [] 19for line in f .readlines (): 20if not (line == "\n" or line [0 ]== '/' ): 21if line .find ('/' )> - 1 : 22line = line [0 :line .find ('/' )] 23line = line .strip () 24program .append (line ) 25f .close () 26 27if '-m' not in sys .argv : 28"Reading symbols..." 29 30# Adds jump symbols to table 31i = 0 32j = 16 33for command in program : 34if command .startswith ('(' ): 35symbols [command [1 :- 1 ]]= i 36i -= 1 37i += 1 38 39# Adds A instruction symbols to table, and replaces symbols with numeric equivilants 40for command in program : 41if command .startswith ('@' ): 42if not command [1 :].isdigit (): 43if symbols .has_key (command [1 :]): 44command = '@' + str (symbols .get (command [1 :])) 45else : 46symbols [command [1 :]]= j 47j += 1 48 49if '-m' not in sys .argv : 50"Assembling code..." 51 52# Translates instructions into hack op codes 53hack = [] 54for command in program : 55# A instructions 56if command .startswith ('@' ): 57load = command .split ('@' )[1 ] 58if not load .isdigit (): 59load = str (symbols [load ]) 60hack .append (bin (int (load ))[2 :]) 61hack [len (hack )- 1 ]= hack [len (hack )- 1 ].zfill (16 ) 62# C instructions 63elif not command .startswith ('(' ): 64hack .append ("111" ) 65if command .find ('=' )> - 1 : 66comp = command .split ('=' )[1 ] 67else : 68comp = command 69if command .find (';' )> - 1 : 70comp = comp .split (';' )[0 ] 71# Computation 72hack [len (hack )- 1 ]+= str (int (comp .find ('M' )> - 1 )) 73if comp == '0' : 74hack [len (hack )- 1 ]+= "101010" 75if comp == '1' : 76hack [len (hack )- 1 ]+= "111111" 77if comp == "-1" : 78hack [len (hack )- 1 ]+= "111010" 79if comp == 'D' : 80hack [len (hack )- 1 ]+= "001100" 81if comp == 'A' or comp == 'M' : 82hack [len (hack )- 1 ]+= "110000" 83if comp == "!D" : 84hack [len (hack )- 1 ]+= "001101" 85if comp == "!A" or comp == "!M" : 86hack [len (hack )- 1 ]+= "110001" 87if comp == "-D" : 88hack [len (hack )- 1 ]+= "001101" 89if comp == "-A" or comp == "-M" : 90hack [len (hack )- 1 ]+= "110001" 91if comp == "D+1" : 92hack [len (hack )- 1 ]+= "011111" 93if comp == "A+1" or comp == "M+1" : 94hack [len (hack )- 1 ]+= "110111" 95if comp == "D-1" : 96hack [len (hack )- 1 ]+= "001110" 97if comp == "A-1" or comp == "M-1" : 98hack [len (hack )- 1 ]+= "110010" 99if comp == "D+A" or comp == "D+M" : 100hack [len (hack )- 1 ]+= "000010" 101if comp == "D-A" or comp == "D-M" : 102hack [len (hack )- 1 ]+= "010011" 103if comp == "A-D" or comp == "M-D" : 104hack [len (hack )- 1 ]+= "000111" 105if comp == "D&A" or comp == "D&M" : 106hack [len (hack )- 1 ]+= "000000" 107if comp == "D|A" or comp == "D|M" : 108hack [len (hack )- 1 ]+= "010101" 109# Destination 110if command .find ('=' )> - 1 : 111dest = command .split ('=' )[0 ] 112hack [len (hack )- 1 ]+= str (int (dest .find ('A' )> - 1 )) 113hack [len (hack )- 1 ]+= str (int (dest .find ('D' )> - 1 )) 114hack [len (hack )- 1 ]+= str (int (dest .find ('M' )> - 1 )) 115else : 116hack [len (hack )- 1 ]+= "000" 117# Jump 118if command .find (';' )> - 1 : 119jmp = command .split (';' )[1 ][1 :3 ] 120if jmp == "GT" : 121hack [len (hack )- 1 ]+= "001" 122if jmp == "EQ" : 123hack [len (hack )- 1 ]+= "010" 124if jmp == "GE" : 125hack [len (hack )- 1 ]+= "011" 126if jmp == "LT" : 127hack [len (hack )- 1 ]+= "100" 128if jmp == "NE" : 129hack [len (hack )- 1 ]+= "101" 130if jmp == "LE" : 131hack [len (hack )- 1 ]+= "110" 132if jmp == "MP" : 133hack [len (hack )- 1 ]+= "111" 134else : 135hack [len (hack )- 1 ]+= "000" 136 137if '-m' not in sys .argv : 138"Writing output to file..." 139 140# Write resulting binary 141f = open (fileName [:- 3 ]+ "hack" ,"w" ) 142for line in hack : 143if '-p' in sys .argv : 144line 145f .write (line + "\n" ) 146if '-m' not in sys .argv : 147"Finished!" 148 149else : 150"usage: assembler.py sourceFile[.asm]\noptions:\n\t-m mutes status messages\n\t-p prints output to stdout"