Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "lib/opreg.h"
- #include <ncurses.h>
- /*
- * Función que convierte una cadena a su hexadecimal
- * Argumentos: char *string, cadena.
- * Extendible: No.
- */
- unsigned long long strtohex(char *string){
- char hexstr[strlen(string)+3];
- if(string[strlen(string)-1] == '\n')
- string[strlen(string)-1]='\0';
- hexstr[0]='0';
- hexstr[1]='x';
- hexstr[2]='\0';
- strcat(hexstr,string);
- return strtoull(hexstr,NULL,16);
- }
- /*
- * Función que funciona como parser, parte la instrucción en sus elementos verificando que sean válidos
- * Argumentos: char []: instruccion, codop, op1, op2
- * Extendible: Si, se pueden añadir codigos de operación y registros
- */
- int parteInstruccion(char instruccion[], char codop[], char op1[], char op2[], char op3[], char op4[]){
- int i = 0, j = 0, arg, k = 0;
- /*
- * Sección 1: Partiendo y validando comando
- */
- while( instruccion[i] != ' ' && instruccion[i] != '\0')
- i++;
- if ( i == 3 ){
- strncpy(codop,&instruccion[0],3);
- codop[3] = '\0';
- arg = comparaComando(codop);
- if ( ( arg == 1 ) || ( arg == 2 ) ){
- i++;
- if( strcmp(codop,"JNZ") == 0 ){
- while( instruccion[i] != '\0' ){
- op1[i-4] = instruccion[i];
- i++;
- }
- op1[i-4] = '\0';
- return 0;
- }
- while( instruccion[i] != '\0' && instruccion[i] != ',')
- i++;
- if ( i == 6 ){
- i++;
- strncpy( op1, &instruccion[4], 2 );
- op1[2] = '\0';
- if ( comparaRegistro(op1) == 1 ){
- for ( j = 0; j < strlen( op1 ) ;j++ )
- if( op1[i] > 47 && op1[i] < 58 )
- k++;
- if ( k != j )
- return 1;
- }
- }
- else
- return 1;
- if ( arg == 1 ) return 0;
- if ( arg == 2 ){
- if ( instruccion[i] == ' ' )
- return 1;
- else{
- while ( instruccion[i] != ' ' && instruccion[i] != '\0'){
- if( j >= 17 )
- return 1;
- op2[j] = instruccion[i];
- i++;
- j++;
- }
- op2[j] = '\0';
- if ( ( comparaRegistro(op2) == 0 ) || ( comparaHex(op2) == 0 ) )
- return 0;
- else
- return 1;
- }
- }
- }
- else if( arg == 0 )
- return 0;
- else if( arg == 4 ){
- /*
- * Se supone que no hay muchos recursos por lo que el manejo de recursos se toma como
- * una cifra no mayor a 3 digitos en cada operador
- */
- i++; j = 0;
- while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
- op1[j] = instruccion[i];
- j++;i++;
- }
- op1[j] = '\0';
- if( instruccion[i] == '\0' || j>=3 )
- return 1;
- i++;
- j = 0;
- while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
- op2[j] = instruccion[i];
- j++;i++;
- }
- op2[j] = '\0';
- if( instruccion[i] == '\0' || j>=3 )
- return 1;
- i++;
- j = 0;
- while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
- op3[j] = instruccion[i];
- j++;i++;
- }
- op3[j] = '\0';
- if( instruccion[i] == '\0' || j>=3 )
- return 1;
- i++;
- j = 0;
- while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
- op4[j] = instruccion[i];
- j++;i++;
- }
- op4[j] = '\0';
- if( j > 3 || instruccion[i] == ' ' )
- return 1;
- }
- else
- return 1;
- }
- else
- return 1;
- return 0;
- }
- void armaComando( char *comando, char c ){
- int i = 0;
- while( comando[i] != '\0' )
- i++;
- comando[i+1] = '\0';
- comando[i] = c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement