Advertisement
xangelux

Maquina virtual: 1 opreg.c

Apr 3rd, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "lib/opcad.h"
  5. #include "lib/util.h"
  6. #include <stdbool.h>
  7. /*
  8.  * Función que verifíca si es un comando válido y si lo és regresa el número de argumentos que requiere para funcionar
  9.  * Argumentos: char *comando, cadena.
  10.  * Extendible: Si, se pueden aumentar operaciones.
  11.  */
  12.  
  13. int comparaComando(char *comando){
  14.  
  15.     if( strcmp("MOV", comando) == 0 )
  16.         return 2;
  17.     else if( strcmp("ADD", comando) == 0 )
  18.         return 2;
  19.     else if( strcmp("SUB", comando) == 0 )
  20.         return 2;
  21.     else if( strcmp("MUL", comando) == 0 )
  22.         return 2;
  23.     else if( strcmp("DIV", comando) == 0 )
  24.         return 2;
  25.     else if( strcmp("INC", comando) == 0 )
  26.         return 1;
  27.     else if( strcmp("DEC", comando) == 0 )
  28.         return 1;
  29.     else if( strcmp("JNZ", comando) == 0 )
  30.         return 1;
  31.     else if( strcmp("END", comando) == 0 )
  32.         return 0;
  33.     else if( strcmp("MAX", comando) == 0 )
  34.         return 4;
  35.     else if( strcmp("GET", comando) == 0 )
  36.         return 4;
  37.     else if( strcmp("USE", comando) == 0 )
  38.         return 4;
  39.     else if( strcmp("FRE", comando) == 0 )
  40.         return 4;
  41.     /*
  42.      * Insertar operaciones nuevas aquí
  43.      */
  44.  
  45.     else
  46.         return 3;
  47. }
  48.  
  49. /*
  50.  * Función que verifica si es un registro válido o no
  51.  * Argumentos: char *registro, cadena.
  52.  * Extendible: Si, Se pueden aumentar registros
  53.  */
  54.  
  55. int comparaRegistro( char *registro ){
  56.     if( strcmp("AX", registro) == 0 )
  57.         return 0;
  58.     else if( strcmp("BX", registro) == 0 )
  59.         return 0;
  60.     else if( strcmp("CX", registro) == 0 )
  61.         return 0;
  62.     else if( strcmp("DX", registro) == 0 )
  63.         return 0;
  64.     /*
  65.      * Insertar nuevos registros aquí
  66.      */
  67.  
  68.     else
  69.         return 1;
  70. }
  71.  
  72. /*
  73.  * Función que verifica si una cadena es un número hexadecimal válido
  74.  * Argumentos: char *reg, cadena.
  75.  * Extendible: No.
  76.  */
  77.  
  78. int comparaHex(char *reg){
  79.  
  80. int i = 0;
  81.  
  82.     while( reg[i] != '\0' ){
  83.         switch( reg[i] ){
  84.             case '0':
  85.             case '1':
  86.             case '2':
  87.             case '3':
  88.             case '4':
  89.             case '5':
  90.             case '6':
  91.             case '7':
  92.             case '8':
  93.             case '9':
  94.             case 'A':
  95.             case 'B':
  96.             case 'C':
  97.             case 'D':
  98.             case 'E':
  99.             case 'F':
  100.             case 'a':
  101.             case 'b':
  102.             case 'c':
  103.             case 'd':
  104.             case 'e':
  105.             case 'f':break;
  106.             default: return 1;break;
  107.         }
  108.         i++;
  109.     }
  110.     return 0;
  111. }
  112.  
  113. int mov(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  114.  
  115.     if( strcmp("AX", op1) == 0 ){
  116.         if( strcmp("AX", op2) == 0 )
  117.             return 0;
  118.         else if( strcmp("BX", op2) == 0 )
  119.             *AX = *BX;
  120.         else if( strcmp("CX", op2) == 0 )
  121.             *AX = *CX;
  122.         else if( strcmp("DX", op2) == 0 )
  123.             *AX = *DX;
  124.         else if( comparaHex(op2) == 0 )
  125.             *AX = strtohex(op2);
  126.         else
  127.             return 1;
  128.         return 0;
  129.     }
  130.     else if( strcmp("BX", op1) == 0 ){
  131.         if( strcmp("BX", op2) == 0 )
  132.             return 0;
  133.         else if( strcmp("AX", op2) == 0 )
  134.             *BX = *AX;
  135.         else if( strcmp("CX", op2) == 0 )
  136.             *BX = *CX;
  137.         else if( strcmp("DX", op2) == 0 )
  138.             *BX = *DX;
  139.         else if( comparaHex(op2) == 0 )
  140.             *BX = strtohex(op2);
  141.         else
  142.             return 1;
  143.         return 0;
  144.     }
  145.     else if( strcmp("CX", op1) == 0 ){
  146.         if( strcmp("CX", op2) == 0 )
  147.             return 0;
  148.         else if( strcmp("AX", op2) == 0 )
  149.             *CX = *AX;
  150.         else if( strcmp("BX", op2) == 0 )
  151.             *CX = *BX;
  152.         else if( strcmp("DX", op2) == 0 )
  153.             *CX = *DX;
  154.         else if( comparaHex(op2) == 0 )
  155.             *CX = strtohex(op2);
  156.         else
  157.             return 1;
  158.         return 0;
  159.     }
  160.  
  161.     else if( strcmp("DX", op1) == 0 ){
  162.         if( strcmp("DX", op2) == 0 )
  163.             return 0;
  164.         else if( strcmp("AX", op2) == 0 )
  165.             *DX = *AX;
  166.         else if( strcmp("CX", op2) == 0 )
  167.             *DX = *CX;
  168.         else if( strcmp("BX", op2) == 0 )
  169.             *DX = *BX;
  170.         else if( comparaHex(op2) == 0 )
  171.             *DX = strtohex(op2);
  172.         else
  173.             return 1;
  174.         return 0;
  175.     }
  176.     else
  177.         return 1;
  178.     return 0;
  179. }
  180. int sub(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  181.  
  182.     if( strcmp("AX", op1) == 0 ){
  183.         if( strcmp("AX", op2) == 0 )
  184.             *AX -= *AX;
  185.         else if( strcmp("BX", op2) == 0 )
  186.             *AX -= *BX;
  187.         else if( strcmp("CX", op2) == 0 )
  188.             *AX -= *CX;
  189.         else if( strcmp("DX", op2) == 0 )
  190.             *AX -= *DX;
  191.         else if( comparaHex(op2) == 0 )
  192.             *AX -= strtohex(op2);
  193.         else
  194.             return 1;
  195.         return 0;
  196.     }
  197.     else if( strcmp("BX", op1) == 0 ){
  198.         if( strcmp("BX", op2) == 0 )
  199.             *BX -= *BX;
  200.         else if( strcmp("AX", op2) == 0 )
  201.             *BX -= *AX;
  202.         else if( strcmp("CX", op2) == 0 )
  203.             *BX -= *CX;
  204.         else if( strcmp("DX", op2) == 0 )
  205.             *BX -= *DX;
  206.         else if( comparaHex(op2) == 0 )
  207.             *BX -= strtohex(op2);
  208.         else
  209.             return 1;
  210.         return 0;
  211.     }
  212.     else if( strcmp("CX", op1) == 0 ){
  213.         if( strcmp("CX", op2) == 0 )
  214.             *CX -= *CX;
  215.         else if( strcmp("AX", op2) == 0 )
  216.             *CX -= *AX;
  217.         else if( strcmp("BX", op2) == 0 )
  218.             *CX -= *BX;
  219.         else if( strcmp("DX", op2) == 0 )
  220.             *CX -= *DX;
  221.         else if( comparaHex(op2) == 0 )
  222.             *CX -= strtohex(op2);
  223.         else
  224.             return 1;
  225.         return 0;
  226.     }
  227.  
  228.     else if( strcmp("DX", op1) == 0 ){
  229.         if( strcmp("DX", op2) == 0 )
  230.             *DX -= *DX;
  231.         else if( strcmp("AX", op2) == 0 )
  232.             *DX -= *AX;
  233.         else if( strcmp("CX", op2) == 0 )
  234.             *DX -= *CX;
  235.         else if( strcmp("BX", op2) == 0 )
  236.             *DX -= *BX;
  237.         else if( comparaHex(op2) == 0 )
  238.             *DX -= strtohex(op2);
  239.         else
  240.             return 1;
  241.         return 0;
  242.     }
  243.     else
  244.         return 1;
  245. return 0;
  246. }
  247.  
  248. int mul(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  249.  
  250.     if( strcmp("AX", op1) == 0 ){
  251.         if( strcmp("AX", op2) == 0 )
  252.             *AX *= *AX;
  253.         else if( strcmp("BX", op2) == 0 )
  254.             *AX *= *BX;
  255.         else if( strcmp("CX", op2) == 0 )
  256.             *AX *= *CX;
  257.         else if( strcmp("DX", op2) == 0 )
  258.             *AX *= *DX;
  259.         else if( comparaHex(op2) == 0 )
  260.             *AX *= strtohex(op2);
  261.         else
  262.             return 1;
  263.         return 0;
  264.     }
  265.     else if( strcmp("BX", op1) == 0 ){
  266.         if( strcmp("BX", op2) == 0 )
  267.             *BX *= *BX;
  268.         else if( strcmp("AX", op2) == 0 )
  269.             *BX *= *AX;
  270.         else if( strcmp("CX", op2) == 0 )
  271.             *BX *= *CX;
  272.         else if( strcmp("DX", op2) == 0 )
  273.             *BX *= *DX;
  274.         else if( comparaHex(op2) == 0 )
  275.             *BX *= strtohex(op2);
  276.         else
  277.             return 1;
  278.         return 0;
  279.     }
  280.     else if( strcmp("CX", op1) == 0 ){
  281.         if( strcmp("CX", op2) == 0 )
  282.             *CX *= *CX;
  283.         else if( strcmp("AX", op2) == 0 )
  284.             *CX *= *AX;
  285.         else if( strcmp("BX", op2) == 0 )
  286.             *CX *= *BX;
  287.         else if( strcmp("DX", op2) == 0 )
  288.             *CX *= *DX;
  289.         else if( comparaHex(op2) == 0 )
  290.             *CX *= strtohex(op2);
  291.         else
  292.             return 1;
  293.         return 0;
  294.     }
  295.  
  296.     else if( strcmp("DX", op1) == 0 ){
  297.         if( strcmp("DX", op2) == 0 )
  298.             *DX *= *DX;
  299.         else if( strcmp("AX", op2) == 0 )
  300.             *DX *= *AX;
  301.         else if( strcmp("CX", op2) == 0 )
  302.             *DX *= *CX;
  303.         else if( strcmp("BX", op2) == 0 )
  304.             *DX *= *BX;
  305.         else if( comparaHex(op2) == 0 )
  306.             *DX *= strtohex(op2);
  307.         else
  308.             return 1;
  309.         return 0;
  310.     }
  311.     else
  312.         return 1;
  313. return 0;
  314. }
  315.  
  316. int divi(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  317.  
  318.     if( strcmp("AX", op1) == 0 ){
  319.         if( strcmp("AX", op2) == 0 )
  320.             *AX /= *AX;
  321.         else if( strcmp("BX", op2) == 0 && *BX != 0 )
  322.             *AX /= *BX;
  323.         else if( strcmp("CX", op2) == 0 && *CX != 0 )
  324.             *AX /= *CX;
  325.         else if( strcmp("DX", op2) == 0 && *DX != 0 )
  326.             *AX /= *DX;
  327.         else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
  328.             *AX /= strtohex(op2);
  329.         else
  330.             return 1;
  331.         return 0;
  332.     }
  333.     else if( strcmp("BX", op1) == 0 ){
  334.         if( strcmp("BX", op2) == 0 )
  335.             *BX /= *BX;
  336.         else if( strcmp("AX", op2) == 0 && *AX != 0 )
  337.             *BX /= *AX;
  338.         else if( strcmp("CX", op2) == 0 && *CX != 0 )
  339.             *BX /= *CX;
  340.         else if( strcmp("DX", op2) == 0 && *DX != 0 )
  341.             *BX /= *DX;
  342.         else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
  343.             *BX /= strtohex(op2);
  344.         else
  345.             return 1;
  346.         return 0;
  347.     }
  348.     else if( strcmp("CX", op1) == 0 ){
  349.         if( strcmp("CX", op2) == 0 )
  350.             *CX /= *CX;
  351.         else if( strcmp("AX", op2) == 0 && *AX != 0 )
  352.             *CX /= *AX;
  353.         else if( strcmp("BX", op2) == 0 && *BX != 0 )
  354.             *CX /= *BX;
  355.         else if( strcmp("DX", op2) == 0 && *DX != 0 )
  356.             *CX /= *DX;
  357.         else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
  358.             *CX /= strtohex(op2);
  359.         else
  360.             return 1;
  361.         return 0;
  362.     }
  363.  
  364.     else if( strcmp("DX", op1) == 0 ){
  365.         if( strcmp("DX", op2) == 0 )
  366.             *DX /= *DX;
  367.         else if( strcmp("AX", op2) == 0 && *AX != 0 )
  368.             *DX /= *AX;
  369.         else if( strcmp("CX", op2) == 0 && *CX != 0 )
  370.             *DX /= *CX;
  371.         else if( strcmp("BX", op2) == 0 && *BX != 0 )
  372.             *DX /= *BX;
  373.         else if( comparaHex(op2) == 0 && strtohex(op2) != 0 )
  374.             *DX /= strtohex(op2);
  375.         else
  376.             return 1;
  377.         return 0;
  378.     }
  379.     else
  380.         return 1;
  381. return 0;
  382. }
  383.  
  384. int inc(char *op1,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  385.  
  386.     if( strcmp("AX", op1) == 0 )
  387.         *AX = *AX + 1;
  388.     else if( strcmp("BX", op1) == 0 )
  389.         *BX = *BX + 1;
  390.     else if( strcmp("CX", op1) == 0 )
  391.         *CX = *CX + 1;
  392.     else if( strcmp("DX", op1) == 0 )
  393.         *DX = *DX + 1;
  394.     else
  395.         return 1;
  396.     return 0;
  397. }
  398.  
  399. int dec(char *op1,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  400.  
  401.     if( strcmp("AX", op1) == 0 )
  402.         *AX = *AX - 1;
  403.     else if( strcmp("BX", op1) == 0 )
  404.         *BX = *BX - 1;
  405.     else if( strcmp("CX", op1) == 0 )
  406.         *CX = *CX - 1;
  407.     else if( strcmp("DX", op1) == 0 )
  408.         *DX = *DX - 1;
  409.     else
  410.         return 1;
  411.     return 0;
  412. }
  413.  
  414. int end(char *IR){
  415.     if( strlen(IR) != 3 )
  416.         return 1;
  417.     else
  418.         return 0;
  419. }
  420.  
  421. int add(char *op1,char *op2,unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX){
  422.  
  423.     if( strcmp("AX", op1) == 0 ){
  424.         if( strcmp("AX", op2) == 0 )
  425.             *AX += *AX;
  426.         else if( strcmp("BX", op2) == 0 )
  427.             *AX += *BX;
  428.         else if( strcmp("CX", op2) == 0 )
  429.             *AX += *CX;
  430.         else if( strcmp("DX", op2) == 0 )
  431.             *AX += *DX;
  432.         else if( comparaHex(op2) == 0 )
  433.             *AX += strtohex(op2);
  434.         else
  435.             return 1;
  436.         return 0;
  437.     }
  438.     else if( strcmp("BX", op1) == 0 ){
  439.         if( strcmp("BX", op2) == 0 )
  440.             *BX += *BX;
  441.         else if( strcmp("AX", op2) == 0 )
  442.             *BX += *AX;
  443.         else if( strcmp("CX", op2) == 0 )
  444.             *BX += *CX;
  445.         else if( strcmp("DX", op2) == 0 )
  446.             *BX += *DX;
  447.         else if( comparaHex(op2) == 0 )
  448.             *BX += strtohex(op2);
  449.         else
  450.             return 1;
  451.         return 0;
  452.     }
  453.     else if( strcmp("CX", op1) == 0 ){
  454.         if( strcmp("CX", op2) == 0 )
  455.             *CX += *CX;
  456.         else if( strcmp("AX", op2) == 0 )
  457.             *CX += *AX;
  458.         else if( strcmp("BX", op2) == 0 )
  459.             *CX += *BX;
  460.         else if( strcmp("DX", op2) == 0 )
  461.             *CX += *DX;
  462.         else if( comparaHex(op2) == 0 )
  463.             *CX += strtohex(op2);
  464.         else
  465.             return 1;
  466.         return 0;
  467.     }
  468.  
  469.     else if( strcmp("DX", op1) == 0 ){
  470.         if( strcmp("DX", op2) == 0 )
  471.             *DX += *DX;
  472.         else if( strcmp("AX", op2) == 0 )
  473.             *DX += *AX;
  474.         else if( strcmp("CX", op2) == 0 )
  475.             *DX += *CX;
  476.         else if( strcmp("BX", op2) == 0 )
  477.             *DX += *BX;
  478.         else if( comparaHex(op2) == 0 )
  479.             *DX += strtohex(op2);
  480.         else
  481.             return 1;
  482.         return 0;
  483.     }
  484.     else
  485.         return 1;
  486. return 0;
  487. }
  488.  
  489. int jump(char *op1, unsigned long long *CX, int *PC){
  490.     int aux = atoi(op1);
  491.     if( *CX != 0 ){
  492.         if ( aux < 0 )
  493.             return 1;
  494.         else{
  495.             *PC = aux;
  496.             return 0;
  497.         }
  498.     }
  499.     else{
  500.         (*PC)++;
  501.         return 0;
  502.     }
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement