Advertisement
DrAungWinHtut

simpletron_v1.c

Dec 14th, 2022
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 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.     //LOAD PROGRAM INTO MEMORY
  27.     memory[0] = 1050;
  28.     memory[1] = 2050;
  29.     memory[2] = 1051;
  30.     memory[3] = 3051;
  31.     memory[4] = 2152;
  32.     memory[5] = 1152;
  33.     memory[6] = 4300;
  34.  
  35.     while (1)
  36.     {
  37.         instruction_register = memory[instruction_counter++];
  38.         operation_code = instruction_register / 100;
  39.         operand = instruction_register % 100;
  40.         switch (operation_code)
  41.         {
  42.         case 10:
  43.             scanf_s("%d", &memory[operand]);
  44.             break;
  45.         case 11:
  46.             printf("%d", memory[operand]);
  47.             break;
  48.         case 20:
  49.             accumulator = memory[operand];
  50.             break;
  51.         case 21:
  52.             memory[operand] = accumulator;
  53.             break;
  54.         case 30:
  55.             accumulator += memory[operand];
  56.             break;
  57.         case 31:
  58.             accumulator -= memory[operand];
  59.             break;
  60.         case 32:
  61.             accumulator /= memory[operand];
  62.             break;
  63.         case 33:
  64.             accumulator *= memory[operand];
  65.             break;
  66.         case 40:
  67.             instruction_counter = operand;
  68.             break;
  69.         case 41:
  70.             if (accumulator < 0)
  71.             {
  72.                 instruction_counter = operand;
  73.             }
  74.             break;
  75.         case 42:
  76.             if (accumulator == 0)
  77.             {
  78.                 instruction_counter = operand;
  79.             }
  80.             break;
  81.         case 43:
  82.             printf("Program is terminated!");
  83.             exit(0);
  84.             break;
  85.         default:
  86.             printf("Err! No such instruction!!!! . Program is terminated!");
  87.             exit(0);
  88.             break;
  89.         }
  90.  
  91.     }
  92.  
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement