Advertisement
xangelux

Maquina virtual: 1 opcad.c

Apr 3rd, 2011
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "lib/opreg.h"
  5. #include <ncurses.h>
  6. /*
  7.  * Función que convierte una cadena a su hexadecimal
  8.  * Argumentos: char *string, cadena.
  9.  * Extendible: No.
  10.  */
  11.  
  12. unsigned long long strtohex(char *string){
  13.  
  14.     char hexstr[strlen(string)+3];
  15.  
  16. if(string[strlen(string)-1] == '\n')
  17.     string[strlen(string)-1]='\0';
  18.     hexstr[0]='0';
  19.     hexstr[1]='x';
  20.     hexstr[2]='\0';
  21.     strcat(hexstr,string);
  22.     return strtoull(hexstr,NULL,16);
  23. }
  24.  
  25. /*
  26.  * Función que funciona como parser, parte la instrucción en sus elementos verificando que sean válidos
  27.  * Argumentos: char []: instruccion, codop, op1, op2
  28.  * Extendible: Si, se pueden añadir codigos de operación y registros
  29.  */
  30.  
  31. int parteInstruccion(char instruccion[], char codop[], char op1[], char op2[], char op3[], char op4[]){
  32.  
  33.     int i = 0, j = 0, arg, k = 0;
  34.  
  35.     /*
  36.      * Sección 1: Partiendo y validando comando
  37.      */
  38.  
  39.     while( instruccion[i] != ' ' && instruccion[i] != '\0')
  40.         i++;
  41.     if ( i == 3 ){
  42.         strncpy(codop,&instruccion[0],3);
  43.         codop[3] = '\0';
  44.         arg = comparaComando(codop);
  45.         if ( ( arg == 1 ) || ( arg == 2 ) ){
  46.             i++;
  47.             if( strcmp(codop,"JNZ") == 0 ){
  48.                 while( instruccion[i] != '\0' ){
  49.                     op1[i-4] = instruccion[i];
  50.                     i++;
  51.                 }
  52.                 op1[i-4] = '\0';
  53.                 return 0;
  54.             }
  55.             while( instruccion[i] != '\0' && instruccion[i] != ',')
  56.                 i++;
  57.             if ( i == 6 ){
  58.                 i++;
  59.                 strncpy( op1, &instruccion[4], 2 );
  60.                 op1[2] = '\0';
  61.                 if ( comparaRegistro(op1) == 1 ){
  62.                     for ( j = 0; j < strlen( op1 ) ;j++ )
  63.                         if( op1[i] > 47 && op1[i] < 58 )
  64.                             k++;
  65.                     if ( k != j )
  66.                         return 1;
  67.                 }
  68.             }
  69.             else
  70.                 return 1;
  71.             if ( arg == 1 ) return 0;
  72.             if ( arg == 2 ){
  73.                 if ( instruccion[i] == ' ' )
  74.                     return 1;
  75.                 else{  
  76.                     while ( instruccion[i] != ' ' && instruccion[i] != '\0'){
  77.                         if( j >= 17 )
  78.                             return 1;
  79.                         op2[j] = instruccion[i];
  80.                         i++;
  81.                         j++;
  82.                     }
  83.                     op2[j] = '\0';
  84.                     if ( ( comparaRegistro(op2) == 0 ) || ( comparaHex(op2) == 0 ) )
  85.                         return 0;
  86.                     else
  87.                         return 1;
  88.                 }
  89.             }
  90.         }
  91.         else if( arg == 0 )
  92.             return 0;
  93.         else if( arg == 4 ){
  94.  
  95.             /*
  96.              * Se supone que no hay muchos recursos por lo que el manejo de recursos se toma como
  97.              * una cifra no mayor a 3 digitos en cada operador
  98.              */
  99.             i++; j = 0;
  100.             while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
  101.                 op1[j] = instruccion[i];
  102.                 j++;i++;
  103.             }
  104.             op1[j] = '\0';
  105.             if( instruccion[i] == '\0' || j>=3 )
  106.                 return 1;
  107.             i++;
  108.             j = 0;
  109.             while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
  110.                 op2[j] = instruccion[i];
  111.                 j++;i++;
  112.             }
  113.             op2[j] = '\0';
  114.             if( instruccion[i] == '\0' || j>=3 )
  115.                 return 1;
  116.             i++;
  117.             j = 0;
  118.             while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
  119.                 op3[j] = instruccion[i];
  120.                 j++;i++;
  121.             }
  122.             op3[j] = '\0';
  123.             if( instruccion[i] == '\0' || j>=3 )
  124.                 return 1;
  125.             i++;
  126.             j = 0;
  127.             while( instruccion[i] != ',' && instruccion[i] != '\0' && j < 3 ){
  128.                 op4[j] = instruccion[i];
  129.                 j++;i++;
  130.             }
  131.             op4[j] = '\0';
  132.             if( j > 3 || instruccion[i] == ' ' )
  133.                 return 1;
  134.            
  135.         }
  136.         else
  137.             return 1;
  138.     }
  139.     else
  140.         return 1;
  141.     return 0;
  142. }
  143.  
  144. void armaComando( char *comando, char c ){
  145.  
  146. int i = 0;
  147.     while( comando[i] != '\0' )
  148.         i++;
  149.     comando[i+1] = '\0';
  150.     comando[i] = c;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement