public static string SUBforDiv(string val1, string val2) { if (val1.Length > val2.Length) { val2 = "0" + val2; } string s = ""; StringBuilder ans = new StringBuilder(new string('0', val1.Length)); int over = 0; val2 = Checker.NEG(val2); for (int i = val1.Length - 1; i > -1; i--) { if (val1[i] == '1' && val2[i] == '1') { if (over == 0) { ans[i] = '0'; } else { ans[i] = '1'; } over = 1; } else if (val1[i] == '0' && val2[i] == '0') { if (over == 0) { ans[i] = '0'; } else { ans[i] = '1'; } over = 0; } else { if (over == 0) { ans[i] = '1'; over = 0; } else { ans[i] = '0'; over = 1; } } } s = ans.ToString(); s = s.TrimStart('0'); return(s == "" ? "0" : s); }
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); }