示例#1
0
        public static void VisualCommandFDiv(string a1, string a2)
        {
            SoReg fir = SCPU.reg[a1] as SoReg;
            SoReg sec = SCPU.reg[a2] as SoReg;

            string f   = fir.man;
            string s   = sec.man;
            int    exp = fir.exp - sec.exp;

            Console.WriteLine("First  value S {0} E {1} M{2}", fir.sign, fir.or, fir.man);
            Console.WriteLine("Second value S {0} E {1} M{2}", sec.sign, sec.or, sec.man);
            //Console.WriteLine(Convert.ToString(exp + 127, 2));
            //Console.WriteLine("{0, 55}", fir.man);
            //Console.WriteLine("{0, 55}", sec.man);

            int i = 0;

            f += new string('0', 23);

            bool   exit = SCPU.MExit;
            int    step = 0;
            string help = "";
            string ans  = "";
            int    u    = 0;

            Console.WriteLine(f.PadLeft(f.Length + 9));
            Console.WriteLine(s.PadLeft(s.Length + 9));

            for (; u < 23; ++u)
            {
                help += f[u];
            }
            while (true)
            {
                Console.WriteLine("Step {0}: {1}{2}", (++step).ToString("D2"), new string(' ', i++), help);
                if (u >= f.Length)
                {
                    break;
                }
                if (!exit)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        exit = true;
                    }
                }

                if (Checker.Equals(help, s))
                {
                    ans += "1";
                    int           over = 0;
                    StringBuilder A    = new StringBuilder(new string('0', help.Length));
                    s = new String('0', help.Length - s.Length) + s;
                    for (int j = help.Length - 1; j > -1; --j)
                    {
                        if (help[j] == '1' && s[j] == '1')
                        {
                            if (over == 0)
                            {
                                A[j] = '0';
                                over = 0;
                            }
                            else
                            {
                                A[j] = '1';
                                over = 1;
                            }
                        }
                        else
                        if (help[j] == '0' && s[j] == '0')
                        {
                            if (over == 0)
                            {
                                A[j] = '0';
                                over = 0;
                            }
                            else
                            {
                                A[j] = '1';
                                over = 1;
                            }
                        }
                        else
                        if (help[j] == '1' && s[j] == '0')
                        {
                            if (over == 0)
                            {
                                A[j] = '1';
                                over = 0;
                            }
                            else
                            {
                                A[j] = '0';
                                over = 0;
                            }
                        }
                        else
                        if (help[j] == '0' && s[j] == '1')
                        {
                            if (over == 0)
                            {
                                A[j] = '1';
                                over = 1;
                            }
                            else
                            {
                                A[j] = '0';
                                over = 1;
                            }
                        }
                    }

                    help  = A.ToString();
                    help  = help.TrimStart('0');
                    s     = s.TrimStart('0');
                    help += f[u++];
                }
                else
                {
                    ans  += "0";
                    help += f[u++];
                }
            }

            Console.WriteLine("Answer: " + ans);


            ans = ans.TrimStart('0');

            if (ans.Length > 22)
            {
                ans = ans.Substring(0, 23);
            }

            int v = 0;

            if (fir.val / sec.val < 0)
            {
                v = 1;
            }

            Console.WriteLine("Answer: S {0} E {1} M {2} = {3}", v, Convert.ToString(exp + 127, 2), ans + new string('0', 23 - ans.Length), fir.val / sec.val);
        }
示例#2
0
        public static void VisualCommandDIV(long val1, long val2, string reg)
        {
            string first = "", second = "";
            bool   isNeg = Checker.IsNegativeResultForMUL(val1, val2);

            Checker.ConvertBinaryValueForDIV(val1, val2, ref first, ref second);
            if (Math.Abs(val1) < Math.Abs(val2))
            {
                VisualIfNothingNot(val1, val2, first, second, reg);
                return;
            }
            string ans = "";

            string help = "";
            int    step = 0, len = Math.Max(first.Length, second.Length), padleft = 0;

            Console.WriteLine("{0, -7}| {1}| {2}{4}{3}", "Step:", first, second, "|Answer:", new string(' ', first.Length * 2 + 2 - second.Length));

            bool exit = false;
            int  i    = 0;

            while (true)
            {
                if (!exit)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        exit = true;
                    }
                }

                help += first[i];
                int l = help.Length;
                help     = help.TrimStart('0');
                padleft += (l - help.Length);

                Console.Write("Step{0} | {1}", (++step).ToString("D2"), (help == "" ? "0" : help).PadLeft(padleft + help.Length));
                if (!Checker.Equals(help, second))
                {
                    ans += "0";
                    ans  = ans.TrimStart('0');
                }
                else
                {
                    ans     += "1";
                    l        = help.Length;
                    help     = ALU.SUBforDiv(help, second);
                    padleft += (l - help.Length);
                }
                Console.WriteLine("{1}|{0}", ans, new string(' ', first.Length * 3 - help.Length - padleft + 4));
                if (++i == first.Length)
                {
                    break;
                }
            }

            if (ans == "")
            {
                ans = "0";
            }
            if (help == "")
            {
                help = "0";
            }

            long div = Convert.ToInt64(ans, 2);
            long mod = Convert.ToInt64(help, 2);

            if (isNeg)
            {
                div = -div;
                mod = -mod;
            }

            Console.WriteLine("\nAnswer = {0} = {1}({2})", ans, div, div.ToString("X4").Substring(div.ToString("X4").Length - 4));
            Console.WriteLine("Modulo = {0} = {1}({2})", help, mod, mod.ToString("X4").Substring(mod.ToString("X4").Length - 4));
            if (reg[1] == 'l' || reg[1] == 'h')
            {
                Console.WriteLine("AH = {1}, AL = {0}", div.ToString("X2").Substring(div.ToString("X").Length - 4), mod.ToString("X2").Substring(mod.ToString("X2").Length - 4));
            }
            else
            {
                Console.WriteLine("AX = {0}, DX = {1}", div.ToString("X4").Substring(div.ToString("X4").Length - 4), mod.ToString("X4").Substring(mod.ToString("X4").Length - 4));
            }
        }