首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python写了一个简易MIPS汇编器,代码出错,求高手解答,该怎么解决

2012-04-16 
python写了一个简易MIPS汇编器,代码出错,求高手解答Python codeimport wximport stringdef process(event)

python写了一个简易MIPS汇编器,代码出错,求高手解答

Python code
import wximport stringdef process(event):    destination.Clear()    lineNo=source.GetNumberOfLines()    for i in range(0, lineNo, 1):        destination.AppendText(macCode(source.GetLineText(i))+'\n')def clr(event):    source.Clear()    destination.Clear()    def macCode(mipsStr):    mipsStr=string.replace(mipsStr, ",", "")    mipsStr=string.replace(mipsStr, "(", " ")    mipsStr=string.replace(mipsStr, "$", "")    codeList=string.split(mipsStr, ' ')    if codeList[0]=="add" or "sub" or "and" or "or" or "slt":        macStr=getOp(codeList[0])+getRegAsm(codeList[2])+getRegAsm(codeList[3])+getRegAsm(codeList[1])+getShamt(codeList[0])+getFunc(codeList[0])    elif codeList[0]=="lw" or "sw":        macStr=getOp(codeList[0])+getRegAsm(codeList[3])+getRegAsm(codeList[1])+getImAsm(codeList[2])    elif codeList[0]=="beq":        macStr=getOp(codeList[0])+getRegAsm(codeList[2])+getRegAsm(codeList[1])+getImAsm(codeList[3])    elif codeList[0]=="j":        macStr=getOp(codeList[0])+getJaddressAsm(codeList[1])    elif codeList[0]=="nop":        macStr="00000000000000000000000000000000"    else: macStr="Invalid line..."    return macStrdef getOp(instType):    if instType=="add" or "sub" or "and" or "or" or "slt":        return "000000"    elif instType=="lw":        return "100011"    elif instType=="sw":        return "101011"    elif instType=="beq":        return "000100"    elif instType=="j":        return "000010"    else: return "000000"def toBinary(num, length):    result=''    tmp=int(num, 10)    for i in range(0, length, 1):        if tmp%2:            result='1'+result            tmp-=1        else: result='0'+result        tmp/=2    return resultdef getRegAsm(num):    return toBinary(num, 5)def getImAsm(num):    return toBinary(num, 16)def getJAddressAsm(num):    return toBinary(num, 26)def getShamt(instType):    if instType=="add" or "sub" or "and" or "or" or "slt":        return "000000"    else: return "000000"def getFunc(instType):    if instType=="add":        return "100000"    elif instType=="sub":        return "100010"    elif instType=="and":        return "100100"    elif instType=="or":        return "100101"    elif instType=="slt":        return "101010"    else: return "000000"    #UI part...app=wx.App()win=wx.Frame(parent=None, title="MIPS 汇编器", size=(420, 500))bkg=wx.Panel(win)source=wx.TextCtrl(parent=bkg, pos=(8,10), size=(390, 200), style=wx.TE_MULTILINE|wx.HSCROLL)destination=wx.TextCtrl(parent=bkg, pos=(8, 250), size=(390, 200), style=wx.TE_MULTILINE|wx.HSCROLL)clrButton=wx.Button(parent=bkg, pos=(315,215), size=(80, 30), label="清空")proButton=wx.Button(parent=bkg, pos=(230,215), size=(80, 30), label="汇编")source.SetValue("nop\nadd $1, $2, $3\nsub $1, $2, $3\nand $2, $3, $4\nor $1, $5, $3\nslt $1, $2, $5\nlw $1, 10($2)\nsw $1, 10($1)\nbeq $1, $2, 10\nj 10000")proButton.Bind(wx.EVT_BUTTON, process)clrButton.Bind(wx.EVT_BUTTON, clr)win.Show()app.MainLoop()#End of UI part


[解决办法]
macCode代码有点问题啊

Python code
def macCode(mipsStr):    mipsStr = string.replace(mipsStr, ",", "")    mipsStr = string.replace(mipsStr, "(", " ")    mipsStr = string.replace(mipsStr, ")", "")    mipsStr = string.replace(mipsStr, "$", "")    codeList = string.split(mipsStr, ' ')    if codeList[0] in ["add", "sub", "and", "or", "slt"]:        macStr = getOp(codeList[0]) + getRegAsm(codeList[2]) + getRegAsm(codeList[3]) + getRegAsm(codeList[1]) + getShamt(codeList[0]) + getFunc(codeList[0])    elif codeList[0] in ["lw", "sw"]:        macStr = getOp(codeList[0]) + getRegAsm(codeList[3]) + getRegAsm(codeList[1]) + getImAsm(codeList[2])    elif codeList[0] == "beq":        macStr = getOp(codeList[0]) + getRegAsm(codeList[2]) + getRegAsm(codeList[1]) + getImAsm(codeList[3])    elif codeList[0] == "j":        macStr = getOp(codeList[0]) + getJAddressAsm(codeList[1])    elif codeList[0] == "nop":        macStr = "00000000000000000000000000000000"    else: macStr = "Invalid line..."    return macStr 

热点排行