Liba

interpretador

Jul 1st, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.55 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. void changeLabels();
  7. void convert();
  8. void convertIntToString(char [], int);
  9. void convertStringOfIntToStringOfBinary(char[], char[]);
  10. void convertStringOfIntToStringOfBinary2(char[], char[]);
  11. void convertStringOfIntToStringOfBinary5(char[], char[]);
  12. void convertIntToBinary(int, char[]);
  13. void writeFile(char []);
  14.  
  15. int main()
  16. {
  17.     //changeLabels();
  18.     convert();
  19.     return EXIT_SUCCESS;
  20. }
  21.  
  22. void convert() {
  23.     int rs,rd,rt,i;
  24.     char instruction[32];
  25.     for(i = 0; i < 32; i++) {
  26.         instruction[i] = '0';
  27.     }
  28.     FILE *r;
  29.     r = fopen("assembly.txt", "r");
  30.     if(r != NULL) {
  31.         char line[256];
  32.         while(!feof(r)) {
  33.             if(fgets(line, 256, r) != NULL) {          
  34.                 if((line[0] == 'a' && line[1] == 'd' && line[2] == 'd')
  35.                 || (line[0] == 's' && line[1] == 'u' && line[2] == 'b')
  36.                 || (line[0] == 'm' && line[1] == 'u' && line[2] == 'l')
  37.                 || (line[0] == 's' && line[1] == 'l' && line[2] == 't')
  38.                 || (line[0] == 'o' && line[1] == 'r' && line[2] == 32)
  39.                 || (line[0] == 'n' && line[1] == 'o' && line[2] == 'r')
  40.                 || (line[0] == 'a' && line[1] == 'n' && line[2] == 'd')) {
  41.                     rs = (int)line[5] - 48;
  42.                     rs = rs * 10;
  43.                     rs += line[6] - 48;
  44.                    
  45.                     rt = (int)line[9] - 48;
  46.                     rt = rt * 10;
  47.                     rt += line[10] - 48;
  48.                    
  49.                     rd = (int)line[12] - 48;
  50.                     rd = rd * 10;
  51.                     rd += line[13] - 48;
  52.                     for(i = 0; i < 6; i++) {
  53.                         instruction[i] = '0';
  54.                     }
  55.                     char regist1[5], regist2[5], regist3[5];
  56.                     convertIntToString(regist3, rs);
  57.                     convertIntToString(regist2, rt);
  58.                     convertIntToString(regist1, rd);
  59.                     for(i = 0; i < 5; i++) {
  60.                         instruction[6+i] = regist1[i];
  61.                         instruction[11+i] = regist2[i];
  62.                         instruction[16+i] = regist3[i];
  63.                         instruction[21+i] = '0';
  64.                     }
  65.                     if(line[0] == 'a' && line[1] == 'd' && line[2] == 'd') {
  66.                         instruction[26] = '1';
  67.                         instruction[27] = '0';
  68.                         instruction[28] = '0';
  69.                         instruction[29] = '0';
  70.                         instruction[30] = '0';
  71.                         instruction[31] = '0';
  72.                     }
  73.                     else if(line[0] == 'a' && line[1] == 'n' && line[2] == 'd') {
  74.                         instruction[26] = '1';
  75.                         instruction[27] = '0';
  76.                         instruction[28] = '0';
  77.                         instruction[29] = '1';
  78.                         instruction[30] = '0';
  79.                         instruction[31] = '0';
  80.                     }
  81.                     else if(line[0] == 'm' && line[1] == 'u' && line[2] == 'l') {
  82.                         instruction[26] = '0';
  83.                         instruction[27] = '1';
  84.                         instruction[28] = '1';
  85.                         instruction[29] = '0';
  86.                         instruction[30] = '0';
  87.                         instruction[31] = '0';
  88.                     }
  89.                     else if(line[0] == 'o' && line[1] == 'r' && line[2] == 32) {
  90.                         instruction[26] = '1';
  91.                         instruction[27] = '0';
  92.                         instruction[28] = '0';
  93.                         instruction[29] = '1';
  94.                         instruction[30] = '0';
  95.                         instruction[31] = '1';
  96.                     }
  97.                     else if(line[0] == 's' && line[1] == 'l' && line[2] == 't') {
  98.                         instruction[26] = '1';
  99.                         instruction[27] = '0';
  100.                         instruction[28] = '1';
  101.                         instruction[29] = '0';
  102.                         instruction[30] = '1';
  103.                         instruction[31] = '0';
  104.                     }
  105.                     else if(line[0] == 's' && line[1] == 'u' && line[2] == 'b') {
  106.                         instruction[26] = '1';
  107.                         instruction[27] = '0';
  108.                         instruction[28] = '0';
  109.                         instruction[29] = '0';
  110.                         instruction[30] = '1';
  111.                         instruction[31] = '0';
  112.                     }
  113.                     else if(line[0] == 'n' && line[1] == 'o' && line[2] == 'r') {
  114.                         instruction[26] = '1';
  115.                         instruction[27] = '0';
  116.                         instruction[28] = '0';
  117.                         instruction[29] = '1';
  118.                         instruction[30] = '1';
  119.                         instruction[31] = '0';
  120.                     }
  121.                 }
  122.                 else if((line[0] == 'b' && line[1] == 'e' && line[2] == 'q')
  123.                 || (line[0] == 'l' && line[1] == 'w' && line[2] == 32)
  124.                 || (line[0] == 's' && line[1] == 'w' && line[2] == 32)
  125.                 || (line[0] == 'a' && line[1] == 'd' && line[2] == 'i')){
  126.                     rs = (int)line[5] - 48;
  127.                     rs = rs * 10;
  128.                     rs += line[6] - 48;
  129.                    
  130.                     rt = (int)line[9] - 48;
  131.                     rt = rt * 10;
  132.                     rt += line[10] - 48;
  133.                     char regist1[5], regist2[5];
  134.                     convertIntToString(regist2, rs);
  135.                     convertIntToString(regist1, rt);
  136.                     for(i = 0; i < 5; i++) {
  137.                         instruction[6+i] = regist1[i];
  138.                         instruction[11+i] = regist2[i];
  139.                     }
  140.                     char number[8];
  141.                     for(i = 0; i < 8; i++) {
  142.                         if(line[12+i] != 10) {
  143.                             number[i] = line[12+i];
  144.                         }
  145.                     }
  146.                     char label[16];
  147.                     convertStringOfIntToStringOfBinary(number, label);
  148.                     for(i = 0; i < 16; i++) {
  149.                         instruction[16+i] = label[i];
  150.                     }
  151.                     if(line[0] == 'b' && line[1] == 'e' && line[2] == 'q') {
  152.                         instruction[0] = '0';
  153.                         instruction[1] = '0';
  154.                         instruction[2] = '0';
  155.                         instruction[3] = '1';
  156.                         instruction[4] = '0';
  157.                         instruction[5] = '0';
  158.                     }
  159.                     if(line[0] == 'a' && line[1] == 'd' && line[2] == 'i') {
  160.                         instruction[0] = '0';
  161.                         instruction[1] = '0';
  162.                         instruction[2] = '1';
  163.                         instruction[3] = '0';
  164.                         instruction[4] = '0';
  165.                         instruction[5] = '0';
  166.                     }
  167.                     if(line[0] == 'l' && line[1] == 'w' && line[2] == 32) {
  168.                         instruction[0] = '1';
  169.                         instruction[1] = '0';
  170.                         instruction[2] = '0';
  171.                         instruction[3] = '0';
  172.                         instruction[4] = '1';
  173.                         instruction[5] = '1';
  174.                     }
  175.                     if(line[0] == 's' && line[1] == 'w' && line[2] == 32) {
  176.                         instruction[0] = '1';
  177.                         instruction[1] = '0';
  178.                         instruction[2] = '1';
  179.                         instruction[3] = '0';
  180.                         instruction[4] = '1';
  181.                         instruction[5] = '1';
  182.                     }
  183.                 }
  184.                 else if((line[0] == 'j' && line[1] == 'm' && line[2] == 'p')
  185.                 || (line[0] == 'j' && line[1] == 'a' && line[2] == 'l')){
  186.                     char number[12];
  187.                     for(i = 0; i < 12; i++) {
  188.                         if(line[4+i] != 10) {
  189.                             number[i] = line[4+i];
  190.                         }
  191.                     }
  192.                     char label[26];
  193.                     convertStringOfIntToStringOfBinary2(number, label);
  194.                     for(i = 0; i < 26; i++) {
  195.                         instruction[6+i] = label[i];
  196.                     }
  197.                     if(line[0] == 'j' && line[1] == 'm' && line[2] == 'p') {
  198.                         instruction[0] = '0';
  199.                         instruction[1] = '0';
  200.                         instruction[2] = '0';
  201.                         instruction[3] = '0';
  202.                         instruction[4] = '1';
  203.                         instruction[5] = '0';
  204.                     }
  205.                     if(line[0] == 'j' && line[1] == 'a' && line[2] == 'l') {
  206.                         instruction[0] = '0';
  207.                         instruction[1] = '0';
  208.                         instruction[2] = '0';
  209.                         instruction[3] = '0';
  210.                         instruction[4] = '1';
  211.                         instruction[5] = '1';
  212.                     }
  213.                 }
  214.                 else if(line[0] == 'h' && line[1] == 'l' && line[2] == 't') {
  215.                     for(i = 0; i < 32; i++) {
  216.                         instruction[i] = '1';
  217.                     }
  218.                 }
  219.                 else if(line[0] == 'm' && line[1] == 'o' && line[2] == 'v') {
  220.                     for(i = 0; i < 6; i++) {
  221.                         instruction[i] = '0';
  222.                     }
  223.                     for(i = 0; i < 6; i++) {
  224.                         instruction[16+i] = '0';
  225.                         instruction[21+i] = '0';
  226.                     }
  227.                    
  228.                     instruction[26] = '1';
  229.                     instruction[27] = '0';
  230.                     instruction[28] = '0';
  231.                     instruction[29] = '0';
  232.                     instruction[30] = '0';
  233.                     instruction[31] = '0';
  234.                    
  235.                     rs = (int)line[5] - 48;
  236.                     rs = rs * 10;
  237.                     rs += line[6] - 48;
  238.                    
  239.                     rt = (int)line[9] - 48;
  240.                     rt = rt * 10;
  241.                     rt += line[10] - 48;
  242.                     char regist1[5], regist2[5];
  243.                     convertIntToString(regist2, rs);
  244.                     convertIntToString(regist1, rt);
  245.                     for(i = 0; i < 5; i++) {
  246.                         instruction[6+i] = regist1[i];
  247.                         instruction[11+i] = regist2[i];
  248.                     }
  249.                 }
  250.                 else if((line[0] == 's' && line[1] == 'l' && line[2] == 'l')
  251.                 || (line[0] == 's' && line[1] == 'r' && line[2] == 'l')){
  252.                     for(i = 0; i < 6; i++) {
  253.                         instruction[i] = '0';
  254.                     }
  255.                     rs = (int)line[5] - 48;
  256.                     rs = rs * 10;
  257.                     rs += line[6] - 48;
  258.                    
  259.                     rt = (int)line[9] - 48;
  260.                     rt = rt * 10;
  261.                     rt += line[10] - 48;
  262.                     char regist1[5], regist2[5];
  263.                     convertIntToString(regist2, rs);
  264.                     convertIntToString(regist1, rt);
  265.                     for(i = 0; i < 5; i++) {
  266.                         instruction[6+i] = regist1[i];
  267.                         instruction[11+i] = regist2[i];
  268.                         instruction[16+i] = '0';
  269.                         instruction[26+i] = '0';
  270.                     }
  271.                     char number[8];
  272.                     for(i = 0; i < 8; i++) {
  273.                         if(line[12+i] != 10) {
  274.                             number[i] = line[12+i];
  275.                         }
  276.                     }
  277.                     char shift[5];
  278.                     convertStringOfIntToStringOfBinary5(number, shift);
  279.                     for(i = 0; i < 5; i++) {
  280.                         instruction[21+i] = shift[i];
  281.                     }
  282.                 }
  283.                 else if(line[0] == 'j' && line[1] == 'r' && line[2] == 32) {
  284.                     for(i = 0; i < 6; i++) {
  285.                         instruction[i] = '0';
  286.                     }
  287.                     rs = (int)line[5] - 48;
  288.                     rs = rs * 10;
  289.                     rs += line[6] - 48;
  290.                     char regist1[5];
  291.                     convertIntToString(regist1, rs);
  292.                     for(i = 0; i < 5; i++) {
  293.                         instruction[6+i] = regist1[i];
  294.                         instruction[11+i] = '0';
  295.                         instruction[16+i] = '0';
  296.                         instruction[21+i] = '0';
  297.                     }
  298.                     instruction[26] = '0';
  299.                     instruction[27] = '0';
  300.                     instruction[28] = '1';
  301.                     instruction[29] = '0';
  302.                     instruction[30] = '0';
  303.                     instruction[31] = '0';
  304.                 }
  305.                 writeFile(instruction);
  306.             }
  307.         }  
  308.     }
  309. }
  310.  
  311. void changeLabels() {
  312.     //~ char line[256];
  313.     //~ FILE *p;
  314.     //~ p = fopen("assembly.txt", "r");
  315.     //~ if(p != NULL) {
  316.         //~
  317.     //~ }
  318. }
  319.  
  320. void writeFile(char data[]) {
  321.     int i;
  322.     char end;
  323.     end = 10;
  324.     FILE *w;
  325.     w = fopen("instructions.txt", "wa");
  326.     if(w != NULL) {
  327.         for(i = 0; i < 32; i++) {
  328.             fprintf(w, "%c", data[i]);
  329.         }
  330.         fprintf(w, "%c", end);
  331.     }
  332. }
  333.  
  334. void convertStringOfIntToStringOfBinary(char number[], char label[]) {
  335.     int numberInt;
  336.     numberInt = atoi(number);
  337.     convertIntToBinary(numberInt, label);
  338. }
  339.  
  340. void convertStringOfIntToStringOfBinary2(char number[], char jump[]) {
  341. }
  342.  
  343. void convertStringOfIntToStringOfBinary5(char number[], char shift[]) {
  344.    
  345. }
  346.  
  347. void convertIntToBinary(int numberInt, char label[]) {
  348. }
Advertisement
Add Comment
Please, Sign In to add comment