Advertisement
DrAungWinHtut

simpletron_v2.c

Dec 14th, 2022
824
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define READ    10
  4. #define WRITE   11
  5. #define LOAD    20
  6. #define STORE   21
  7. #define ADD     30
  8. #define SUBTRACT 31
  9. #define DIVIDE  32
  10. #define MULTIPLY 33
  11. #define BRANCH 40
  12. #define BRANCHNEG 41
  13. #define BRANCHZERO 42
  14. #define HALT 43
  15.  
  16.  
  17.  
  18. int main()
  19. {
  20.     int memory[100] = { 0 };
  21.     int accumulator = 0;
  22.     int instruction_counter = 0; //boot address - 0000
  23.     int instruction_register = 0;
  24.     int operation_code = 0;
  25.     int operand = 0;
  26.     /*
  27.     FILE* fp;
  28.     fopen_s(&fp,"d:\\test.txt", "w");
  29.     if (fp != NULL)
  30.     {
  31.         fprintf_s(fp, "%d\n", 1050);
  32.         fprintf_s(fp, "%d\n", 2050);
  33.         fprintf_s(fp, "%d\n", 1051);
  34.         fprintf_s(fp, "%d\n", 3051);
  35.         fprintf_s(fp, "%d\n", 2152);
  36.         fprintf_s(fp, "%d\n", 1152);
  37.         fprintf_s(fp, "%d\n", 4300);
  38.         fclose(fp);
  39.     }
  40.     */
  41.     int index = 0;
  42.     FILE* fin;
  43.     fopen_s(&fin, "D:\\test.txt", "r");
  44.     printf("EOF = %d\n\n", EOF);
  45.     if (fin != NULL)
  46.     {
  47.         while (fscanf_s(fin, "%d", &memory[index++]) > 0)
  48.         {
  49.  
  50.         }
  51.    
  52.        
  53.         fclose(fin);
  54.     }
  55.  
  56.  
  57.  
  58.     //LOAD PROGRAM INTO MEMORY
  59.     //memory[0] = 1050;
  60.     //memory[1] = 2050;
  61.     //memory[2] = 1051;
  62.     //memory[3] = 3051;
  63.     //memory[4] = 2152;
  64.     //memory[5] = 1152;
  65.     //memory[6] = 4300;
  66.  
  67.     while (1)
  68.     {
  69.         instruction_register = memory[instruction_counter++];
  70.         operation_code = instruction_register / 100;
  71.         operand = instruction_register % 100;
  72.         switch (operation_code)
  73.         {
  74.         case 10:
  75.             scanf_s("%d", &memory[operand]);
  76.             break;
  77.         case 11:
  78.             printf("%d", memory[operand]);
  79.             break;
  80.         case 20:
  81.             accumulator = memory[operand];
  82.             break;
  83.         case 21:
  84.             memory[operand] = accumulator;
  85.             break;
  86.         case 30:
  87.             accumulator += memory[operand];
  88.             break;
  89.         case 31:
  90.             accumulator -= memory[operand];
  91.             break;
  92.         case 32:
  93.             accumulator /= memory[operand];
  94.             break;
  95.         case 33:
  96.             accumulator *= memory[operand];
  97.             break;
  98.         case 40:
  99.             instruction_counter = operand;
  100.             break;
  101.         case 41:
  102.             if (accumulator < 0)
  103.             {
  104.                 instruction_counter = operand;
  105.             }
  106.             break;
  107.         case 42:
  108.             if (accumulator == 0)
  109.             {
  110.                 instruction_counter = operand;
  111.             }
  112.             break;
  113.         case 43:
  114.             printf("Program is terminated!");
  115.             exit(0);
  116.             break;
  117.         default:
  118.             printf("Err! No such instruction!!!! . Program is terminated!");
  119.             exit(0);
  120.             break;
  121.         }
  122.  
  123.     }
  124.  
  125.  
  126.     return 0;
  127. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement