硬件设计测试中,MIPS汇编指令翻译成二进制编码的Python实现
2011-04-18 23:43
621 查看
最近在做计算机体系结构与组成原理的实验,需要写MIPS指令系统的CPU。在测试时,人工将需要测试的指令翻译成二进制的编码实在有些繁琐,而且极易出错,正好最近在学习python,就写了一个实用的脚本,来完成这个工作。
由于我们实现的CPU暂时只实现了16条指令,分别是:
add rd,rs,rt
addu rd,rs,rt
addi rt,rs,imm
addiu rt,rs,imm
sub rd,rs,rt
subu rd,rs,rt
nor rd,rs,rt
xori rt,rs,imm
clo rd, rs
clz rs,rs
slt rd,rs,rt
sltu rd,rs,rt
slti rt,rs,imm
sltiu rt,rs imm
blez rs,imm
j target
因此这个脚本只涉及了这些指令的内容,以后再扩展吧。
下面说一下实现思路:
1、从一个文件中读取MIPS的汇编指令;
2、通过字符串处理,然后将其各个部分进行匹配;
3、将生成的二进制编码写到一个文件中。
需要注意的是,指令应该向上边的示例一样,在操作符和操作数之间应该有个空格,操作数之间以逗号和若干(最好0)个空格分隔,每行写一条指令,指令前最好没有空字符。由于这个脚本主要是自己用,鲁棒性不是很好,见谅。
下面直接上代码:
Reference:
《Mips instruction Reference》
http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html
由于我们实现的CPU暂时只实现了16条指令,分别是:
add rd,rs,rt
addu rd,rs,rt
addi rt,rs,imm
addiu rt,rs,imm
sub rd,rs,rt
subu rd,rs,rt
nor rd,rs,rt
xori rt,rs,imm
clo rd, rs
clz rs,rs
slt rd,rs,rt
sltu rd,rs,rt
slti rt,rs,imm
sltiu rt,rs imm
blez rs,imm
j target
因此这个脚本只涉及了这些指令的内容,以后再扩展吧。
下面说一下实现思路:
1、从一个文件中读取MIPS的汇编指令;
2、通过字符串处理,然后将其各个部分进行匹配;
3、将生成的二进制编码写到一个文件中。
需要注意的是,指令应该向上边的示例一样,在操作符和操作数之间应该有个空格,操作数之间以逗号和若干(最好0)个空格分隔,每行写一条指令,指令前最好没有空字符。由于这个脚本主要是自己用,鲁棒性不是很好,见谅。
下面直接上代码:
import os def readfile(filename): """read and store test file""" try: fobj = open(filename, 'r') except IOError, e: print "*** file open error:", e else: lines = [] for eachLine in fobj: lines.append(eachLine) fobj.close() return lines def writefile(filename, l): """write the file with the binary""" ls = os.linesep fobj = open(filename, 'w') fobj.writelines(['%s%s' % (x, ls) for x in l]) fobj.close() print 'DONE!' def splitOandD(inst): """split the op and data""" sinst = [] for x in inst: z = x.rstrip() y = z.split(' ', 1) sinst.append(y); return sinst def numtobin(num, count): """tran a num to binary""" s = (bin(num))[2:] ss = ((count - len(s)) * '0') + s return ss def regtobin(reg): """make a regs to binary""" regs = reg.lstrip() regnum = regs[1:] if((int)(regnum) <= 31 and (int)(regnum) >= 0): regb = numtobin((int)(regnum), 5) return regb else: print "***no regs: ", regs return regb def handle(ins): """handle the strings to regular binary code""" op = (str)(ins[0]) data = (str)(ins[1]) sdata = data.lstrip() dl = sdata.split(',') if op == "add": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "100000" incode = opcode+rs+rt+rd+"00000"+func elif op == "addu": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "100001" incode = opcode+rs+rt+rd+"00000"+func elif op == "addi": opcode = "001000" rt = regtobin(dl[0]) rs = regtobin(dl[1]) imm = numtobin((int)(dl[2]), 16) incode = opcode + rs + rt + imm elif op == "addiu": opcode = "001001" rt = regtobin(dl[0]) rs = regtobin(dl[1]) imm = numtobin((int)(dl[2]), 16) incode = opcode + rs + rt + imm elif op == "sub": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "100010" incode = opcode+rs+rt+rd+"00000"+func elif op == "subu": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "100011" incode = opcode+rs+rt+rd+"00000"+func elif op == "nor": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "100111" incode = opcode+rs+rt+rd+"00000"+func elif op == "xori": opcode = "001110" rt = regtobin(dl[0]) rs = regtobin(dl[1]) imm = numtobin((int)(dl[2]), 16) incode = opcode + rs + rt + imm elif op == "clo": opcode = "011100" rd = regtobin(dl[0]) rs = regtobin(dl[1]) func = "100001" incode = opcode + rs + "00000" + rd + "00000" + func elif op == "clz": opcode = "011100" rd = regtobin(dl[0]) rs = regtobin(dl[1]) func = "100000" incode = opcode + rs + "00000" + rd + "00000" + func elif op == "slt": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "101010" incode = opcode+rs+rt+rd+"00000"+func elif op == "sltu": opcode = "000000" rd = regtobin(dl[0]) rs = regtobin(dl[1]) rt = regtobin(dl[2]) func = "101011" incode = opcode+rs+rt+rd+"00000"+func elif op == "slti": opcode = "001010" rt = regtobin(dl[0]) rs = regtobin(dl[1]) imm = numtobin((int)(dl[2]), 16) incode = opcode + rs + rt + imm elif op == "sltiu": opcode = "001011" rt = regtobin(dl[0]) rs = regtobin(dl[1]) imm = numtobin((int)(dl[2]), 16) incode = opcode + rs + rt + imm elif op == "blez": opcode = "000110" rs = regtobin(dl[0]) imm = numtobin((int)(dl[1]), 16) incode = opcode + rs + "00000" + imm elif op == "j": opcode = "000010" imm = numtobin((int)(dl[0]), 26) incode = opcode + imm else: print "***Not Define: ", op return incode def main(): l = readfile("i.txt") s = splitOandD(l) b = [] print s for m in s: if m != ['']: print m b.append(handle(m)) writefile("o.txt", b) if __name__ == "__main__": main()
Reference:
《Mips instruction Reference》
http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html
相关文章推荐
- 简单的性能自动化测试架构设计和实现(pylot)-python
- Python实现正交实验法自动设计测试用例
- Python:实用抓图工具开发介绍(含需求分析、设计、编码、单元测试、打包、系统测试、发布各环节)
- [python实现设计模式]-5.迭代器模式-一起撸串嗨皮啦
- Python实现的将文件每一列写入列表功能示例【测试可用】
- 用python做自动化测试--实现高性能测试工具(4) --系统架构
- Ubuntu中为Android系统实现内置Java应用程序测试Application Frameworks层的硬件服务
- 流畅的python 第六章: 使用一等函数实现设计模式
- 第22课 Python字符串分割函数设计与实现
- 专为渗透测试人员设计的 Python 工具大合集
- python用递归实现反转一个整型数(记2017腾讯测试工程师面试)
- 翻译-精通python设计模式-工厂模式
- python 测试实现方法
- python实现http接口测试框架
- [python实现设计模式]-1. 单例模式
- 【Python】实现性能测试工具方法
- Python实现的本地文件搜索功能示例【测试可用】
- [C++] 测试硬件popcnt(位1计数)指令与各种软件算法,利用模板实现静态多态优化性能
- 23种设计模式之python实现--Flyweight模式