public override void paso1(DataGridView dataGridViewIntermedio) { string[] codigo; MyErrorListener errorListener; this.lineasError = new List <bool>(); this.errores = new List <string>(); this.intermedio = new List <string>(); this.tabSim = new Dictionary <string, long>(); for (int i = 0; i < this.archivo.Count; i++) { lexer = new SICExtendidaLexer(new AntlrInputStream(this.Archivo[i])); tokens = new CommonTokenStream(lexer); parser = new SICExtendidaParser(tokens); errorListener = new MyErrorListener(i + 1); parser.AddErrorListener(errorListener); parser.prog(); codigo = this.Archivo[i].Split('\t'); this.lineasError.Add(errorListener.ExisteError); if (!errorListener.ExisteError) { try { if (!codigo[1].Equals("START") & !codigo[1].Equals("END") & !codigo[1].Equals("BASE")) { this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "No"); if (!string.IsNullOrEmpty(codigo[0])) { this.TabSim.Add(codigo[0], this.cp); } this.incrementaInstruccionDirectiva(codigo); } else if (codigo[1].Equals("START")) { codigo[2] = codigo[2].ToUpper(); if (codigo[2].Contains("H")) { this.cp = MetodosAuxiliares.hexadecimalADecimal(codigo[2].Replace("H", "")); this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "No"); } else { this.cp = long.Parse(codigo[2]); this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "no"); } } else if (codigo[1].Equals("END") | codigo[1].Equals("BASE")) { //this.intermedio.Add(this.cp.ToString() + "\t" + this.archivo[i]); this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "no"); } } catch (ArgumentException) { this.errores.Add("Linea" + (i + 1).ToString() + ": Error Simbolo repetido"); dataGridViewIntermedio.Rows.Remove(dataGridViewIntermedio.Rows[dataGridViewIntermedio.Rows.Count - 1]); this.intermedio.Remove(this.intermedio.Last()); this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "Simbolo"); this.incrementaInstruccionDirectiva(codigo); } } else { if (!string.IsNullOrEmpty(this.Archivo[i])) { if (this.instruccionesFormato1.Keys.Contains(codigo[1]) || this.instruccionesFormato2.Keys.Contains(codigo[1]) || this.instruccionesFormato3.Keys.Contains(codigo[1]) || this.Directivas.Contains(codigo[1])) { this.errores.Add("Linea" + (i + 1).ToString() + ": Error de sintaxis la etiqueta no puede ser la palabra reservada \"" + codigo[0] + "\""); this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "Syntax"); } else { this.errores.Add("Linea" + (i + 1).ToString() + ": Error de sintaxis el operando: \"" + codigo[2] + "\" Esta mal escrito"); this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "Syntax"); } } else { this.errores.Add("Linea" + (i + 1).ToString() + ": Error de sintaxis no debe haber lineas vacias"); codigo = new string[] { "\t", "\t", "\t" }; this.ensamblaIntermedio(dataGridViewIntermedio, codigo, i, "Vacia"); } } } this.paso1Logrado = true; }
protected override void creaArchivoObj() { string linea; string[] instruccion; this.archivoObj = new List <string>(); instruccion = this.intermedio.First().Split('\t'); linea = instruccion[3].Replace("H", ""); linea = linea.Replace("h", ""); linea = "H" + MetodosAuxiliares.ajustaCadena(instruccion[1], 6) + MetodosAuxiliares.ajustaDireccion(linea) + MetodosAuxiliares.ajustaDireccion(MetodosAuxiliares.decimalAHexadecimal(this.tamaño)); this.archivoObj.Add(linea); this.generaRegistrosT(); instruccion = this.intermedio.Last().Split('\t'); linea = "E" + this.calculaDireccionEnd(instruccion[3]); this.archivoObj.Add(linea); }
public static long FinalPrograma(string inicio, string tamaño) { return(MetodosAuxiliares.hexadecimalADecimal(inicio) + MetodosAuxiliares.hexadecimalADecimal(tamaño)); }
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { string[] codigo; switch (e.ClickedItem.AccessibleName) { case "Abrir": #region Abrir if (openFileProgram.ShowDialog().Equals(DialogResult.OK)) { this.limpiaDataGrids(); if (extendida) { this.ensamblador = new EnsambladorSICExtendida(openFileProgram.FileName); } else { this.ensamblador = new EnsambladorSICEstandar(openFileProgram.FileName); } for (int i = 0; i < this.ensamblador.Archivo.Count; i++) { codigo = this.ensamblador.Archivo[i].Split('\t'); if (codigo.Length == 3) { this.dataGridViewFuente.Rows.Add(i + 1, codigo[0], codigo[1], codigo[2]); } else if (codigo.Length == 2) { this.dataGridViewFuente.Rows.Add(i + 1, codigo[0], codigo[1], ""); } else { this.dataGridViewFuente.Rows.Add(i + 1, "", "", ""); } } } #endregion break; case "Analizar": #region Analizar if (this.ensamblador != null) { this.dataGridViewIntermedio.Rows.Clear(); this.ensamblador.paso1(this.dataGridViewIntermedio); this.ensamblador.Tamaño = MetodosAuxiliares.calculaTamaño(this.ensamblador.Intermedio.First(), this.ensamblador.Intermedio.Last()); this.llenaTabSimYErrores(); MetodosAuxiliares.grabaTabSim(this.ensamblador.Nombre, this.ensamblador.TabSim); MetodosAuxiliares.grabaIntermedio(this.ensamblador.Nombre, this.ensamblador.Intermedio); if (this.ensamblador.Errores.Count != 0) { MetodosAuxiliares.grabaErrores(this.ensamblador.Nombre, this.ensamblador.Errores); } } else { MessageBox.Show("Por favor abra un archivo primero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } #endregion break; case "ArchivoObj": #region Archivo Obj if (this.ensamblador != null) { if (this.ensamblador.Paso1Logrado) { this.ensamblador.paso2(this.dataGridViewIntermedio); this.richTextBoxObj.Text += "\n"; foreach (string linea in this.ensamblador.ArchivoObj) { this.richTextBoxObj.Text += linea + "\n"; } MetodosAuxiliares.grabaObj(this.ensamblador.Nombre, this.ensamblador.ArchivoObj); } } else { MessageBox.Show("Por favor abra un archivo primero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } #endregion break; case "Texto": EditorDeCodigo editor; editor = new EditorDeCodigo(this.extendida); editor.ShowDialog(); break; case "EjecutaObj": Ejecucion ejecucion; ejecucion = new Ejecucion(); ejecucion.ShowDialog(); break; case "Cambio": this.seleccionaArquitectura(); break; } }
public override void paso2(DataGridView dataGridViewIntermedio) { bool x; int resto; string aux; char[] cadena; string[] linea; this.codigoObj = new string[this.intermedio.Count]; for (int i = 0; i < this.intermedio.Count; i++) { linea = this.intermedio[i].Split('\t'); if (!string.IsNullOrEmpty(linea[2])) { if (!this.lineasError[i]) { if (this.instrucciones.Keys.Contains(linea[2]) | linea[2].Equals("WORD") | linea[2].Equals("BYTE")) { switch (linea[2]) { case "BYTE": #region Byte if (linea[3].Contains("X") | linea[3].Contains("x")) { //FE codigoObj[i] = linea[3].Replace("X'", ""); codigoObj[i] = codigoObj[i].Replace("x'", ""); codigoObj[i] = codigoObj[i].Replace("'", ""); } else { codigoObj[i] = linea[3].Replace("C'", ""); codigoObj[i] = codigoObj[i].Replace("c'", ""); codigoObj[i] = codigoObj[i].Replace("'", ""); codigoObj[i] = MetodosAuxiliares.ASCIIToHexadecimal(codigoObj[i]); } #endregion break; case "WORD": #region WORD x = linea[3].Contains("H") | linea[3].Contains("h"); codigoObj[i] = linea[3].Replace("h", ""); codigoObj[i] = linea[3].Replace("H", ""); if (!x) { codigoObj[i] = MetodosAuxiliares.decimalAHexadecimal(int.Parse(codigoObj[i])); } if (codigoObj[i].Length < 6) { resto = 6 - codigoObj[i].Length; aux = ""; for (int j = 0; j < resto; j++) { aux += "0"; } codigoObj[i] = aux + codigoObj[i]; } else if (codigoObj[i].Length > 6) { codigoObj[i] = codigoObj[i].Substring(0, 6); } #endregion break; default: #region Instrucciones if (!linea[2].Equals("RSUB")) { linea[3] = linea[3].Replace(" ", ""); x = linea[3].Contains(",X") | linea[3].Contains(",x"); linea[3] = linea[3].Replace(",X", ""); linea[3] = linea[3].Replace(",x", ""); if (this.tabSim.Keys.Contains(linea[3])) { codigoObj[i] = this.instrucciones[linea[2]] + MetodosAuxiliares.decimalAHexadecimal(this.tabSim[linea[3]]); } else { codigoObj[i] = this.instrucciones[linea[2]] + "7FFF"; } if (x) { codigoObj[i] = MetodosAuxiliares.hexadecimalABinario(codigoObj[i]); cadena = codigoObj[i].ToCharArray(); cadena[8] = '1'; codigoObj[i] = new string(cadena); codigoObj[i] = MetodosAuxiliares.decimalAHexadecimal(MetodosAuxiliares.binarioADecimal(codigoObj[i])); } } else { codigoObj[i] = this.instrucciones[linea[2]] + "0000"; } #endregion break; } } else { codigoObj[i] = "---"; } } else { codigoObj[i] = "Error: de Sintaxis"; } } else { this.codigoObj[i] = "Error: Linea vacia"; } } this.ensamblaIntermedio(dataGridViewIntermedio); this.creaArchivoObj(); }
private void llenaDataGrid() { this.limpiaDatagrid(); string[] fila; fila = new string[17]; for (int i = 0; i < sicEstandar.Memoria.Filas; i++) { fila[0] = MetodosAuxiliares.decimalAHexadecimal(MetodosAuxiliares.hexadecimalADecimal(sicEstandar.Memoria.Inicio) + (i * 16)); for (int j = 0; j < sicEstandar.Memoria.Columnas; j++) { fila[j + 1] = sicEstandar.Memoria.Mapa[i, j]; } this.dataGridViewMapaDeMemoria.Rows.Add(fila); } this.dataGridViewRegistros.Rows.Add("CP", CP, MetodosAuxiliares.ajustaDireccion(MetodosAuxiliares.decimalAHexadecimal(this.sicEstandar.CP))); this.dataGridViewRegistros.Rows.Add("A", A, MetodosAuxiliares.ajustaDireccion(MetodosAuxiliares.decimalAHexadecimal(this.sicEstandar.A))); this.dataGridViewRegistros.Rows.Add("X", X, MetodosAuxiliares.ajustaDireccion(MetodosAuxiliares.decimalAHexadecimal(this.sicEstandar.X))); this.dataGridViewRegistros.Rows.Add("L", L, MetodosAuxiliares.ajustaDireccion(MetodosAuxiliares.decimalAHexadecimal(this.sicEstandar.L))); this.dataGridViewRegistros.Rows.Add("CC", this.sicEstandar.CC, ""); this.label1.Text = "Tamaño del programa:" + this.sicEstandar.Memoria.Tamaño; }
protected override void generaRegistrosT() { int i; int j; string registroT; string aux; string bytes; aux = ""; i = 1; while (!this.archivo[i].Contains("END")) { registroT = "T" + MetodosAuxiliares.ajustaDireccion(this.intermedio[i].Split('\t').First()); for (j = i; j < this.codigoObj.Length - 1; j++) { if (!this.codigoObj[j].Contains("Error")) { if (this.codigoObj[j].Contains("*")) { string cpHex; cpHex = MetodosAuxiliares.decimalAHexadecimal(MetodosAuxiliares.hexadecimalADecimal(this.intermedio[j].Split('\t').First()) +1); this.relocalizables.Add(MetodosAuxiliares.ajustaOperando(cpHex,6,"0")); this.codigoObj[j] = this.codigoObj[j].Replace("*", ""); } if (((aux.Length + this.codigoObj[j].Length) / 2) > 30) { break; } else if (this.codigoObj[j].Equals("---") & !this.archivo[j].Contains("BASE")) { break; } else if(!this.archivo[j].Contains("BASE")) { aux += this.codigoObj[j]; } } } bytes = MetodosAuxiliares.decimalAHexadecimal(aux.Length / 2); if (bytes.Length < 2) { bytes = "0" + bytes; } registroT += bytes; registroT += aux; aux = ""; if (registroT.Length > 9) { this.archivoObj.Add(registroT); } for (int k = j; k < this.codigoObj.Length; k++) { if (!codigoObj[k].Equals("---") | this.intermedio[k].Contains("END")) { i = k; break; } } } }
private string ensamblaInstruccion3(string codOp, string operando, string cpSig) { string codigoObj; char[] nixbpe; codigoObj = ""; nixbpe = new char[6]; codigoObj = MetodosAuxiliares.hexadecimalABinario(this.instruccionesFormato3[codOp]).Substring(4,2); nixbpe[5] = '0'; if (!codOp.Equals("RSUB")) { codOp = this.instruccionesFormato3[codOp][0].ToString(); switch (operando.First()) { case '@'://Direccionamiento Indirecto nixbpe[0] = '1'; nixbpe[1] = '0'; nixbpe[2] = '0'; operando = operando.Replace("@",""); break; case '#'://Direccionamiento Inmediato nixbpe[0] = '0'; nixbpe[1] = '1'; nixbpe[2] = '0'; operando = operando.Replace("#", ""); break; default://Direccionamiento Simple nixbpe[0] = '1'; nixbpe[1] = '1'; if (operando.Contains(",X") | operando.Contains(",x")) { operando = operando.Replace(",X", ""); operando = operando.Replace(",x", ""); nixbpe[2] = '1'; } else { nixbpe[2] = '0'; } break; } if (this.tabSim.Keys.Contains(operando)) { this.determinaBanderas(ref nixbpe, ref operando, cpSig); } else { bool h; h = operando.Last().Equals('H') | operando.Last().Equals('h'); if (h) { operando = operando.Replace("H", ""); operando = operando.Replace("h", ""); } if (Information.IsNumeric(operando)) { if (h) { operando = MetodosAuxiliares.hexadecimalADecimal(operando).ToString(); } int c; c = int.Parse(operando); operando = MetodosAuxiliares.decimalAHexadecimal(c); if (0 <= c & c <= 4095) { nixbpe[3] = '0'; nixbpe[4] = '0'; operando = MetodosAuxiliares.ajustaOperando(operando, 3, "0"); } else { this.determinaBanderas(ref nixbpe, ref operando, cpSig); } } else { nixbpe[3] = '1'; nixbpe[4] = '1'; operando = "FFF"; } } codigoObj += new string(nixbpe); codigoObj = codOp + MetodosAuxiliares.decimalAHexadecimal(MetodosAuxiliares.binarioADecimal(codigoObj)) + operando; } else { codigoObj = "4F0000"; } return codigoObj; }
private string ensamblaInstruccion4(string codOp, string operando) { string codigoObj; char[] nixbpe; codigoObj = ""; long dir; int resto; nixbpe = new char[6]; codigoObj = MetodosAuxiliares.hexadecimalABinario(this.instruccionesFormato4[codOp]).Substring(0, 6); nixbpe[3] = '0'; nixbpe[4] = '0'; nixbpe[5] = '1'; if (!codOp.Equals("RSUB")) { switch (operando.First()) { case '@'://Direccionamiento Indirecto nixbpe[0] = '1'; nixbpe[1] = '0'; nixbpe[2] = '0'; operando = operando.Replace("@", ""); break; case '#'://Direccionamiento Inmediato nixbpe[0] = '0'; nixbpe[1] = '1'; nixbpe[2] = '0'; operando = operando.Replace("#", ""); break; default://Direccionamiento Simple nixbpe[0] = '1'; nixbpe[1] = '1'; if (operando.Contains(",X") | operando.Contains(",x")) { operando = operando.Replace(",X", ""); operando = operando.Replace(",x", ""); nixbpe[2] = '1'; } else { nixbpe[2] = '0'; } break; } if (tabSim.TryGetValue(operando, out dir)) { operando = MetodosAuxiliares.decimalAHexadecimal(dir); } else { bool h; h = operando.Last().Equals('H') | operando.Last().Equals('h'); if (h) { operando = operando.Replace("H", ""); operando = operando.Replace("h", ""); } if (Information.IsNumeric(operando)) { if (h) { operando = MetodosAuxiliares.hexadecimalADecimal(operando).ToString(); } int c; c = int.Parse(operando); if (c > 4095) { operando = MetodosAuxiliares.decimalAHexadecimal(c); } else { operando = "FFFFF"; } } else { operando = "FFFFF"; } } if (MetodosAuxiliares.hexadecimalADecimal(operando) > 0) { operando = MetodosAuxiliares.ajustaOperando(operando, 5, "0"); } else { operando = MetodosAuxiliares.ajustaOperando(operando, 5, "F"); } codigoObj = MetodosAuxiliares.decimalAHexadecimal(MetodosAuxiliares.binarioADecimal(codigoObj + new string(nixbpe))) + operando; codigoObj += "*"; } else { codigoObj = "4F100000*"; } return codigoObj; }
public override void paso2(DataGridView dataGridViewIntermedio) { bool x; int resto; string aux; string[] linea; string[] lineaSig; this.asignaBase(); this.codigoObj = new string[this.intermedio.Count]; for (int i = 0; i < this.intermedio.Count; i++) { linea = this.intermedio[i].Split('\t'); if (!string.IsNullOrEmpty(linea[2])) { if (!this.lineasError[i]) { if (this.instruccionesFormato1.Keys.Contains(linea[2])) { this.codigoObj[i] = this.instruccionesFormato1[linea[2]]; } else if(this.instruccionesFormato2.Keys.Contains(linea[2])) { this.codigoObj[i] = this.ensamblaInstruccion2(linea[2], linea[3]); } else if (this.instruccionesFormato3.Keys.Contains(linea[2])) { lineaSig = this.intermedio[i+1].Split('\t'); this.codigoObj[i] = this.ensamblaInstruccion3(linea[2], linea[3], lineaSig[0]); } else if (this.instruccionesFormato4.Keys.Contains(linea[2])) { this.codigoObj[i] = this.ensamblaInstruccion4(linea[2], linea[3]); } else { switch (linea[2]) { case "BYTE": #region Byte if (linea[3].Contains("X") | linea[3].Contains("x")) { //FE codigoObj[i] = linea[3].Replace("X'", ""); codigoObj[i] = codigoObj[i].Replace("x'", ""); codigoObj[i] = codigoObj[i].Replace("'", ""); if (codigoObj[i].Length % 2 != 0) { codigoObj[i] = "0" + codigoObj[i]; } } else { codigoObj[i] = linea[3].Replace("C'", ""); codigoObj[i] = codigoObj[i].Replace("c'", ""); codigoObj[i] = codigoObj[i].Replace("'", ""); codigoObj[i] = MetodosAuxiliares.ASCIIToHexadecimal(codigoObj[i]); } #endregion break; case "WORD": #region WORD x = linea[3].Contains("H") | linea[3].Contains("h"); codigoObj[i] = linea[3].Replace("h", ""); codigoObj[i] = linea[3].Replace("H", ""); if (!x) { codigoObj[i] = MetodosAuxiliares.decimalAHexadecimal(int.Parse(codigoObj[i])); } if (codigoObj[i].Length < 6) { resto = 6 - codigoObj[i].Length; aux = ""; for (int j = 0; j < resto; j++) { aux += "0"; } codigoObj[i] = aux + codigoObj[i]; } else if (codigoObj[i].Length > 6) { codigoObj[i] = codigoObj[i].Substring(0, 6); } #endregion break; default: this.codigoObj[i] = "---"; break; } } } else { codigoObj[i] = "Error: de Sintaxis"; } } else { this.codigoObj[i] = "Error: Linea vacia"; } } this.ensamblaIntermedio(dataGridViewIntermedio); this.creaArchivoObj(); }