public static string BinaryStrAnd(string a, string b)//逻辑乘:异或 { char[] A = new char[8]; numA = The_Method.zh(a); numB = The_Method.zh(b); for (int i = numA.Length - 1; i >= 0; i--) { for (int j = numB.Length - 1; j >= 0; j--) { if (numA[i] == '1' && numB[i] == '1' || numA[i] == '0' && numB[i] == '0') { A[i] = '0'; } else if (numA[i] == '1' && numB[i] == '0' || numA[i] == '0' && numB[i] == '1') { A[i] = '1'; } else { MessageBox.Show("异或错误"); } } } return(Convert.ToString(Convert.ToInt32(new string(A), 2), 16)); }
public void Sub_one(string a) //单操作数减一 { string b = The_Method.BinaryStrSub(Convert.ToString(Convert.ToInt32(a, 16), 2), "1"); try { tbox_R[m_id].Text = Convert.ToString(Convert.ToInt32(b, 2), 16).ToUpper(); } catch { tbox_R[m_id].Text = b; } }
public void Add_one(string a)//单操作数加一 { string b = The_Method.BinaryStrAdd_one(a, "1"); if (ir.Split(" ".ToCharArray()).Length == 5 && ir.Split(" ".ToCharArray())[3] != "000") { return; } try { tbox_R[m_id].Text = Convert.ToString(Convert.ToInt32(b, 2), 16).ToUpper(); } catch { tbox_R[m_id].Text = b; } }
public void one_begin() { initMemory(); string[] Arr = richTextBox_input.Text.ToLower().Split('\n'); //将汇编指令一行行分到arr中 pc = Convert.ToInt32(textBox_PC.Text.Replace(" ", ""), 16); //将空格换掉并写入pc ir = richTextBox_jiqima.Lines[Convert.ToInt32(pc - i) - 4096]; //从pc写入ir if (i != Arr.Length) { The_Method.select_line(0, pc - i - 4096); The_Method.select_line(1, pc - i - 4096); if (i != 0) { The_Method.select_line_cancel(0, pc - i - 4097); The_Method.select_line_cancel(1, pc - i - 4097); } } pc = pc + 2; i++; textBox_PC.Text = Convert.ToString(pc, 16); //pc的显示 string[] fenge_4 = ir.Split(' '); //分成4段 if (fenge_4.Length == 5) { y_id = Convert.ToInt32(fenge_4[2], 2); //寄存器编号 m_id = Convert.ToInt32(fenge_4[4], 2); } if (fenge_4.Length == 3) { single_caozuoqi = Convert.ToInt32(fenge_4[2], 2); //如果是单操作数 } if (ir == "") { MessageBox.Show("" + "指令执行完毕"); } //*****************************************取指 *************************************************************************** /********************加法 减法 与 MOV数据传送 ********************************************************************/ if (fenge_4[0] == "0001" || fenge_4[0] == "0010" || fenge_4[0] == "0011" || fenge_4[0] == "1010") { if (fenge_4[1] == "000") //直接寻址 { Y_j = tbox_R[y_id].Text; } if (fenge_4[1] == "001") //间接寻址 { Y_j = Convert.ToString(readMemory(Convert.ToInt32(tbox_R[y_id].Text.Replace(" ", " "), 2)), 2); //用寄存器的地址去访问主存,再从主存中取出数送Y_j } if (fenge_4[1] == "010") //自增型间接寻址 { Y_j = Convert.ToString(readMemory(Convert.ToInt32(tbox_R[y_id].Text.Replace(" ", " "), 2)), 2); //在间接的基础上,修改RS的内容,经运算器加一在送回rs当前操作的寄存器 tbox_R[y_id].Text = Convert.ToString(Convert.ToInt32(tbox_R[y_id].Text) + 1, 2); } if (fenge_4[1] == "011") //自增型双间址 { Y_j = Convert.ToString(readMemory(readMemory(Convert.ToInt32(tbox_R[y_id].Text.Replace(" ", " "), 2))), 2); //第一次访问主存取出操作数的地址送往地址寄存器 tbox_R[y_id].Text = Convert.ToString(Convert.ToInt32(tbox_R[y_id].Text) + 1, 2); //第二次以此地址访问存储器取出操作数送入Y_j //RS的内容加一 } if (fenge_4[1] == "100") //变址寻址 { Y_j = Convert.ToString(readMemory(Convert.ToInt32(richTextBox_jiqima.Lines[Convert.ToInt32(textBox_PC.Text) - i - 4096].Replace(" ", ""), 2) + Convert.ToInt32(tbox_R[y_id].Text.Replace(" ", ""), 2)), 2); //以PC现在的地址从主存中取出位移量,在与RS的内容相加 pc = pc + 2; //以相加结果的地址取出操作数送入Y_j //pc的内容加一 } } //***************************************取目的数****************************************************************************** /********************加法 减法 与 MOV数据传送 LDI载入立即数 NEC求补*********单操作数加一******减一***/ if (fenge_4[0] == "0001" || fenge_4[0] == "0010" || fenge_4[0] == "0011" || fenge_4[0] == "1010" || fenge_4[0] == "0110" || fenge_4[0] == "0100" || fenge_4[0] == "0101") { if (fenge_4[3] == "000") //直接寻址 { M_j = tbox_R[m_id].Text; } else if (fenge_4[3] == "001") //间接寻址 { M_j = Convert.ToString(readMemory(Convert.ToInt32(tbox_R[m_id].Text.Replace(" ", ""), 16)), 2); } else if (fenge_4[3] == "010") //自增型间接寻址 { M_j = Convert.ToString(readMemory(Convert.ToInt32(tbox_R[m_id].Text.Replace(" ", ""), 16)), 2); //在间接的基础上,修改RS的内容,经运算器加一在送回rs当前操作的寄存器 tbox_R[m_id].Text = Convert.ToString(Convert.ToInt32(tbox_R[m_id].Text) + 1, 16); } else if (fenge_4[3] == "011") //自增型双间址 { M_j = Convert.ToString(readMemory(readMemory(Convert.ToInt32(tbox_R[m_id].Text.Replace(" ", ""), 16))), 2); //第一次访问主存取出操作数的地址送往地址寄存器 tbox_R[m_id].Text = Convert.ToString(Convert.ToInt32(tbox_R[m_id].Text.Replace(" ", ""), 16) + 1, 16); //第二次以此地址访问存储器取出操作数送入Y_j //RS的内容加一 } else if (fenge_4[3] == "100") //变址寻址 { int pc_10 = int.Parse(textBox_PC.Text, System.Globalization.NumberStyles.HexNumber); int R_10 = int.Parse(tbox_R[m_id].Text, System.Globalization.NumberStyles.HexNumber); M_j = Convert.ToString(readMemory(Convert.ToInt32(Convert.ToString(pc_10 + R_10, 16)))); pc = pc + 2; M[Convert.ToInt32(Convert.ToString(pc_10 + R_10, 16))]--; } else { MessageBox.Show("进入失败"); } } if (fenge_4[0] == "1110") // LDI载入立即数 { M_j = fenge_4[1].Substring(0, 8); } if (fenge_4[0] == "1001") //装载指令ld { M_j = Convert.ToString(readMemory(Convert.ToInt32(fenge_4[1].Substring(0, 8), 2)), 2); //把K的地址的内容送Rd } //***************************************************执行周期***************************************************************** if (fenge_4[0] == "0001") { tbox_R[y_id].Text = (The_Method.BinaryStrAdd(Y_j.Replace(" ", ""), M_j.Replace(" ", ""))); //加法 } if (fenge_4[0] == "0010") { tbox_R[y_id].Text = (The_Method.BinaryStrSub(Y_j.Replace(" ", ""), M_j.Replace(" ", ""))); //减法 } if (fenge_4[0] == "0011") { tbox_R[y_id].Text = (The_Method.BinaryStrAnd(Y_j.Replace(" ", ""), M_j.Replace(" ", ""))); //与 } if (fenge_4[0] == "1010") { Move_data(Y_j.Replace(" ", ""), M_j.Replace(" ", "")); //数据传送指令 } if (fenge_4[0] == "0100") { Add_one(M_j.Replace(" ", "")); //单操作数加一 } if (fenge_4[0] == "0101" && fenge_4[3] != "100") { Sub_one(M_j.Replace(" ", "")); //单操作数减一 } if (fenge_4[0] == "1110") { Ldi(M_j.Replace(" ", "")); //载入立即数ldi } if (fenge_4[0] == "1001") { Ld(M_j.Replace(" ", "")); //装载指令ld } }
public static string jiqima_2(string code) { string strValue = ""; string[] str = new string[] { "000", "001", "010", "011", "100", "101", "110", "111" };//寄存器编号 string[] codes = code.Split(new char[2] { ' ', ',' }); //双操作数 string[] codes1 = code.Split(new char[1] { ' ' }); //单操作数 if (codes[0] == "nop") { strValue += "0000 " + "000 " + "000 " + "000" + "000\n"; } if (codes[0] == "add") { char[] s = codes[1].ToCharArray(); char[] d = codes[2].ToCharArray(); if (s[0] == 'r')// 源操作数直接寻址 { if (d[0] == 'r') { strValue += "0001 " + "000 " + str[((int)s[1]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; // 目的操作数直接寻址 } if (d[0] == '(' && d[3] == ')' && d.Length == 4) //间接寻址 { strValue += "0001 " + "000 " + str[((int)s[1]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0001 " + "000 " + str[((int)s[1]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0001 " + "000 " + str[((int)s[1]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')//变址寻址 { strValue += "0001 " + "000 " + str[((int)s[1]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && s.Length == 4) // 源操作数间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0001 " + "001 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0001 " + "001 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0001 " + "001 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0001 " + "001 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0001 " + "001 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && s.Length == 5) //源操作数 自增型 间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0001 " + "010 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0001 " + "010 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0001 " + "010 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0001 " + "010 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0001 " + "010 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+' && s.Length == 6) //源操作数自增型 双间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0001 " + "011 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0001 " + "011 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0001 " + "011 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0001 " + "011 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0001 " + "011 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == 'x' && s[1] == '(' && s[4] == ')' && s.Length == 5) // 源操作数 变址寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0001 " + "100 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0001 " + "100 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0001 " + "100 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0001 " + "100 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0001 " + "100 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } } /*******************************************减法************************************************************************************************************/ if (codes[0] == "sub") { char[] s = codes[1].ToCharArray(); char[] d = codes[2].ToCharArray(); if (s[0] == 'r') // 源操作数直接寻址 { if (d[0] == 'r') // 目的操作数直接寻址 { strValue += "0010 " + "000 " + str[((int)s[1]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) //间接寻址 { strValue += "0010 " + "000 " + str[((int)s[1]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0010 " + "000 " + str[((int)s[1]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0010 " + "000 " + str[((int)s[1]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')//变址寻址 { strValue += "0010 " + "000 " + str[((int)s[1]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && s.Length == 4) // 源操作数间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0010 " + "001 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0010 " + "001 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d[4] == '+' && d.Length == 5)//自增型 间接寻址 { strValue += "0010 " + "001 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0010 " + "001 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0010 " + "001 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && s.Length == 5) //源操作数 自增型 间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0010 " + "010 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0010 " + "010 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0010 " + "010 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0010 " + "010 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0010 " + "010 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+' && s.Length == 6) //源操作数自增型 双间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0010 " + "011 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0010 " + "011 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0010 " + "011 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0010 " + "011 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0010 " + "011 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == 'x' && s[1] == '(' && s[4] == ')' && s.Length == 5) // 源操作数 变址寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0010 " + "100 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0010 " + "100 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0010 " + "100 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0010 " + "100 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0010 " + "100 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } } /***************************************************************逻辑乘****************************************************************************************/ if (codes[0] == "and") { char[] s = codes[1].ToCharArray(); char[] d = codes[2].ToCharArray(); if (s[0] == 'r') // 源操作数直接寻址 { if (d[0] == 'r') // 目的操作数直接寻址 { strValue += "0011 " + "000 " + str[((int)s[1]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) //间接寻址 { strValue += "0011 " + "000 " + str[((int)s[1]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0011 " + "000 " + str[((int)s[1]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0011 " + "000 " + str[((int)s[1]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')//变址寻址 { strValue += "0011 " + "000 " + str[((int)s[1]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && d.Length == 4) // 源操作数间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0011 " + "001 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0011 " + "001 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0011 " + "001 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0011 " + "001 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0011 " + "001 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && s[4] == '+' && s.Length == 5) //源操作数 自增型 间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0011 " + "010 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0011 " + "010 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0011 " + "010 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0011 " + "010 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0011 " + "010 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+' && s.Length == 6) //源操作数自增型 双间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0011 " + "011 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0011 " + "011 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0011 " + "011 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0011 " + "011 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0011 " + "011 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == 'x' && s[1] == '(' && s[4] == ')' && s.Length == 5) // 源操作数 变址寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "0011 " + "100 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "0011 " + "100 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "0011 " + "100 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "0011 " + "100 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "0011 " + "100 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } } /*************************************************数据传送指令**********************************************************************************************/ if (codes[0] == "mov") { char[] s = codes[1].ToCharArray(); char[] d = codes[2].ToCharArray(); if (s[0] == 'r') // 源操作数直接寻址 { if (d[0] == 'r') // 目的操作数直接寻址 { strValue += "1010 " + "000 " + str[((int)s[1]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) //间接寻址 { strValue += "1010 " + "000 " + str[((int)s[1]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "1010 " + "000 " + str[((int)s[1]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "1010 " + "000 " + str[((int)s[1]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')//变址寻址 { strValue += "1010 " + "000 " + str[((int)s[1]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && d.Length == 4) // 源操作数间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "1010 " + "001 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "1010 " + "001 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "1010 " + "001 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "1010 " + "001 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "1010 " + "001 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '(' && s[3] == ')' && s[4] == '+' && s.Length == 5) //源操作数 自增型 间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "1010 " + "010 " + str[((int)s[2]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "1010 " + "010 " + str[((int)s[2]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "1010 " + "010 " + str[((int)s[2]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "1010 " + "010 " + str[((int)s[2]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "1010 " + "010 " + str[((int)s[2]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+' && s.Length == 6) //源操作数自增型 双间接寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "1010 " + "011 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "1010 " + "011 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "1010 " + "011 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "1010 " + "011 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "1010 " + "011 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } /***********************************************************************************************************************************************************/ if (s[0] == 'x' && s[1] == '(' && s[4] == ')' && s.Length == 5) // 源操作数 变址寻址 { if (d[0] == 'r') // 目的操作数 直接寻址 { strValue += "1010 " + "100 " + str[((int)s[3]) - 48] + " 000 " + str[Convert.ToInt32(d[1].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 4) // 间接寻址 { strValue += "1010 " + "100 " + str[((int)s[3]) - 48] + " 001 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '(' && d[3] == ')' && d.Length == 5)//自增型 间接寻址 { strValue += "1010 " + "100 " + str[((int)s[3]) - 48] + " 010 " + str[Convert.ToInt32(d[2].ToString())] + "\n"; } if (d[0] == '@' && d[1] == '(' && d[4] == ')' && d[5] == '+')//自增型 双间接寻址 { strValue += "1010 " + "100 " + str[((int)s[3]) - 48] + " 011 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } if (d[0] == 'x' && d[1] == '(' && d[4] == ')')// 变址寻址 { strValue += "1010 " + "100 " + str[((int)s[3]) - 48] + " 100 " + str[Convert.ToInt32(d[3].ToString())] + "\n"; } } } /****************************************************单操作数加一指令**********************************************************************************/ if (codes1[0] == "inc") { char[] s = codes1[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "0100 " + "000 " + "000 " + "000 " + str[((int)s[1]) - 48] + "\n"; } if (s[0] == '(' && s[3] == ')' && s.Length == 4) // 间接寻址 { strValue += "0100 " + "000 " + "000 " + "001 " + str[((int)s[2]) - 48] + "\n"; } if (s[0] == '(' && s[3] == ')' && s.Length == 5)//自增型 间接寻址 { strValue += "0100 " + "000 " + "000 " + "010 " + str[((int)s[2]) - 48] + "\n"; } if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+')//自增型 双间接寻址 { strValue += "0100 " + "000 " + "000 " + "011 " + str[((int)s[3]) - 48] + "\n"; } if (s[0] == 'x' && s[1] == '(' && s[4] == ')')// 变址寻址 { strValue += "0100 " + "000 " + "000 " + "100 " + str[((int)s[3]) - 48] + "\n"; } } /***********************************************单操作数减一指令*****************************************************************************************/ if (codes1[0] == "dec") { char[] s = codes1[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "0101 " + "000 " + "000 " + "000 " + str[((int)s[1]) - 48] + "\n"; } if (s[0] == '(' && s[3] == ')' && s.Length == 4) // 间接寻址 { strValue += "0101 " + "000 " + "000 " + "001 " + str[((int)s[2]) - 48] + "\n"; } if (s[0] == '(' && s[3] == ')' && s.Length == 5)//自增型 间接寻址 { strValue += "0101 " + "000 " + "000 " + "010 " + str[((int)s[2]) - 48] + "\n"; } if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+')//自增型 双间接寻址 { strValue += "0101 " + "000 " + "000 " + "011 " + str[((int)s[3]) - 48] + "\n"; } if (s[0] == 'x' && s[1] == '(' && s[4] == ')')// 变址寻址 { strValue += "0101 " + "000 " + "000 " + "100 " + str[((int)s[3]) - 48] + "\n"; } } /***********************************************求补码指令****************************************************************************************/ if (codes1[0] == "nec") { char[] s = codes1[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "0110 " + "000 " + "000 " + "000 " + str[((int)s[1]) - 48] + "\n"; } if (s[0] == '(' && s[3] == ')' && s.Length == 4) // 间接寻址 { strValue += "0110 " + "000 " + "000 " + "001 " + str[((int)s[2]) - 48] + "\n"; } if (s[0] == '(' && s[3] == ')' && s.Length == 5)//自增型 间接寻址 { strValue += "0110 " + "000 " + "000 " + "010 " + str[((int)s[2]) - 48] + "\n"; } if (s[0] == '@' && s[1] == '(' && s[4] == ')' && s[5] == '+')//自增型 双间接寻址 { strValue += "0110 " + "000 " + "000 " + "011 " + str[((int)s[3]) - 48] + "\n"; } if (s[0] == 'x' && s[1] == '(' && s[4] == ')')// 变址寻址 { strValue += "0110 " + "000 " + "000 " + "100 " + str[((int)s[3]) - 48] + "\n"; } } /**********************************************无条件相对跳转指令***************************************************************************************/ if (codes1[0] == "jmp") { char[] s = codes1[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "0111 " + "000 " + "000 " + "000 " + str[((int)s[1]) - 48] + "\n"; } } /*********************************************有进位跳转指令**********************************************************************************************/ if (codes1[0] == "jc") { char[] s = codes1[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "1000 " + "000 " + "000 " + "000 " + str[((int)s[1]) - 48] + "\n"; } } /******************************************载入立即数指令***********************************************************************************************/ if (codes[0] == "ldi") { char[] s = codes[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "1110 " + The_Method.shiliu_erjinzhi(codes[2]) + "0 " + str[Convert.ToInt32(s[1].ToString())] + "\n"; } } /****************************************装载指令*********************************************************************************************************/ if (codes[0] == "ld") { char[] s = codes[1].ToCharArray(); if (s[0] == 'r')// 操作数 直接寻址 { strValue += "1001 " + The_Method.shiliu_erjinzhi(codes[2]) + "1 " + str[Convert.ToInt32(s[1].ToString())] + "\n"; } } return(strValue); }//将指令转换为机器码