Ejemplo n.º 1
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));
            }
        }
Ejemplo n.º 2
0
        public static void VisualCommandMUL(string val)
        {
            int    val1 = (CPU.registors["a"] as Register).ReturnNumber();
            int    val2 = (CPU.registors[val[0].ToString()] as Register).ReturnNumber(val);
            string first = "", second = "";

            Checker.ConvertBinaryValueForDIV(val1, val2, ref first, ref second);
            //Console.WriteLine(Convert.ToString(val2, 2));
            string        help = new string('0', first.Length + second.Length);
            StringBuilder ans  = new StringBuilder(help);

            int len = ans.Length;

            int  step = 0;
            bool exit = false;

            Console.WriteLine("{0, -7}| {1}", "AX", first.PadLeft(len));
            Console.WriteLine("{0, -7}| {1}", val.ToUpper(), second.PadLeft(len));
            Console.WriteLine(new string('-', ans.Length + 9));

            int i = second.Length;

            //int t = second.Length;

            while (true)
            {
                i--;
                //t--;
                if (i < 0)
                {
                    break;
                }
                help = "";

                for (int j = 0; j < first.Length; j++)
                {
                    if (first[j] == '0' || second[i] == '0')
                    {
                        help += '0';
                    }
                    else
                    {
                        help += '1';
                    }
                }

                int k = first.Length - 1;
                int over = 0, j1 = 0;
                for (int j = len - 1; k > -1; j--, k--)
                {
                    int prval = ((ans[j] - '0') + over + (help[k] - '0'));
                    ans[j] = (char)(prval % 2 + '0');
                    over   = prval / 2;
                    j1     = j;
                }
                if (over > 0)
                {
                    ans[j1 - 1] = '1';
                }

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

                Console.WriteLine("step {0}| {1}", (++step).ToString("D2"), help.PadLeft(len--));
            }

            Console.WriteLine(new string('-', ans.Length + 9));
            Console.WriteLine("{0, -7}| {1}", "answer", ans);

            string Ans = ans.ToString();

            if (Ans.Length > 16)
            {
                Ans = Ans.Substring(Ans.Length - 16);
            }

            int x;

            if (Checker.IsNegativeResultForMUL(val1, val2))
            {
                x   = val1 * val2;
                Ans = Checker.NEG(Ans);
                Console.WriteLine("Answer: {0} => {1} = {2} ({3})\n", ans, Ans, x, x.ToString("X"));
            }
            else
            {
                x = Convert.ToInt32(Ans, 2);
                Console.WriteLine("Answer: {0} => {1} = {2} ({3})\n", ans, Ans, x, x.ToString("X"));
            }

            help = x.ToString("X8");
            if (help.Length > 8)
            {
                help = help.Substring(help.Length - 7);
            }

            Console.WriteLine("Result: {0} = {1}({4}), DX = {2}, AX = {3}\n", Ans, x, help.Substring(0, 4), help.Substring(4), help);
        }