Advertisement
Guest User

Bottom Text.exe

a guest
Apr 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MaxProgSize 100
  4. #define MaxMemSize 50
  5.  
  6. struct Instruction {int OpCode, Address;
  7. };
  8.  
  9.  
  10. struct Instruction IM[MaxProgSize];
  11. struct Instruction MDR1;
  12. struct Instruction IR;
  13. int DM[MaxMemSize];
  14. int PC = 0, MAR, MDR2, A;
  15. int j = 0;
  16.  
  17. struct Instruction fetchExe();
  18. int load();
  19. int add();
  20. int store();
  21. int subt();
  22. int in();
  23. int out();
  24. void end();
  25. int jmp();
  26. int skipz();
  27.  
  28. int main(int argc, char *argv[]){
  29. int c;
  30. int i;
  31. //Pointing towards the input file Final.txt
  32. FILE *Final;
  33.  
  34. //Instead of using Final = fopen(argv[1], "r"); ,
  35. //I used this because the command line in DevC++ does not read my file "Final.txt" if it sets on argv[1]
  36. Final = fopen("Final.txt", "r");
  37. // if the file does not exist,
  38. if (Final == NULL)
  39. printf("Cannot open file!");
  40. // if it exists and does work.
  41. else {
  42. printf("Assembling Program...\n");
  43. int i = 0;
  44. while ((c = getc(Final)) != EOF){
  45. c-='0';
  46. if(c<0||c>9){
  47. continue;
  48. }
  49. if(i%2==0){
  50. IM[PC+i/2].OpCode=c;
  51. }else{
  52. IM[PC+i/2].Address=c;
  53. }
  54. i++;
  55. }
  56. printf("Program assembled\n Run.\n");
  57.  
  58. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  59. for(j=0;j<MaxMemSize-1;j++){
  60. printf("%d, ",DM[j]);
  61. }
  62. printf("%d]\n",DM[j]);
  63.  
  64.  
  65.  
  66. while(MaxProgSize > A ){
  67. switch(IR.OpCode){
  68. case 0: fetchExe();
  69. break;
  70. case 1: load();{
  71. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  72. for(j=0;j<MaxMemSize-1;j++){
  73. printf("%d, ",DM[j]);
  74. }
  75. printf("%d]\n",DM[j]);
  76. }
  77. break;
  78. case 2: add();{
  79. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  80. for(j=0;j<MaxMemSize-1;j++){
  81. printf("%d, ",DM[j]);
  82. }
  83. printf("%d]\n",DM[j]);
  84. }
  85. break;
  86. case 3: store();{
  87. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  88. for(j=0;j<MaxMemSize-1;j++){
  89. printf("%d, ",DM[j]);
  90. }
  91. printf("%d]\n",DM[j]);
  92. }
  93. break;
  94. case 4: subt();{
  95. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  96. for(j=0;j<MaxMemSize-1;j++){
  97. printf("%d, ",DM[j]);
  98. }
  99. printf("%d]\n",DM[j]);
  100. }
  101. break;
  102. case 5: in();{
  103. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  104. for(j=0;j<MaxMemSize-1;j++){
  105. printf("%d, ",DM[j]);
  106. }
  107. printf("%d]\n",DM[j]);
  108. }
  109. break;
  110. case 6: out();
  111. break;
  112. case 7: end();
  113. break;
  114. case 8: jmp();{
  115. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  116. for(j=0;j<MaxMemSize-1;j++){
  117. printf("%d, ",DM[j]);
  118. }
  119. printf("%d]\n",DM[j]);
  120. }
  121. break;
  122. case 9: skipz();{
  123. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  124. for(j=0;j<MaxMemSize-1;j++){
  125. printf("%d, ",DM[j]);
  126. }
  127. printf("%d]\n",DM[j]);
  128. }
  129. break;
  130. }
  131. }
  132. printf("Program Complete\n");
  133. fclose(Final);
  134. }
  135. system("PAUSE");
  136. return 0;
  137. }
  138. //Fetching instructions and address to perform operations
  139. struct Instruction fetchExe(){
  140. MAR = PC;
  141. PC = PC +1;
  142. MDR1 = IM[MAR];
  143. IR = MDR1;
  144. return IR;
  145. }
  146.  
  147. //loading the value from Accumulator from DM
  148. int load(){
  149. printf("/*Loading Accumulator to DM %d */ \n", IR.Address);
  150. MAR = IR.Address;
  151. MDR2 = DM[MAR];
  152. A = MDR2;
  153. IR.OpCode = 0;
  154. return IR.OpCode;
  155. }
  156.  
  157.  
  158. //Adding the value of the loaded address to the target address
  159. int add(){
  160. MAR = IR.Address;
  161. MDR2 = DM[MAR];
  162. printf("/*Adding %d and %d */ \n", A, MDR2);
  163. A = A + MDR2;
  164. IR.OpCode = 0;
  165. return IR.OpCode;
  166. }
  167.  
  168. //Storing the value to the target address
  169. int store(){
  170. printf("/* Storing Accumulator to memory %d */ \n", IR.Address);
  171. MAR = IR.Address;
  172. MDR2 = A;
  173. DM[MAR] = MDR2;
  174. IR.OpCode = 0;
  175. return IR.OpCode;
  176. }
  177.  
  178. //Subtracting the value of the loaded address to the target address
  179. int subt(){
  180. MAR = IR.Address;
  181. MDR2 = DM[MAR];
  182. printf("/*Subtracting %d and %d */ \n", A, MDR2);
  183. A = A - MDR2;
  184. IR.OpCode = 0;
  185. return IR.OpCode;
  186. }
  187.  
  188. //Inputting value
  189. int in(){
  190. printf("/* Input value */ \n");
  191. printf("Enter a number: ");
  192. scanf("%d",&A);
  193. IR.OpCode = 0;
  194. return IR.OpCode;
  195. }
  196.  
  197. //showing the value by printing it
  198. int out(){
  199. printf("/* Outputting accumulator to screen */ \n");
  200. printf("PC = %d \t A = %d \t MEM = [",PC,A);
  201. for(j=0;j<MaxMemSize-1;j++){
  202. printf("%d, ",DM[j]);
  203. }
  204. printf("%d]\n",DM[j]);
  205. IR.OpCode = 0;
  206. return IR.OpCode;
  207. }
  208.  
  209.  
  210. void end(){
  211. printf("/*Program terminated.*/ \n");
  212. exit(1);
  213. }
  214.  
  215. int jmp(){
  216. int i;
  217. PC = IR.Address;
  218. printf("/*Jumping to %d */ \n", PC);
  219. IR.OpCode = 0;
  220. return IR.OpCode;
  221. }
  222.  
  223. int skipz(){
  224. int i;
  225. if(A == 0){
  226. PC = PC +1;
  227. IR.OpCode = 0;
  228. return IR.OpCode;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement