print("MMC assembler") import sys args=sys.argv if(len(args)==1): args.append(input("input filename:")) file=open(args[1]) filedat=file.read() file.close() split=False tmp="" tokens=[] for i in filedat: if i=='"': split=not split if(tmp!=""): tokens.append(tmp) tmp="" if split: tokens.append('"') elif (i.isspace() or i==',') and not split: if(tmp!=""): tokens.append(tmp) tmp="" else: tmp+=i tokens.append(tmp) oldtokens=tokens names={} location=0 progstart=0 i=0 registers=["pc","ax","bx","cx","dx","sp","si","di"] reg8=["al","ah","bl","bh","cl","ch","dl","dh"] while i=0): codesize=int(tokens[i].replace("%bits",''))""" elif tokens[i]=="jmp": i=i+1 if(tokens[i] in registers): location+=1 else: location+=3 elif tokens[i]=="jpz": i=i+1 if(tokens[i] in reg8): location+=3 else: print("'"+tokens[i]+"' is not a valid 8-bit register for JPZ opcode.") i+=1 elif tokens[i]=="sub": i=i+2 if(tokens[i] in reg8)or tokens[i] in registers: location+=2 else: location+=3 elif tokens[i]=="call": location+=3 i+=1 elif tokens[i]=='hlt': location+=1 elif tokens[i]=='ldr': location+=2 i+=2 elif tokens[i]=='ldw': location+=2 i+=2 elif tokens[i]=='kbd': i+=1 location+=1 elif tokens[i]=='wrt': i+=1 location+=1 elif tokens[i]=='ret': location+=1 elif tokens[i]=='rtz': location+=1 elif tokens[i]=='lod': location+=1 elif tokens[i]=='sto': location+=1 elif tokens[i]=='ld': i+=2 location+=3 else: print("error: invalid:"+tokens[i]) tokens.remove(tokens[i]) i-=1 i+=1 i=0 location=0 out=[] while i>8) out.append(location&255) location+=3 elif(tokens[i] in names.keys()): out.append(0xC3) out.append(names[tokens[i]]>>8) out.append(names[tokens[i]]&255) location+=3 else: out.append(0xC3) out.append(int(tokens[i])>>8) out.append(int(tokens[i])&255) location+=3 elif tokens[i]=="jpz": i=i+1 if(tokens[i] in reg8): out.append(0xD8+reg8.index(tokens[i])) i+=1 if(tokens[i] in registers): print("'jpz' does not support registers as addresses.") elif(tokens[i]=='$'): out.append(location>>8) out.append(location&255) elif(tokens[i] in names.keys()): out.append(names[tokens[i]]>>8) out.append(names[tokens[i]]&255) else: out.append(int(tokens[i])>>8) out.append(int(tokens[i])&255) location+=3 elif tokens[i]=="call": i=i+1 if(tokens[i] in registers): print("'call' does not support registers. use 'push "+tokens[i]+"' and 'ret'") elif(tokens[i]=='$'): out.append(0xC4) out.append(location>>8) out.append(location&255) location+=3 elif(tokens[i] in names.keys()): out.append(0xC4) out.append(names[tokens[i]]>>8) out.append(names[tokens[i]]&255) location+=3 else: out.append(0xC4) out.append(int(tokens[i])>>8) out.append(int(tokens[i])&255) location+=3 elif tokens[i]=="sub": i=i+1 if(tokens[i] in reg8): i+=1 if(tokens[i] in reg8): out.append(0x4E) out.append((reg8.index(tokens[i-1])<<3)|reg8.index(tokens[i])) location-=1 elif(tokens[i]=='$'): print("'$' used for 16-bit value in an 8-bit instruction.") elif(tokens[i] in names.keys()): out.append(2) out.append(reg8.index(tokens[i-1])) out.append(names[tokens[i]]&255) else: out.append(2) out.append(reg8.index(tokens[i-1])) out.append(int(tokens[i])&255) location+=3 elif(tokens[i] in registers): i+=1 if(tokens[i] in registers): out.append(0x4F) out.append((registers.index(tokens[i-1])<<3)|registers.index(tokens[i])) location-=1 elif(tokens[i]=='$'): out.append(0x40+registers.index(tokens[i])) out.append(location>>8) out.append(location&255) elif(tokens[i] in names.keys()): out.append(0x40+registers.index(tokens[i])) out.append(names[tokens[i]]>>8) out.append(names[tokens[i]]&255) else: out.append(0x40+registers.index(tokens[i-1])) out.append(int(tokens[i])>>8) out.append(int(tokens[i])&255) elif tokens[i]=="ld": i=i+1 if(tokens[i] in reg8): reg=reg8.index(tokens[i]) i+=1 if(tokens[i]=='$'): print("'$' used for 16-bit value in an 8-bit instruction.") location+=3 elif(tokens[i] in names.keys()): out.append(0x06) out.append(reg) out.append(names[tokens[i]]&255) location+=3 elif(tokens[i] in reg8): out.append(0x04) out.append(reg8.index(tokens[i])+8*reg8.index(tokens[i-1])) location+=2 else: out.append(0x06) out.append(reg) out.append(int(tokens[i])&255) location+=3 elif(tokens[i] in registers): reg=registers.index(tokens[i]) i+=1 out.append(0x08+reg) if(tokens[i]=='$'): out.append(int(location)>>8) out.append(int(location)&255) location+=3 elif(tokens[i] in names.keys()): out.append(names[tokens[i]]>>8) out.append(names[tokens[i]]&255) location+=3 elif(tokens[i] in registers): out.append(16) out.append(registers.index(tokens[i])+8*registers.index(tokens[i-1])) location+=2 else: out.append(int(tokens[i])>>8) out.append(int(tokens[i])&255) location+=3 elif tokens[i]=='hlt': location+=1 out.append(0x07) elif tokens[i]=='kbd': location+=1 i+=1 out.append(reg8.index(tokens[i])+0xF0) elif tokens[i]=='wrt': location+=1 i+=1 out.append(reg8.index(tokens[i])+0x80) elif tokens[i]=='ldw': location+=2 i+=2 out.append(0x1F) out.append(reg8.index(tokens[i-1])+8*registers.index(tokens[i])) elif tokens[i]=='ldr': location+=2 i+=2 out.append(0x2F) out.append(reg8.index(tokens[i-1])+8*registers.index(tokens[i])) elif tokens[i]=='ret': location+=1 out.append(0x03) elif tokens[i]=='rtz': location+=1 out.append(0x19) elif tokens[i]=='lod': location+=1 out.append(0x2B) elif tokens[i]=='sto': location+=1 out.append(0x2A) else: print("error: invalid:"+tokens[i]) i+=1 stringout="#define BOOTLEN "+str(len(out))+"\nchar boot[BOOTLEN]={" for i in range(len(out)): stringout+=hex(out[i])+',' stringout+='};\n' print(stringout)