Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "StackLib.h"
  3. #include <math.h>
  4.  
  5. #define LABELS_COUNT 32
  6. #define MEMORY_SIZE 128
  7.  
  8.  enum command_list
  9. {
  10.     CMD_PUSH = 1,
  11.     CMD_ADD = 3,
  12.     CMD_MUL = 4,
  13.     CMD_DIV = 5,
  14.     CMD_SUB = 6,
  15.     CMD_POP = 20,
  16.     CMD_POPsq = 21,
  17.     CMD_POPax = 22,
  18.     CMD_POPbx = 23,
  19.     CMD_POPcx = 24,
  20.     CMD_POPdx = 25,
  21.     CMD_JMP = 100,
  22.     CMD_JE = 101,
  23.     CMD_JNE = 102,
  24.     CMD_JA = 103,
  25.     CMD_JAE = 104,
  26.     CMD_JB = 105,
  27.     CMD_JBE = 106,
  28.     CMD_OUT = 7,
  29.     CMD_SQRT = 8,
  30.     CMD_IN = 9
  31. };
  32.  
  33. struct CPU
  34. {
  35.     double ax;
  36.     double bx;
  37.     double cx;
  38.     double dx;
  39.     int * memory;
  40.     struct * Stack;
  41. };
  42.  
  43. int main ()
  44. {
  45.     struct CPU MY_CPU;
  46.     initialize_CPU(&MY_CPU);
  47.     FILE * bytecod = fopen ("bytecod.txt", "r");
  48.     int size = fsize(FILE * bytecod);
  49.  
  50.     int * commands = (int *) calloc(size, sizeof(int));
  51.     int * labels = (int *) calloc(LABELS_COUNT, sizeof(int));
  52.     char symbol = '\0';
  53.     int label = 0;
  54.    
  55.     mem_labels(bytecod, labels);
  56. }
  57.  
  58. int initialize_CPU(struct CPU * MY_CPU)
  59. {
  60.     /*
  61.      *   МЕСТО ДЛЯ ПРОВЕРОК
  62.      */
  63.    
  64.     MY_CPU -> memory = (int *) calloc (MEMORY_SIZE, sizeof(int));
  65.     MY_CPU -> ax = 0;
  66.     MY_CPU -> bx = 0;
  67.     MY_CPU -> cx = 0;
  68.     MY_CPU -> dx = 0;
  69.     MY_CPU - = init_stack()
  70.    
  71. }
  72.  
  73.  
  74. int fsize(FILE * bytecode)
  75. {
  76.     if (bytecod == NULL )
  77.     {
  78.         return -1;
  79.     }
  80.    
  81.     int size = 0;
  82.     int curpos = 0;
  83.     curpos = ftell(bytecod);
  84.     fseek (bytecod, 0, SEEK_END);
  85.     size = ftell(bytecod);
  86.     fseek (bytecod, 0, curpos);
  87.     return size;
  88. }
  89.  
  90. int mem_labels (FILE * bytecod, int * labels)
  91. {
  92.     /*
  93.      *   МЕСТО ДЛЯ ПРОВЕРОК
  94.     */
  95.    
  96.    
  97.     char symbol = '\0';
  98.     int number_of_label = 0;
  99.     int number_of_command = 0;
  100.    
  101.     initialize_array(labels);
  102.    
  103.     while(fscanf(bytecod, "%c", &symbol) != EOF)
  104.     {
  105.         if (symbol == ':')
  106.         {
  107.             fscanf(bytecod, "%d", &number_of_label);
  108.             labels[number_of_label] = number_of_command;
  109.         }
  110.  
  111.         else
  112.             number_of_command += 1;
  113.     }
  114.    
  115.     return 0;
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement