1、计算机结构与组成cpu仿真 Project 1一问题描述: 这个工程需要编写MIPS汇编语言一个子集的指令解释器. 它将实现取指、反汇编, 解码, 并执行MIPS机器指令。也就是构建一个缩微的MARS。二问题分析:老师已经给出了一个工程,要求我们在所给文件中添加相应的代码,使sample.dump中的机器码(也就是一串数字)可以实现反汇编,在控制台黑屏中输出汇编指令和寄存器及内存的更新情况。而且.cpp文件中提供了两个函数disassembled和simulateInstr来分别实现上述功能。反汇编说明:在disassembled函数中,我要将数字指令转化为汇编语言,这首先需要对一段数字进行分
2、割,如先取32位数字的前6位为其opcode段等,数字分割是通过左移和右移实现的,具体代码如下:int opcode,func,rs,rt,rd,shamt,targaddress,immediate; opcode=instr26; rs=(instr27; rt=(instr27; rd=(instr27; shamt=(instr27; func=(instr26; immediate=(instr16; targaddress=(instr6; 完成数字分割后,就进入具体的指令分析阶段,此步通过if-else语句实现。如先解析R格式语句,先选出opcode为0的32为指令数字段,再针对
3、具体的func的值为其对上不同的汇编指令,例如opcode=0,func=33表示addu指令:if(opcode=0) if(func=33) /addu strcat_s(result1,addu $); strcat_s(result,result1); _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,te
4、mp); 运用strcat函数实现字符串的拼接,itoa_s函数将二进制数变为其他进制数输出,结果便可在黑频上输出如下类似指令: addu $0, $2, $2将所有情况的不同opcode和func的值用if-else语句像上面这样表示出来,于是就可以将所有的32位指令数字段解码成汇编代码,但此时的代码只是个空壳而已,需要用下面的simulateInstr函数将不同的指令的pc改变,计算,寄存器和内存的更新表示出来。在simulateInstr函数中同样也需要对每个32位指令数字段进行划分然后依据不同的opcode和func设置pc,进行计算以及表示寄存器和内存是否被更新 。在这里,寄存器及内
5、存的是否更新使用数字表示,-1代表没有更新,例如addu:if(opcode=0) if(func=33) /addu mips-pc = mips-pc + 4; mips-registersrd=mips-registersrt+mips-registersrs; if(rd=0) rd=-1; *changedReg = rd; *changedMem = -1; 其中pc会顺序加4,内存没有改变,但addu所加的结果被存在rd寄存器中,所以rd寄存器会更新。 将所有指令的实质改变按此方法用if-else语句表示出来后,该工程也就完成了。三所遇问题及改进方案: (1)在编程过程中,我开始
6、不知道该怎么样实现数据的符号扩展,因为我对32位数据采用的是左移右移方法来实现指令分割的,这就相对于对所有字段都进行的是零扩展,这对于addi,lw,sw指令中需要进行符号扩展的立即数来说显然矛盾。后来,在请教同学的基础上,我改进了方法,对于那些只需做零扩展的立即数,不需要做什么改变。对与addi这类需做符号扩展的立即数,我在相应的if语句中对立即数进行判断,如果立即数小于32768(也就是2的15次方),也就是该立即数的第一位数是0,那么零扩展与符号扩展对该立即数的真实值无影响,也就不需改变什么;反之,则该立即数的第一位数是1,那么必须进行符号扩展,即将该立即数减去65536(2的16次方)
7、即可。例如sw:if(opcode=43) /sw strcat_s(result1,sw $); strcat_s(result,result1); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, ); if(immediate26; rs=(instr27; rt=(instr27; rd=(instr27; shamt=(instr27; func=(instr26; immediate=(instr16; targaddress=(instr6; if(opcode=0) if(func=35) /subu st
8、rcat_s(result1,subu $); strcat_s(result,result1); _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); if(func=33) /addu strcat_s(result1,addu $); strcat_s(result,result1); _itoa_
9、s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); if(func=36) /and strcat_s(result1,and $); strcat_s(result,result1); _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $);
10、 _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); if(func=37) /or strcat_s(result1,or $); strcat_s(result,result1); _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result
11、, $); _itoa_s(rt,temp,10); strcat_s(result,temp); if(func=42) /slt strcat_s(result1,slt $); strcat_s(result,result1); _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); if(func=
12、0) /sll strcat_s(result1,sll $); strcat_s(result,result1); _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, ); _itoa_s(shamt,temp,10); strcat_s(result,temp); if(func=2) /srl strcat_s(result1,srl $); strcat_s(result,result1)
13、; _itoa_s(rd,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, ); _itoa_s(shamt,temp,10); strcat_s(result,temp); if(func=8) /jr strcat_s(result1,jr $); strcat_s(result,result1); _itoa_s(rs,temp,10); strcat_s(result,temp); if(opcode=9)
14、/addiu strcat_s(result1,addiu $); strcat_s(result,result1); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, ); if(immediate=32768) immediate-=65536; pc=pc+4+immediate*4; _itoa_s(pc,temp,16); strcat_s(result,temp); if(opcod
15、e=5) /bne strcat_s(result1,bne $); strcat_s(result,result1); _itoa_s(rs,temp,10); strcat_s(result,temp); strcat_s(result, $); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, 0x); if(immediate=32768) immediate-=65536; pc=pc+4+immediate*4; _itoa_s(pc,temp,16); strcat_s(result,temp); if(op
16、code=35) /lw strcat_s(result1,lw $); strcat_s(result,result1); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, ); if(immediate32768) _itoa_s(immediate,temp,10); else _itoa_s(immediate-65536,temp,10); strcat_s(result,temp); strcat_s(result,($); _itoa_s(rs,temp,10); strcat_s(result,temp);
17、 strcat_s(result,); if(opcode=43) /sw strcat_s(result1,sw $); strcat_s(result,result1); _itoa_s(rt,temp,10); strcat_s(result,temp); strcat_s(result, ); if(immediate28)28)+(targaddress4); strcat_s(result1,j 0x); strcat_s(result,result1); _itoa_s(pc,temp,16); strcat_s(result,temp); if(opcode=3) /jal p
18、c=(pc+4)28)28)+(targaddress4); strcat_s(result1,jal 0x); strcat_s(result,result1); _itoa_s(pc,temp,16); strcat_s(result,temp); return result;void simulateInstr (Computer mips, unsigned int instr, int *changedReg, int *changedMem) /* You replace this code by the right stuff. */ int opcode,func,rs,rt,
19、rd,shamt,targaddress,immediate; opcode=instr26; rs=(instr27; rt=(instr27; rd=(instr27; shamt=(instr27; func=(instr26; immediate=(instr16; targaddress=(instr6; if(opcode=0) if(func=33) /addu mips-pc = mips-pc + 4; mips-registersrd=mips-registersrt+mips-registersrs; if(rd=0) rd=-1; *changedReg = rd; *
20、changedMem = -1; if(func=35) /subu mips-pc = mips-pc + 4; mips-registersrd=mips-registersrs+mips-registersrt; if(rd=0) rd=-1; *changedReg = rd; *changedMem = -1; if(func=36) /and mips-pc = mips-pc + 4; mips-registersrd=mips-registersrs&mips-registersrt; if(rd=0) rd=-1; *changedReg = rd; *changedMem
21、= -1; if(func=37) /or mips-pc = mips-pc + 4; mips-registersrd=mips-registersrs|mips-registersrt; if(rd=0) rd=-1; *changedReg = rd; *changedMem = -1; if(func=42) /slt mips-pc = mips-pc + 4; if(mips-registersrsregistersrt) mips-registersrd=1; else mips-registersrd=0; if(rd=0) rd=-1; *changedReg = rd; *changedMem = -1; if(func=0) /
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1