Advertisement
Guest User

mik

a guest
Dec 4th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #define SIZE 8
  6. #define SIZE_OF_REJESTRY 16
  7. #define SIZE_OF_MEMORY 256
  8. #define SEPARATOR '%'
  9. #define NUMBER_OF_ARGUMENTS 3
  10. #define CORE_DUMP 0
  11. #define GET_INT 1
  12. #define PUT_INT 2
  13. #define GET_CHAR 3
  14. #define PUT_CHAR 4
  15. #define PUT_STRING 5
  16.  
  17.  
  18. /*PODSTAWOWA STRUKTURA*/
  19. typedef struct func_data{
  20.     int pc_index;
  21.     int func_code;
  22.     int ARG[NUMBER_OF_ARGUMENTS];
  23.     struct func_data *next;
  24. } func_data;
  25.  
  26. func_data *func_data_create_node(int index, int f_num, int *arg)
  27. {
  28.     int i;
  29.     func_data *output = malloc(sizeof(func_data));
  30.     if(output == NULL) exit(1);
  31.  
  32.     output->pc_index = index;
  33.     output->func_code = f_num;
  34.     output->next = NULL;
  35.     for(i = 0; i < 3; ++i) output->ARG[i] = arg[i];
  36.  
  37.     return output;
  38. }
  39.  
  40. void func_data_add_node(func_data *f, int index, int f_num, int *arg)
  41. {
  42.     func_data *output = func_data_create_node(index, f_num, arg);
  43.     while(f->next != NULL) f = f->next;
  44.     f->next = output;
  45. }
  46.  
  47. func_data *func_data_find_pc(func_data *f, int index)
  48. {
  49.     func_data *output = f;
  50.     while(output != NULL && output->pc_index != index) output = output->next;
  51.     return output;
  52. }
  53.  
  54. void func_data_release(func_data *f)
  55. {
  56.     func_data *storage = f;
  57.     while(f != NULL){
  58.         storage = f->next;
  59.         free(f);
  60.         f = storage;
  61.     }
  62. }
  63.  
  64. /*INNE funkcje*/
  65. void show_arr(int A[], int n)
  66. {
  67.     int i;
  68.     for(i = 0; i < n; ++i) printf("%d ", A[i]);
  69.     printf("\n");
  70. }
  71.  
  72. /*Funkcja dokonujaca poprawna operacje modulo na liczbach*/
  73. int modulo(int a, int b)
  74. {
  75.     while(a < 0) a += b;
  76.     return a % b;
  77. }
  78.  
  79. /*wyswietla string*/
  80. void show_string(int *A)
  81. {
  82.     int i = 0;
  83.     while( A[i] != 0){
  84.         putchar(A[i]);
  85.         i++;
  86.     }
  87. }
  88.  
  89. /*FUNKCJA WCZYTUJACA DANE*/
  90. char* read_string(int *length)
  91. {
  92.     int size = 2, i = 0;
  93.     char *output;
  94.     char ch;
  95.     int part = 0;
  96.  
  97.     /*Alokowanie mapieci tak ze bedzie przechowywana na steercie*/
  98.     output = malloc(size * sizeof(char));
  99.     /*Sprawdzanie czy pamiec zostala zaalokowana*/
  100.     if(!output) exit(1);
  101.  
  102.     while((ch = getchar()) != EOF && part < 4){
  103.         *(output + i) = ch;
  104.         ++i;
  105.         if(ch == SEPARATOR) part++;
  106.  
  107.         /*Realokowanie pamieci w przypadku gdy liczba
  108.         wczytanych danych przekracza zaalokowana pamiec*/
  109.         if(i > size - 1){
  110.             size *= 2;
  111.             output = realloc(output, size * sizeof(char));
  112.             /*Sprawdzanie czy pamiec zostala zaalokowana*/
  113.             if(!output) exit(1);
  114.         }
  115.     }
  116.     /*Ustawienie dlugosci*/
  117.     *length = i;
  118.     /*Ustanowienie znaku konca tablicy '\0'*/
  119.     *(output + i) = '\0';
  120.  
  121.     return output;
  122. }
  123.  
  124. /*KONWERTOWANIE LICZBY*/
  125. void find_value_and_base(char ch, int *value, int *base)
  126. {
  127.     if(ch >= '0' && ch <= '9'){
  128.         *value = ch - '0';
  129.         *base = 10;
  130.     }
  131.     else if(ch >= 'A' && ch <= 'P'){
  132.         *value = ch - 'A';
  133.         *base = 16;
  134.     }
  135.     else if(ch >= 'Q' && ch <= 'X'){
  136.         *value = ch - 'Q';
  137.         *base = 8;
  138.     }
  139.     else if(ch >= 'Y' && ch <= 'Z'){
  140.         if(ch == 'Y') *value = 1;
  141.         else *value = 0;
  142.         *base = 2;
  143.     }
  144. }
  145.  
  146. int convert_dexal(char *string, int length)
  147. {
  148.     int i, output = 0;
  149.     int value = 0, base = 0;
  150.     /*show_arr1(string, length);*/
  151.  
  152.     for(i = 0; i < length; ++i){
  153.         find_value_and_base(string[i], &value, &base);
  154.         output = output * base + value;
  155.         /*printf("VALUE %d, BASE %d, OUTPUT %d\n", value, base, output);*/
  156.     }
  157.     return output;
  158. }
  159.  
  160. /*WYPELNIA PAMIEC I REJESTR*/
  161. void fill_data(int *Arr, int *start, char *Input, int length)
  162. {
  163.     int i = *start;
  164.     int j = 0;
  165.     int value, not_blank, blank_after;
  166.  
  167.     while(i < length && Input[i] != SEPARATOR){
  168.  
  169.         while(i < length && (Input[i] == ' ' || Input[i] == '\n' || Input[i] == '\t') && Input[i] != SEPARATOR)++i;
  170.         if(Input[i] != SEPARATOR){
  171.             not_blank = i;
  172.             while(i < length && Input[i] != ' ' && Input[i] != '\n' && Input[i] != '\t' && Input[i] != SEPARATOR)++i;
  173.             blank_after = i;
  174.             value = convert_dexal(Input + not_blank, blank_after - not_blank);
  175.             Arr[j] = value;
  176.             ++j;
  177.         }
  178.     }
  179.     ++i;/* nalezy sie zastanowic czy bedzie mialo to sens, ma chyba*/
  180.     *start = i;
  181. }
  182.  
  183. void get_function_data(char *inp, int *start, int len, func_data *f)
  184. {
  185.     int i = *start, j;
  186.     int not_blank, f_code, pc_index = 0;
  187.     int Parameters[3] = {0};
  188.  
  189.     while(i < len && inp[i] != SEPARATOR){
  190.  
  191.         /*Pomin nieznaczace symbole*/
  192.         while(i < len && (inp[i] == ' ' || inp[i] == '\n' || inp[i] == '\t')
  193.             && inp[i] != SEPARATOR) ++i;
  194.        
  195.         if(inp[i] != SEPARATOR){
  196.  
  197.             not_blank = i;
  198.             f_code = convert_dexal  (inp + not_blank, 1);
  199.             j = 0;
  200.  
  201.             /*Wypelnij tablice parametrow*/
  202.             while(i < len && inp[i] != ' ' && inp[i] != '\n' && inp[i] != '\t'
  203.                 && inp[i] != SEPARATOR){
  204.                 Parameters[j] = convert_dexal(inp + not_blank + j + 1   , 1);
  205.                 ++i;
  206.                 ++j;
  207.             }
  208.  
  209.             /*Uzupelnij strukture*/
  210.             func_data_add_node(f, pc_index, f_code, Parameters);
  211.             pc_index++;
  212.         }
  213.     }
  214.     ++i;
  215.     *start = i;
  216. }
  217.  
  218.  
  219. /*TO JEST ZLE*/
  220. /*MALE FUNKCJE*/
  221.  
  222. void core_dump(int val)
  223. {
  224.     fprintf(stderr, "%d", val);
  225. }
  226.  
  227. /*0*/
  228. void divide(int *REJESTRY, int a, int b, int c, int *PC)
  229. {
  230.     int temp_c = REJESTRY[c], temp_b;
  231.     int next_index = modulo(a + 1, SIZE_OF_REJESTRY);
  232.     if(temp_c != 0){
  233.         temp_b = REJESTRY[b];
  234.         REJESTRY[a] = temp_b / temp_c;
  235.         REJESTRY[next_index] = temp_b % temp_c;
  236.     }
  237.     *PC = *PC + 1;
  238. }
  239.  
  240. void push(int *REJESTRY, int *MEMORY, int a, int b, int *PC)
  241. {
  242.     int index = modulo( --REJESTRY[a], SIZE_OF_MEMORY);
  243.     MEMORY[index] = REJESTRY[b];
  244.     *PC = *PC + 1;
  245. }
  246.  
  247. void halt(int *REJESTRY, int a, int *PC)
  248. {
  249.     exit(REJESTRY[a]);
  250.     *PC = *PC + 1;
  251. }
  252.  
  253. /*1*/
  254. void return_function(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  255. {
  256.     int temp_c = *PC;
  257.     *PC = MEMORY[REJESTRY[a]];
  258.     REJESTRY[a] += REJESTRY[c] + 1;
  259.     REJESTRY[a] = modulo(REJESTRY[a], SIZE_OF_MEMORY);
  260.     REJESTRY[b] = temp_c;
  261. }
  262.  
  263. void pop(int *REJESTRY, int *MEMORY, int a, int b, int *PC)
  264. {
  265.     REJESTRY[b] = MEMORY[REJESTRY[a]];
  266.     *PC = *PC + 1;
  267. }
  268.  
  269. void return_from_subroutine(int *REJESTRY, int a, int c, int *PC)
  270. {
  271.     int temp_c = *PC;
  272.     *PC = REJESTRY[c];
  273.     REJESTRY[a] = temp_c;
  274. }
  275.  
  276. /*2*/
  277. void compare(int *REJESTRY, int a, int b, int c, int *PC)
  278. {
  279.     REJESTRY[a] = (REJESTRY[b] < REJESTRY[c]) ? 1 : 0;
  280.     *PC = *PC + 1;
  281. }
  282.  
  283. void shift_left(int *REJESTRY, int a, int b, int *PC)
  284. {
  285.     REJESTRY[a] = REJESTRY[b] << 1; /*czy takze modulo*/
  286.     REJESTRY[a] = modulo(REJESTRY[a], SIZE_OF_MEMORY);
  287.     *PC = *PC + 1;
  288. }
  289.  
  290. /*3*/
  291. void subtract(int *REJESTRY, int a, int b, int c, int *PC)
  292. {
  293.     REJESTRY[a] = modulo(REJESTRY[b] - REJESTRY[c], SIZE_OF_MEMORY);
  294.     *PC = *PC + 1;
  295. }
  296.  
  297. void shift_right(int *REJESTRY, int a, int b, int *PC)
  298. {
  299.     REJESTRY[a] = REJESTRY[b] >> 1;
  300.     REJESTRY[a] = modulo(REJESTRY[a], SIZE_OF_MEMORY);
  301.     *PC = *PC + 1;
  302. }
  303.  
  304. /*4*/
  305. void load_index(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  306. {
  307.     int index_in_memory = modulo(REJESTRY[b] + REJESTRY[c], SIZE_OF_MEMORY);
  308.     REJESTRY[a] = MEMORY[index_in_memory];
  309.     *PC = *PC + 1;
  310. }
  311.  
  312. void add(int *REJESTRY, int a, int b, int c, int *PC)
  313. {
  314.     REJESTRY[a] = modulo(REJESTRY[b] + REJESTRY[c], SIZE_OF_MEMORY);
  315.     *PC = *PC + 1;
  316.     printf("U\n");
  317.     show_arr(REJESTRY, SIZE_OF_REJESTRY);
  318.  
  319. }
  320.  
  321. /*5*/
  322. void store_index(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  323. {
  324.     int index = modulo(REJESTRY[b] + REJESTRY[c], SIZE_OF_MEMORY);
  325.     *PC = *PC + 1;
  326.     MEMORY[index] = REJESTRY[a];
  327. }
  328.  
  329. void bitwise_or(int *REJESTRY, int a, int b, int c, int *PC)
  330. {
  331.     REJESTRY[a] = REJESTRY[b] | REJESTRY[c];
  332.     *PC = *PC + 1;
  333. }
  334.  
  335. /*6*/
  336. /*PYTANKO*/
  337. void multiply(int *REJESTRY, int a, int b, int c, int *PC)
  338. {
  339.     int temp = REJESTRY[b] * REJESTRY[c];
  340.     int index_next = modulo(a + 1, SIZE_OF_REJESTRY);
  341.  
  342.     REJESTRY[a] = temp % SIZE_OF_MEMORY;
  343.     REJESTRY[index_next] = temp / SIZE_OF_MEMORY;
  344.     *PC = *PC + 1;
  345. }
  346.  
  347. void bitwise_and(int *REJESTRY, int a, int b, int c, int *PC)
  348. {
  349.     REJESTRY[a] = REJESTRY[b] & REJESTRY[c];
  350.     *PC = *PC + 1;
  351. }
  352.  
  353. /*7*/
  354. void call_indexed(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  355. {
  356.     int temp_pc = *PC;
  357.     *PC = modulo(MEMORY[REJESTRY[b] + REJESTRY[c]], SIZE_OF_MEMORY);
  358.     REJESTRY[a] = temp_pc;
  359. }
  360.  
  361. void bitwise_exclusive_or(int *REJESTRY, int a, int b, int c, int *PC)
  362. {
  363.     REJESTRY[a] = REJESTRY[b] ^ REJESTRY[c];
  364.     *PC = *PC + 1;
  365. }
  366.  
  367. /*8*/
  368. void jump_if_zero(int *REJESTRY, int a, int b, int c, int *PC)
  369. {
  370.     if(REJESTRY[a] == 0) {*PC = modulo(b * 16 + c, SIZE_OF_MEMORY);printf("jest zero\n");}/*czy dobrze*/
  371.     printf("I\n");
  372.     show_arr(REJESTRY, SIZE_OF_REJESTRY);
  373.     printf("PC wynosi %d\n", *PC);
  374. }
  375.  
  376. /*9*/
  377. void jump_if_not_zero(int *REJESTRY, int a, int b, int c, int *PC)
  378. {
  379.     if(REJESTRY[a] != 0) *PC = modulo(b * 16 + c, SIZE_OF_MEMORY);
  380. }
  381.  
  382. /*10*/
  383. void call_subroutine(int *REJESTRY, int a, int b, int c, int *PC)
  384. {
  385.     REJESTRY[a] = *PC;
  386.     *PC = modulo(b * 16 + c, SIZE_OF_MEMORY);
  387. }
  388.  
  389. /*11*/
  390. void call(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  391. {
  392.     MEMORY[modulo(--REJESTRY[a], SIZE_OF_REJESTRY)] = *PC;
  393.     *PC = modulo(b * 16 + c, SIZE_OF_MEMORY);
  394. }
  395.  
  396. /*12*/
  397. /*PYTANKO*/
  398. void load_register(int *REJESTRY,  int *MEMORY, int a, int b, int c, int *PC)
  399. {
  400.     REJESTRY[a] = modulo(MEMORY[b * 16 + c], SIZE_OF_MEMORY);
  401.     *PC = *PC + 1;
  402. }
  403.  
  404. /*13*/
  405. void store_register(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  406. {
  407.     MEMORY[modulo(b * 16 + c, SIZE_OF_MEMORY)] = REJESTRY[a];
  408.     *PC = *PC + 1;
  409. }
  410.  
  411. /*14*/
  412. void load_constant(int *REJESTRY, int a, int b, int c, int *PC)
  413. {
  414.     /*nie mam to sensu mnozyc przez 16 i tak nic z tego samo c zostanie*/
  415.     REJESTRY[a] = modulo(b * 16 + c, SIZE_OF_MEMORY);
  416.     *PC = *PC + 1;
  417. }
  418.  
  419. /*15*/
  420. void system_call(int *REJESTRY, int *MEMORY, int a, int b, int c, int *PC)
  421. {
  422.     int bc = modulo(b * 16 + c, SIZE_OF_MEMORY);
  423.     int temp_a;
  424.     int next_index = modulo(a + 1, SIZE_OF_REJESTRY);
  425.     *PC = *PC + 1;
  426.     switch(bc){
  427.  
  428.         case CORE_DUMP: /* 0 */
  429.             core_dump(REJESTRY[a]);
  430.             break;
  431.  
  432.         case GET_INT: /* 1 */
  433.             if(scanf("%d", &temp_a) != 1) REJESTRY[next_index] = 0;
  434.             else{
  435.                 REJESTRY[next_index] = 1;
  436.                 REJESTRY[a] = temp_a;
  437.                 printf("PCAB\n");
  438.                 show_arr(REJESTRY, SIZE_OF_REJESTRY);
  439.             }
  440.             break;
  441.  
  442.         case PUT_INT: /* 2 */
  443.             printf("WYPISUJE\n");
  444.             printf("%d", REJESTRY[a]);
  445.             break;
  446.  
  447.         case GET_CHAR: /* 3 */
  448.             temp_a = getchar();
  449.             if(temp_a == EOF) REJESTRY[next_index] = 0;
  450.             else{
  451.                 REJESTRY[next_index] = 1;
  452.                 REJESTRY[a] = temp_a;
  453.             }
  454.             break;
  455.  
  456.         case PUT_CHAR: /* 4 */
  457.             printf("PRZED OSTATNIE\n");
  458.             putchar(REJESTRY[a]);
  459.             break;
  460.  
  461.         case PUT_STRING: /* 5 */
  462.             /*dopoki nie ma \0 wypisuj*/
  463.             show_string(&MEMORY[REJESTRY[a]]);
  464.             //printf("%s", &MEMORY[REJESTRY[a]]);/*ze co to ma znaczyc printf("%s", &mem[reg[a]]);*/
  465.             break;
  466.  
  467.         default:
  468.             /*nic nie rób */
  469.             break;
  470.     }
  471. }
  472.  
  473.  
  474. /*chyba tez zle*/
  475.  
  476. /* SEDNO PROGRAMU*/
  477. void interpret(func_data *f, int *REJESTRY, int *MEMORY, int *PC)
  478. {
  479.     int a = f->ARG[0];
  480.     int b = f->ARG[1];
  481.     int c = f->ARG[2];
  482.     int f_code = f->func_code;
  483.     printf("w func data: pc %d, f->code %d, paratmetry: ",f->pc_index, f_code);
  484.     show_arr(f->ARG, NUMBER_OF_ARGUMENTS);
  485.     switch(f_code){
  486.  
  487.         case 0:
  488.             if(b != c) divide(REJESTRY, a, b, c, PC);
  489.             else if(a != b && b == c) push(REJESTRY, MEMORY, a, b, PC);
  490.             else halt(REJESTRY, a, PC);
  491.             break;
  492.  
  493.         case 1:
  494.             if(a != b && a != c) return_function(REJESTRY, MEMORY, a, b, c, PC);
  495.             else if(a != b && a == c) pop(REJESTRY, MEMORY, a, b, PC);
  496.             else return_from_subroutine(REJESTRY, a, c, PC);
  497.             break;
  498.  
  499.         case 2:
  500.             if(b != c) compare(REJESTRY, a, b, c, PC);
  501.             else shift_left(REJESTRY, a, b, PC);
  502.             break;
  503.  
  504.         case 3:
  505.             if(b != c) subtract(REJESTRY, a, b, c, PC);
  506.             else shift_right(REJESTRY, a, b, PC);
  507.             break;
  508.  
  509.         case 4:
  510.             if(b <= c) load_index(REJESTRY, MEMORY, a, b, c, PC);
  511.             else add(REJESTRY, a, b, c, PC);
  512.             break;
  513.  
  514.         case 5:
  515.             if(b <= c) store_index(REJESTRY, MEMORY, a, b, c, PC);
  516.             else bitwise_or(REJESTRY, a, b, c, PC);
  517.             break;
  518.  
  519.         case 6:
  520.             if(b <= c) multiply(REJESTRY, a, b, c, PC);
  521.             else bitwise_and(REJESTRY, a, b, c, PC);
  522.             break;
  523.  
  524.         case 7:
  525.             if(b <= c) call_indexed(REJESTRY, MEMORY, a, b, c, PC);
  526.             else bitwise_exclusive_or(REJESTRY, a, b, c, PC);
  527.             break;
  528.  
  529.         case 8:
  530.             jump_if_zero(REJESTRY, a, b, c, PC);
  531.             break;
  532.  
  533.         case 9:
  534.             jump_if_not_zero(REJESTRY, a, b, c, PC);
  535.             break;
  536.  
  537.         case 10:
  538.             call_subroutine(REJESTRY, a, b, c, PC);
  539.             break;
  540.  
  541.         case 11:
  542.             call(REJESTRY, MEMORY, a, b, c, PC);
  543.             break;
  544.  
  545.         case 12:
  546.             load_register(REJESTRY, MEMORY, a, b, c, PC);
  547.             break;
  548.  
  549.         case 13:
  550.             store_register(REJESTRY, MEMORY, a, b, c, PC);
  551.             break;
  552.  
  553.         case 14:
  554.             load_constant(REJESTRY, a, b, c, PC);
  555.             break;
  556.  
  557.         case 15:
  558.             system_call(REJESTRY, MEMORY, a, b, c, PC);
  559.             break;
  560.  
  561.         default:
  562.             /*NIEZNANA INSTRUKCJA*/
  563.             break;
  564.     }
  565. }
  566.  
  567. void implementation(func_data *f, int *REJESTRY, int *MEMORY, int *PC)
  568. {
  569.     bool not_found = false;
  570.     func_data *help_str1;
  571.     while(!not_found){
  572.         help_str1 = func_data_find_pc(f, *PC);
  573.         if(help_str1 == NULL) not_found = true;
  574.         else interpret(f, REJESTRY, MEMORY, PC);
  575.     }
  576. }
  577.  
  578. int main(void)
  579. {
  580.     int length = 1, start  = 0, PC = 0;
  581.     char *input = read_string(&length);
  582.     int REJESTRY[SIZE_OF_REJESTRY] = {0}, MEMORY[SIZE_OF_MEMORY] = {0},
  583.     Fake[NUMBER_OF_ARGUMENTS] = {-1, -1, -1};
  584.  
  585.     func_data *func1 = func_data_create_node(-1, -1, Fake);
  586.     func_data *func2 = func_data_create_node(-1, -1, Fake);
  587.     func_data *test;
  588.  
  589.  
  590.     /*wypelnianie rejestru i pamieci*/
  591.     fill_data(REJESTRY, &start, input, length);
  592.     fill_data(MEMORY, &start, input, length);
  593.  
  594.     /*wypelnianie dwoch struktur*/
  595.     get_function_data(input, &start, length, func1);
  596.     get_function_data(input, &start, length, func2);
  597.  
  598.     func1 = func1->next;
  599.     func2 = func2->next;
  600.     implementation(func1, REJESTRY, MEMORY, &PC);
  601.     implementation(func2, REJESTRY, MEMORY, &PC);
  602.     /*
  603.     test = func1->next;
  604.     printf("PIERWSZY:\n");
  605.     while(test){
  606.         printf("pc_index %d, func_num %d    ", test->pc_index, test->func_code);
  607.         show_arr(test->ARG, NUMBER_OF_ARGUMENTS);
  608.         test = test->next;
  609.     }
  610.  
  611.     test = func2->next;
  612.     printf("DRUGI:\n");
  613.     while(test){
  614.         printf("pc_index %d, func_num %d    ", test->pc_index, test->func_code);
  615.         show_arr(test->ARG, NUMBER_OF_ARGUMENTS);
  616.         test = test->next;
  617.     }
  618.     */
  619.  
  620.     func_data_release(func1);  
  621.     func_data_release(func2);
  622.     return 0;
  623. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement