计算机原理
实验报告
课程名称: 计算机原理
指导教师:
姓 名:
班 级:
学 号:
日 期: 2022.5.8
人 工 智 能 学 院
一、设计题目
模拟八功能ALU的设计与实现
- 设计要求
实现八功能ALU。
1.首先确定八功能分别为加法、减法、乘法、与、或、非、同或、异或运算。
2.采用代码实现其运算过程。
三、设计过程
1、加法运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,若A、B为正数,则向数组的第一位存入字符0;若A、B为负数,则向数组的第一位存入字符1。再将A、B的二进制字符串按顺序逐位存入数组中。定义一个整形数组,将字符型数组中的字符,逐位转化为整形。定义一个变量用来存低一位的进位,定义一个整形数组用来存结果。将对应位的值与来自低位的进位相加,若大于等于2则低位的进位变量为1,再将求出来的值减去2,得出来的结果存入数组的对应位上;若小于2则低位的进位变量为0,将得出来的结果直接存入数组的对应位上。
根据数组的前两位的值(双符号位)判断结果是否溢出:若前两位为00或11则不溢出,psw寄存器的OF标志位的值为0;若前两位为01则为上溢出,10则为下溢出,psw寄存器的OF标志位的值为1。然后将整形数组里的数的后七位逐位转换成字符,在以字符串的形式输出到结果的文本框中。
图 1 加法
2、减法运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,若A、B为正数,则向数组的第一位存入字符0;若A、B为负数,则向数组的第一位存入字符1。然后将A的二进制字符串按顺序逐位存入数组中。将B的二进制字符串按顺序取反存入到数组中。定义一个整形数组,将字符型数组中的字符,逐位转化为整形。定义一个变量用来存低一位的进位,定义一个整形数组用来存结果。将最后一位A、B对应的值相加再加上1,其余位将对应位的值与来自低位的进位相加。若大于等于2则低位的进位变量为1,再将求出来的值减去2,得出来的结果存入数组的对应位上;若小于2则低位的进位变量为0,将得出来的结果直接存入数组的对应位上。
根据数组的前两位的值(双符号位)判断结果是否溢出:若前两位为00或11则不溢出,psw寄存器的OF标志位的值为0;若前两位为01则为上溢出,10则为下溢出,psw寄存器的OF标志位的值为1。然后将整形数组里的数的后七位逐位转换成字符,在以字符串的形式输出到结果的文本框中。
图 2 减法
3、乘法运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,将A、B的二进制字符串逐位存入数组中,定义一个整形数组,将字符型数组中的字符,逐位转化为整形。定义一个数组用来存部分积,当B的最后一位为0时,部分积和乘数向右挪一位,部分积的最后一位放入到乘数的最前一位,部分积的最前一位插入0;当B的最后一位为1时,部分积加上被乘数后右移,乘数右移,部分积的最后一位放入到乘数的最前一位,部分积的最前一位插入0。将最后部分积和乘数两部分的结果分别转为字符型存入数组中,最后以字符串的形式输出两部分结果组合在一起的最后结果。
图 3 乘法
4、与运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,将A、B的二进制字符串逐位存入数组中,再将对应位逐位做与运算:若A为0,B为0,则得0;若A为0,B为1,则得0;若A为1,B为0,则得0;若A为1,B为1,则得1。再定义一个数组,将运算结果逐位存入数组中,再以字符串的形式输出到结果的文本框中。
图 4 与运算
5、或运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,将A、B的二进制字符串逐位存入数组中,再将对应位逐位做或运算:若A为0,B为0,则得0;若A为0,B为1,则得1;若A为1,B为0,则得1;若A为1,B为1,则得1。再定义一个数组,将运算结果逐位存入数组中,再以字符串的形式输出到结果的文本框中。
图 5 或运算
6、非运算:将需要运算的数字A输入到文本框中,将其转换为二进制字符串,求出补码,定义一个字符型数组,将A的二进制字符串逐位存入数组中,再将对应位逐位做非运算:若A为0,则得1;若A为1,则得0。再定义一个数组,将运算结果逐位存入数组中,再以字符串的形式输出到结果的文本框中。
图 6 非运算
7、同或运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,将A、B的二进制字符串逐位存入数组中,再将对应位逐位做同或运算:若A为0,B为0,则得1;若A为0,B为1,则得0;若A为1,B为0,则得0;若A为1,B为1,则得1。再定义一个数组,将运算结果逐位存入数组中,再以字符串的形式输出到结果的文本框中。
图 7 同或运算
8、异或运算:将需要运算的数字A、B输入到文本框中,将其转换为二进制字符串,求出补码,定义两个字符型数组,将A、B的二进制字符串逐位存入数组中,再将对应位逐位做异或运算:若A为0,B为0,则得0;若A为0,B为1,则得1;若A为1,B为0,则得1;若A为1,B为1,则得0。再定义一个数组,将运算结果逐位存入数组中,再以字符串的形式输出到结果的文本框中。
图 8 异或运算
四、设计中遇到的问题和解决方案
问题:原方案在不同运算指令之间来回切换的实现代码非常冗余,且过于拆分,结合性不强。
解决方案:将八个指令用三位二进制数来编排,并用switch()语句对八种运算划分,节省了空间成本。
五、设计感触
在计原的一次次作业中,提高了我的实践能力,让我们不再是死读书,而是结合着相关的知识去理解,去自己动手尝试,去理解其中的内涵。也让我知道想当然是并不能实现的,你可以看着一个题目很简单,你觉得怎么怎么样就可以解决,但是当你实际操作时,你就会发现,其是它远比你想象中要复杂的多的多。
从前的课程里没有动手实践的习惯,对于很多知识其实存在着一知半解的问题,看似懂了,当堂课问,我也都知道,但是课下需要我去实际解决问题的时候我就会觉得,这跟我学的不一样,更甚至会觉得一点思路都没有,像这样的实践内容很好的解决了这样的问题。
六、附录(代码电子版提供,纸质版不打印)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 模拟ALU运算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox_wys.AppendText("指令数如下:\n000:加\n001:减\n010:乘\n011:与\n100:或\n101:非\n110:同或\n111:异或");
}
private void btn_Clear_Click(object sender, EventArgs e)
{
tB_A_wys.Clear();
tB_A0_wys.Clear();
tB_A1_wys.Clear();
tB_A2_wys.Clear();
tB_A3_wys.Clear();
tB_A4_wys.Clear();
tB_A5_wys.Clear();
tB_A6_wys.Clear();
tB_A7_wys.Clear();
tB_B_wys.Clear();
tB_B0_wys.Clear();
tB_B1_wys.Clear();
tB_B2_wys.Clear();
tB_B3_wys.Clear();
tB_B4_wys.Clear();
tB_B5_wys.Clear();
tB_B6_wys.Clear();
tB_B7_wys.Clear();
tB_I_wys.Clear();
tB_I0_wys.Clear();
tB_I1_wys.Clear();
tB_I2_wys.Clear();
tB_Y0_wys.Clear();
tB_Y1_wys.Clear();
tB_Y2_wys.Clear();
tB_Y3_wys.Clear();
tB_Y4_wys.Clear();
tB_Y5_wys.Clear();
tB_Y6_wys.Clear();
tB_Y7_wys.Clear();
tB_G_wys.Clear();
tB_V_wys.Clear();
tB_N_wys.Clear();
tB_Z_wys.Clear();
tB_psw_wys.Clear();
tB_Regi_wys.Clear();
richTextBox_wys.Clear();
richTextBox_wys.AppendText("指令数如下:\n000:加\n001:减\n010:乘\n011:与\n100:或\n101:非\n110:同或\n111:异或");
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn_Clock_Click(object sender, EventArgs e)
{
Transition();
char[] ch = tB_I_wys.Text.ToCharArray();
string[] str = new string[tB_I_wys.Text.Length];
for (int l = 0; l < tB_I_wys.Text.Length; l++)
{
str[l] = ch[l].ToString();
}
tB_I2_wys.Text = str[0];
tB_I1_wys.Text = str[1];
tB_I0_wys.Text = str[2];
switch (tB_I_wys.Text)
{
case "000":
add();
break;
case "001":
subtraction();
break;
case "010":
mutiply();
break;
case "011":
yu();
break;
case "100":
huo();
break;
case "101":
fei();
break;
case "110":
TH();
break;
case "111":
YH();
break;
default:
MessageBox.Show("输入指令不正确,请重新输入!");
tB_I_wys.Clear();
break;
}
tB_psw_wys.Text = tB_G_wys.Text + tB_V_wys.Text + tB_N_wys.Text + tB_Z_wys.Text;
}
private void Transition()
{
richTextBox_wys.Clear();
try
{
if (tB_A_wys.Text != "")
{
int a = int.Parse(this.tB_A_wys.Text);
if (a > 0 && a <= 127)
{
string x = Convert.ToString(a, 2);
this.tB_A_wys.Text = x.ToString().PadLeft(8, '0');
}
else
{
if (a < 0 && a >= -128)
{
//string x1 = Convert.ToString(-a, 2);
//this.textBox2.Text = (x1.ToString().PadLeft(7, '0')).ToString().PadLeft(8, '1');
//string x2 = Convert.ToString(255 + a, 2);
//this.textBox3.Text = (x2.ToString().PadLeft(7, '0')).ToString().PadLeft(8, '1');
string x3 = Convert.ToString(256 + a, 2);
this.tB_A_wys.Text = (x3.ToString().PadLeft(7, '0')).ToString().PadLeft(8, '1');
richTextBox_wys.AppendText("a的补码为:" + tB_A_wys.Text + "\n");
}
else
{
MessageBox.Show("请输入-128~127之间的数字!");
return;
}
}
}
char[] ch1 = tB_A_wys.Text.ToCharArray();
string[] str1 = new string[tB_A_wys.Text.Length];
for (int w = 0; w < tB_A_wys.Text.Length; w++)
{
str1[w] = ch1[w].ToString();
}
tB_A0_wys.Text = str1[7];
tB_A1_wys.Text = str1[6];
tB_A2_wys.Text = str1[5];
tB_A3_wys.Text = str1[4];
tB_A4_wys.Text = str1[3];
tB_A5_wys.Text = str1[2];
tB_A6_wys.Text = str1[1];
tB_A7_wys.Text = str1[0];
if (tB_B_wys.Text != "")
{
int b = int.Parse(this.tB_B_wys.Text);
if (b > 0 && b <= 127)
{
string y = Convert.ToString(b, 2);
this.tB_B_wys.Text = y.ToString().PadLeft(8, '0');
}
else
{
if (b < 0 && b >= -128)
{
//string y1 = Convert.ToString(-b, 2);
//this.textBox_2.Text = (y1.ToString().PadLeft(7, '0')).ToString().PadLeft(8, '1');
//string y2 = Convert.ToString(255 + b, 2);
//this.textBox_3.Text = (y2.ToString().PadLeft(7, '0')).ToString().PadLeft(8, '1');
string y3 = Convert.ToString(256 + b, 2);
this.tB_B_wys.Text = (y3.ToString().PadLeft(7, '0')).ToString().PadLeft(8, '1');
richTextBox_wys.AppendText("b的补码为:" + tB_B_wys.Text);
}
else
{
if (tB_I_wys.Text != "010")
{
MessageBox.Show("请输入-128~127之间的数字!");
}
return;
}
}
}
char[] ch2 = tB_B_wys.Text.ToCharArray();
string[] str2 = new string[tB_B_wys.Text.Length];
for (int w = 0; w < tB_B_wys.Text.Length; w++)
{
str2[w] = ch2[w].ToString();
}
tB_B0_wys.Text = str2[7];
tB_B1_wys.Text = str2[6];
tB_B2_wys.Text = str2[5];
tB_B3_wys.Text = str2[4];
tB_B4_wys.Text = str2[3];
tB_B5_wys.Text = str2[2];
tB_B6_wys.Text = str2[1];
tB_B7_wys.Text = str2[0];
}
catch
{
if (tB_I_wys.Text == "010" && tB_B_wys.Text == "")
{ }
else
{
MessageBox.Show("请输入-128~127之间的数字!");
}
return;
}
}
private void yu()
{
richTextBox_wys.Clear();
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
char[] str1 = new char[8];
for (int i = 0; i < c1.Length; i++)
{
if (c1[i] == '1' && c2[i] == '1')
{
str1[i] = '1';
}
else
{
str1[i] = '0';
}
richTextBox_wys.AppendText(c1[i] + "&" + c2[i] + "=" + str1[i] + "\n");
}
string s1 = new string(str1);
richTextBox_wys.AppendText("—————————\n");
richTextBox_wys.AppendText("结果为:" + s1);
tB_G_wys.Text = "0";
tB_V_wys.Text = "0";
if (s1 == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
char[] ch1 = s1.ToCharArray();
string[] str = new string[s1.Length];
for (int w = 0; w < s1.Length; w++)
{
str[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = str[7];
tB_Y1_wys.Text = str[6];
tB_Y2_wys.Text = str[5];
tB_Y3_wys.Text = str[4];
tB_Y4_wys.Text = str[3];
tB_Y5_wys.Text = str[2];
tB_Y6_wys.Text = str[1];
tB_Y7_wys.Text = str[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
private void huo()
{
richTextBox_wys.Clear();
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
char[] str1 = new char[8];
for (int i = 0; i < c1.Length; i++)
{
if (c1[i] == '0' && c2[i] == '0')
{
str1[i] = '0';
}
else
{
str1[i] = '1';
}
richTextBox_wys.AppendText(c1[i] + "|" + c2[i] + "=" + str1[i] + "\n");
}
string s1 = new string(str1);
richTextBox_wys.AppendText("—————————\n");
richTextBox_wys.AppendText("结果为:" + s1);
tB_G_wys.Text = "0";
tB_V_wys.Text = "0";
if (s1 == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
char[] ch1 = s1.ToCharArray();
string[] str = new string[s1.Length];
for (int w = 0; w < s1.Length; w++)
{
str[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = str[7];
tB_Y1_wys.Text = str[6];
tB_Y2_wys.Text = str[5];
tB_Y3_wys.Text = str[4];
tB_Y4_wys.Text = str[3];
tB_Y5_wys.Text = str[2];
tB_Y6_wys.Text = str[1];
tB_Y7_wys.Text = str[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
private void fei()
{
richTextBox_wys.Clear();
tB_B_wys.Clear();
if(tB_A_wys.Text=="")
{
MessageBox.Show("请将值输入到A框中");
}
char[] c1 = tB_A_wys.Text.ToCharArray();
//char[] c2 = tB_B_wys.Text.ToCharArray();
char[] str1 = new char[8];
//char[] str2 = new char[8];
for (int i = 0; i < c1.Length; i++)
{
if (c1[i] == '1')
{
str1[i] = '0';
}
else
{
str1[i] = '1';
}
richTextBox_wys.AppendText(c1[i] + "~" + "=" + str1[i] + "\n");
}
string s1 = new string(str1);
richTextBox_wys.AppendText("—————————\n");
richTextBox_wys.AppendText("结果为:" + s1 + "\n");
richTextBox_wys.AppendText("—————————\n");
tB_G_wys.Text = "0";
tB_V_wys.Text = "0";
if (s1 == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
char[] ch1 = s1.ToCharArray();
string[] str = new string[s1.Length];
for (int w = 0; w < s1.Length; w++)
{
str[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = str[7];
tB_Y1_wys.Text = str[6];
tB_Y2_wys.Text = str[5];
tB_Y3_wys.Text = str[4];
tB_Y4_wys.Text = str[3];
tB_Y5_wys.Text = str[2];
tB_Y6_wys.Text = str[1];
tB_Y7_wys.Text = str[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
private void TH()
{
richTextBox_wys.Clear();
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
char[] str1 = new char[8];
for (int i = 0; i < c1.Length; i++)
{
if ((c1[i] == '0' && c2[i] == '0') || (c1[i] == '1' && c2[i] == '1'))
{
str1[i] = '1';
}
else
{
str1[i] = '0';
}
richTextBox_wys.AppendText(c1[i] + "同或" + c2[i] + "=" + str1[i] + "\n");
}
string s1 = new string(str1);
richTextBox_wys.AppendText("—————————\n");
richTextBox_wys.AppendText("结果为:" + s1);
tB_G_wys.Text = "0";
tB_V_wys.Text = "0";
if (s1 == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
char[] ch1 = s1.ToCharArray();
string[] str = new string[s1.Length];
for (int w = 0; w < s1.Length; w++)
{
str[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = str[7];
tB_Y1_wys.Text = str[6];
tB_Y2_wys.Text = str[5];
tB_Y3_wys.Text = str[4];
tB_Y4_wys.Text = str[3];
tB_Y5_wys.Text = str[2];
tB_Y6_wys.Text = str[1];
tB_Y7_wys.Text = str[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
private void YH()
{
richTextBox_wys.Clear();
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
char[] str1 = new char[8];
tB_G_wys.Text = "0";
tB_V_wys.Text = "0";
for (int i = 0; i < c1.Length; i++)
{
if ((c1[i] == '0' && c2[i] == '1') || (c1[i] == '1' && c2[i] == '0'))
{
str1[i] = '1';
}
else
{
str1[i] = '0';
}
richTextBox_wys.AppendText(c1[i] + "异或" + c2[i] + "=" + str1[i] + "\n");
}
string s1 = new string(str1);
richTextBox_wys.AppendText("—————————\n");
richTextBox_wys.AppendText("结果为:" + s1);
if (s1 == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
char[] ch1 = s1.ToCharArray();
string[] str = new string[s1.Length];
for (int w = 0; w < s1.Length; w++)
{
str[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = str[7];
tB_Y1_wys.Text = str[6];
tB_Y2_wys.Text = str[5];
tB_Y3_wys.Text = str[4];
tB_Y4_wys.Text = str[3];
tB_Y5_wys.Text = str[2];
tB_Y6_wys.Text = str[1];
tB_Y7_wys.Text = str[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
private void add()
{
richTextBox_wys.Clear();
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
string x = "";
string y = "";
if (c1[0] == '0')
{
x = tB_A_wys.Text.ToString().PadLeft(9, '0');
}
else
{
x = tB_A_wys.Text.ToString().PadLeft(9, '1');
}
char[] s1 = x.ToCharArray();
if (c2[0] == '0')
{
y = tB_B_wys.Text.ToString().PadLeft(9, '0');
}
else
{
y = tB_B_wys.Text.ToString().PadLeft(9, '1');
}
char[] s2 = y.ToCharArray();
int[] i1 = new int[9];
int[] i2 = new int[9];
int[] arr_Add = new int[9];
char[] str = new char[8];
for (int i = 0; i < s1.Length; i++)
{
int temp_int = s1[i] - '0';
i1[i] = temp_int;
}
for (int i = 0; i < s2.Length; i++)
{
int temp_int = s2[i] - '0';
i2[i] = temp_int;
}
int j = 0;
for (int i = s1.Length - 1; i >= 0; i--)
{
if (i1[i] + i2[i] + j > 1)
{
arr_Add[i] = i1[i] + i2[i] + j - 2;
richTextBox_wys.AppendText(i1[i] + "+" + i2[i] + "+" + "低位进位:" + j + "结果为:" + arr_Add[i]);
j = 1;
//char temp_char = char.Parse(j.ToString());
richTextBox_wys.AppendText("进位为:" + j + "\n");
}
else
{
arr_Add[i] = i1[i] + i2[i] + j;
richTextBox_wys.AppendText(i1[i] + "+" + i2[i] + "+" + "低位进位:" + j + "结果为:" + arr_Add[i]);
j = 0;
//char temp_char = char.Parse(j.ToString());
richTextBox_wys.AppendText("进位为:" + j + "\n");
}
}
for (int i = arr_Add.Length - 1; i > 0; i--)
{
char temp_char = char.Parse(arr_Add[i].ToString());
str[i - 1] = temp_char;
}
tB_G_wys.Text = char.Parse(arr_Add[0].ToString()).ToString();
if ((arr_Add[0] == 0 && arr_Add[1] == 0) || (arr_Add[0] == 1 && arr_Add[1] == 1))
{
string s = new string(str);
if (s == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
richTextBox_wys.AppendText("结果为:" + s);
richTextBox_wys.AppendText("\n—————————\n");
richTextBox_wys.AppendText("不溢出");
tB_V_wys.Text = "0";
char[] ch1 = s.ToCharArray();
string[] st = new string[s.Length];
for (int w = 0; w < s.Length; w++)
{
st[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = st[7];
tB_Y1_wys.Text = st[6];
tB_Y2_wys.Text = st[5];
tB_Y3_wys.Text = st[4];
tB_Y4_wys.Text = st[3];
tB_Y5_wys.Text = st[2];
tB_Y6_wys.Text = st[1];
tB_Y7_wys.Text = st[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
else
{
string s = new string(str);
if (s == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
richTextBox_wys.AppendText("结果为:" + s);
richTextBox_wys.AppendText("\n—————————\n");
richTextBox_wys.AppendText("溢出!");
tB_V_wys.Text = "1";
char[] ch1 = s.ToCharArray();
string[] st = new string[s.Length];
for (int w = 0; w < s.Length; w++)
{
st[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = st[7];
tB_Y1_wys.Text = st[6];
tB_Y2_wys.Text = st[5];
tB_Y3_wys.Text = st[4];
tB_Y4_wys.Text = st[3];
tB_Y5_wys.Text = st[2];
tB_Y6_wys.Text = st[1];
tB_Y7_wys.Text = st[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
}
private void subtraction()
{
richTextBox_wys.Clear();
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
string x = "";
string y = "";
if (c1[0] == '0')
{
x = tB_A_wys.Text.ToString().PadLeft(9, '0');
}
else
{
x = tB_A_wys.Text.ToString().PadLeft(9, '1');
}
char[] s1 = x.ToCharArray();
if (c2[0] == '0')
{
y = tB_B_wys.Text.ToString().PadLeft(9, '0');
}
else
{
y = tB_B_wys.Text.ToString().PadLeft(9, '1');
}
char[] s2 = y.ToCharArray();
int[] i1 = new int[x.Length];
int[] i2 = new int[y.Length];
int[] arr_Add = new int[x.Length];
char[] str = new char[x.Length - 1];
for (int i = 0; i < s1.Length; i++)
{
int temp_int = s1[i] - '0';
i1[i] = temp_int;
}
for (int i = 0; i < s2.Length; i++)
{
if (s2[i] == '0')
{
s2[i] = '1';
int temp_int = s2[i] - '0';
i2[i] = temp_int;
}
else
{
s2[i] = '0';
int temp_int = s2[i] - '0';
i2[i] = temp_int;
}
}
int j = 0;
for (int i = s1.Length - 1; i >= 0; i--)
{
if (i == s1.Length - 1)
{
if (i1[i] + i2[i] + 1 > 1)
{
arr_Add[i] = i1[i] + i2[i] + 1 - 2;
j = 1;
}
else
{
arr_Add[i] = i1[i] + i2[i] + 1;
j = 0;
}
}
else
{
if (i1[i] + i2[i] + j > 1)
{
arr_Add[i] = i1[i] + i2[i] + j - 2;
j = 1;
}
else
{
arr_Add[i] = i1[i] + i2[i] + j;
j = 0;
}
}
}
for (int i = arr_Add.Length - 1; i > 0; i--)
{
char temp_char = char.Parse(arr_Add[i].ToString());
str[i - 1] = temp_char;
}
tB_G_wys.Text = char.Parse(arr_Add[0].ToString()).ToString();
if ((arr_Add[0] == 0 && arr_Add[1] == 0) || (arr_Add[0] == 1 && arr_Add[1] == 1))
{
string s = new string(str);
if (s == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
richTextBox_wys.AppendText("结果为:" + s);
richTextBox_wys.AppendText("\n—————————\n");
richTextBox_wys.AppendText("不溢出");
tB_V_wys.Text = "0";
char[] ch1 = s.ToCharArray();
string[] st = new string[s.Length];
for (int w = 0; w < s.Length; w++)
{
st[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = st[7];
tB_Y1_wys.Text = st[6];
tB_Y2_wys.Text = st[5];
tB_Y3_wys.Text = st[4];
tB_Y4_wys.Text = st[3];
tB_Y5_wys.Text = st[2];
tB_Y6_wys.Text = st[1];
tB_Y7_wys.Text = st[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
else
{
string s = new string(str);
if (s == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
richTextBox_wys.AppendText("结果为:" + s);
richTextBox_wys.AppendText("\n—————————\n");
richTextBox_wys.AppendText("溢出!");
tB_V_wys.Text = "1";
char[] ch1 = s.ToCharArray();
string[] st = new string[s.Length];
for (int w = 0; w < s.Length; w++)
{
st[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = st[7];
tB_Y1_wys.Text = st[6];
tB_Y2_wys.Text = st[5];
tB_Y3_wys.Text = st[4];
tB_Y4_wys.Text = st[3];
tB_Y5_wys.Text = st[2];
tB_Y6_wys.Text = st[1];
tB_Y7_wys.Text = st[0];
tB_N_wys.Text = tB_Y7_wys.Text;
}
}
private void mutiply()
{
richTextBox_wys.Clear();
//richTextBox_wys.Text = tB_A_wys.Text;
//richTextBox_wys.AppendText("\n" + tB_B_wys.Text + "\n");
//richTextBox_wys.AppendText("—————————\n");
char[] c1 = tB_A_wys.Text.ToCharArray();
char[] c2 = tB_B_wys.Text.ToCharArray();
char[] str1 = new char[8];
char[] str2 = new char[7];
int[] X = new int[8];
int[] Y = new int[7];
int[] Add = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };//用来存放部分积
richTextBox_wys.AppendText("部分积:00000000" +"\n"+"被乘数:" + tB_B_wys.Text + "\n");
richTextBox_wys.AppendText("—————————\n");
for (int i = 0; i < c1.Length; i++)
{
int temp_int = c1[i] - '0';
X[i] = temp_int;
}
for (int i = 0; i < c2.Length - 1; i++)
{
int temp_int = c2[i + 1] - '0';
Y[i] = temp_int;
}
for (int i = Y.Length - 1; i >= 0; i--)
{
if (Y[Y.Length - 1] == 0) //若乘数最后一位为零
{
for (int j = Y.Length - 1; j > 0; j--) //乘数后移一位
{
Y[j] = Y[j - 1];
}
Y[0] = Add[7]; //部分积的最后一位放入乘数数组前
for (int j = Add.Length - 1; j > 0; j--)//部分积后移一位
{
Add[j] = Add[j - 1];
}
Add[0] = 0;
for (int x = Add.Length - 1; x >= 0; x--)
{
char temp_char = char.Parse(Add[x].ToString());
str1[x] = temp_char;
}
for (int x = Y.Length - 1; x >= 0; x--)
{
char temp_char = char.Parse(Y[x].ToString());
str2[x] = temp_char;
}
string s1 = new string(str1);
string s2 = new string(str2);
tB_Regi_wys.Text = s2;
richTextBox_wys.AppendText("右移一位:" + "\n" + "部分积:" + s1 + "\n" + "寄存器:" + s2 + "\n");
richTextBox_wys.AppendText("—————————\n");
}
else
{ //若乘数最后一位为一
for (int j = Y.Length - 1; j > 0; j--)//乘数后移一位
{
Y[j] = Y[j - 1];
}
int a = 0; //部分积与被乘数相加
for (int b = X.Length - 1; b >= 0; b--)
{
if (X[b] + Add[b] + a > 1)
{
Add[b] = Add[b] + X[b] + a - 2;
a = 1;
}
else
{
Add[b] = Add[b] + X[b] + a;
a = 0;
}
}
Y[0] = Add[7]; //部分积最后一位放到乘数的最前面
for (int j = Add.Length - 1; j > 0; j--)//部分积后移一位
{
Add[j] = Add[j - 1];
}
Add[0] = 0;
for (int x = Add.Length - 1; x >= 0; x--)
{
char temp_char = char.Parse(Add[x].ToString());
str1[x] = temp_char;
}
for (int x = Y.Length - 1; x >= 0; x--)
{
char temp_char = char.Parse(Y[x].ToString());
str2[x] = temp_char;
}
string s1 = new string(str1);
string s2 = new string(str2);
tB_Regi_wys.Text = s2;
richTextBox_wys.AppendText("右移一位:" + "\n" + "部分积:" + s1 + "\n" + "寄存器:" + s2 + "\n");
richTextBox_wys.AppendText("—————————\n");
}
}
string ss1 = new string(str1);
string ss2 = new string(str2);
char[] ch1 = ss1.ToCharArray();
string[] str = new string[ss1.Length];
for (int w = 0; w < ss1.Length; w++)
{
str[w] = ch1[w].ToString();
}
tB_Y0_wys.Text = str[7];
tB_Y1_wys.Text = str[6];
tB_Y2_wys.Text = str[5];
tB_Y3_wys.Text = str[4];
tB_Y4_wys.Text = str[3];
tB_Y5_wys.Text = str[2];
tB_Y6_wys.Text = str[1];
tB_Y7_wys.Text = str[0];
tB_N_wys.Text = tB_Y7_wys.Text;
tB_G_wys.Text = tB_Y7_wys.Text;
StringBuilder s = new StringBuilder(ss1);
s.Append(ss2);
//this.tB_Clock.Text = s.ToString(); //输出最后结果
richTextBox_wys.AppendText("结果为:" + s.ToString());
if (s.ToString() == "00000000")
{
tB_Z_wys.Text = "1";
}
else
{
tB_Z_wys.Text = "0";
}
tB_V_wys.Text = "0";
}
}
}