您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 其它相关文档 > 计算机结构与组成cpu仿真
Project1一.问题描述:这个工程需要编写MIPS汇编语言一个子集的指令解释器.它将实现取指、反汇编,解码,并执行MIPS机器指令。也就是构建一个缩微的MARS。二.问题分析:老师已经给出了一个工程,要求我们在所给文件中添加相应的代码,使sample.dump中的机器码(也就是一串数字)可以实现反汇编,在控制台黑屏中输出汇编指令和寄存器及内存的更新情况。而且.cpp文件中提供了两个函数disassembled和simulateInstr来分别实现上述功能。反汇编说明:在disassembled函数中,我要将数字指令转化为汇编语言,这首先需要对一段数字进行分割,如先取32位数字的前6位为其opcode段等,数字分割是通过左移和右移实现的,具体代码如下:intopcode,func,rs,rt,rd,shamt,targaddress,immediate;opcode=instr26;rs=(instr6)27;rt=(instr11)27;rd=(instr16)27;shamt=(instr21)27;func=(instr26)26;immediate=(instr16)16;targaddress=(instr6)6;完成数字分割后,就进入具体的指令分析阶段,此步通过if-else语句实现。如先解析R格式语句,先选出opcode为0的32为指令数字段,再针对具体的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,temp);}运用strcat函数实现字符串的拼接,itoa_s函数将二进制数变为其他进制数输出,结果便可在黑频上输出如下类似指令:addu$0,$2,$2将所有情况的不同opcode和func的值用if-else语句像上面这样表示出来,于是就可以将所有的32位指令数字段解码成汇编代码,但此时的代码只是个空壳而已,需要用下面的simulateInstr函数将不同的指令的pc改变,计算,寄存器和内存的更新表示出来。在simulateInstr函数中同样也需要对每个32位指令数字段进行划分然后依据不同的opcode和func设置pc,进行计算以及表示寄存器和内存是否被更新。在这里,寄存器及内存的是否更新使用数字表示,-1代表没有更新,例如addu:if(opcode==0){if(func==33)//addu{mips-pc=mips-pc+4;mips-registers[rd]=mips-registers[rt]+mips-registers[rs];if(rd==0)rd=-1;*changedReg=rd;*changedMem=-1;}其中pc会顺序加4,内存没有改变,但addu所加的结果被存在rd寄存器中,所以rd寄存器会更新。将所有指令的实质改变按此方法用if-else语句表示出来后,该工程也就完成了。三.所遇问题及改进方案:(1)在编程过程中,我开始不知道该怎么样实现数据的符号扩展,因为我对32位数据采用的是左移右移方法来实现指令分割的,这就相对于对所有字段都进行的是零扩展,这对于addi,lw,sw指令中需要进行符号扩展的立即数来说显然矛盾。后来,在请教同学的基础上,我改进了方法,对于那些只需做零扩展的立即数,不需要做什么改变。对与addi这类需做符号扩展的立即数,我在相应的if语句中对立即数进行判断,如果立即数小于32768(也就是2的15次方),也就是该立即数的第一位数是0,那么零扩展与符号扩展对该立即数的真实值无影响,也就不需改变什么;反之,则该立即数的第一位数是1,那么必须进行符号扩展,即将该立即数减去65536(2的16次方)即可。例如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(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);strcat_s(result,));}(2)在simulateInstr函数中对于sw与lw指令,需要对内存中的值进行读取,开始时我只用rs中的地址和立即数相加得到要读或取的内存地址,但这样得不到正确的结果,在同学的提醒下我注意到该程序设计的内存首地址为0x00400000,于是将上面的数减去0x00400000即可得正确结果。四.以下为源程序代码:charresult[100]={0};charresult1[100]={0};chartemp[100]={0};char*disassembled(unsignedintinstr,unsignedintpc){memset(result,0,100);memset(result1,0,100);memset(temp,0,100);intopcode,func,rs,rt,rd,shamt,targaddress,immediate;opcode=instr26;rs=(instr6)27;rt=(instr11)27;rd=(instr16)27;shamt=(instr21)27;func=(instr26)26;immediate=(instr16)16;targaddress=(instr6)6;if(opcode==0){if(func==35)//subu{strcat_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_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,,$);_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,,$);_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==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);_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)//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(immediate32768)_itoa_s(immediate,temp,10);else_itoa_s(immediate-65536,temp,10);strcat_s(result,temp);}if(opcode==12)//andi{strcat_s(result1,andi$);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,,0x);_itoa_s(immediate,temp,16);strcat_s(result,temp);}if(opcode==13)//ori{
本文标题:计算机结构与组成cpu仿真
链接地址:https://www.777doc.com/doc-5125693 .html