simpletron_base.c (3796B)
1 /* Exercise 7.18 (a, b, and c) */ 2 3 /* The Simpletron (and LMS) implementation */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 8 #define MEMSIZE 100 9 #define BUFSIZE 21 10 11 /* Input/Output */ 12 #define READ 10 13 #define WRITE 11 14 15 /* Loading/Storing */ 16 #define LOAD 20 17 #define STORE 21 18 19 /* Arithmetical */ 20 #define ADD 30 21 #define SUBTRACT 31 22 #define DIVIDE 32 23 #define MULTIPLY 33 24 25 /* Reference and control */ 26 #define BRANCH 40 27 #define BRANCHNEG 41 28 #define BRANCHZERO 42 29 #define HALT 43 30 31 void checkword(int word, int size); 32 33 int main(void) 34 { 35 int memory[MEMSIZE] = { 0 }; 36 int accumulator = 0, i; 37 38 int m_loc, cmd; /* the memory location and the command */ 39 40 int program[BUFSIZE] = { 41 42 /* Calculate the sum of 2 numbers 43 +1009, +1010, +2009, +3110, 44 +4107, +1109, +4300, +1110, 45 +4300, +0000, +0000 */ 46 47 /* Calculate the largest of 2 numbers 48 +1007, +1008, +2007, +3008, 49 +2109, +1109, +4300, +0000, 50 +0000, +0000 */ 51 52 53 /* 7.18 (a): take 10 numbers (in a loop with a dummy value) 54 and calculate its sum */ 55 /* 56 +1008, +2008, +4106, +3009, 57 +2109, +4000, +1109, +4300, 58 +0000, +0000 59 */ 60 61 /* 7.18 (b): take 7 numbers positive or negative (in a loop 62 with a counter) and calculate the media */ 63 /* 64 7, 7, 1, +1020, +2020, 65 +3021, +2121, +2001, +3102, 66 +2101, +4212, +4003, +2021, 67 +3200, +2120, +1120, +4300, 68 +0000, +0000, +0000, +0000 69 */ 70 71 /* 7.18 (c): take a serie of numbers and determine and print 72 the largest. The first number readed set how many 73 values will be inserted */ 74 +1000, 1, +2000, +2117, 75 +2000, +3101, +2100, +4214, 76 +1018, +2018, +3117, +4104, +2018, +4003, 77 +2017, +1117, +4300 78 }; 79 80 /* Load the program into the memory */ 81 for(i = 0; i < BUFSIZE; i++) { 82 checkword(program[i], MEMSIZE); /* check the word */ 83 memory[i] = program[i]; /* passed - loaded */ 84 } 85 86 /* Execute the program: i use BUFSIZE to optimize */ 87 for(i = 0; i < MEMSIZE; i++) { 88 m_loc = memory[i] % 100; 89 cmd = memory[i] / 100; 90 91 /* this is required because after the switch() 92 statement the 'i' counter is incremented of 1. */ 93 if(cmd >= BRANCH) 94 --m_loc; 95 96 switch(cmd) { 97 case READ: 98 printf("Insert a word: "); 99 scanf("%d", &memory[m_loc]); 100 101 break; 102 case WRITE: 103 printf("\nMemory location: %d\nWord: %d\n", 104 m_loc, memory[m_loc]); 105 break; 106 case LOAD: 107 accumulator = memory[m_loc]; 108 break; 109 case STORE: 110 memory[m_loc] = accumulator; 111 break; 112 113 case ADD: 114 accumulator += memory[m_loc]; 115 break; 116 case SUBTRACT: 117 accumulator -= memory[m_loc]; 118 break; 119 case DIVIDE: 120 accumulator /= memory[m_loc]; 121 break; 122 case MULTIPLY: 123 accumulator *= memory[m_loc]; 124 break; 125 126 case BRANCH: 127 i = m_loc; 128 break; 129 case BRANCHNEG: 130 if(accumulator < 0) 131 i = m_loc; 132 133 break; 134 case BRANCHZERO: 135 if(!accumulator) 136 i = m_loc; 137 138 break; 139 140 case HALT: 141 return 0; 142 143 case 0: 144 break; 145 146 default: 147 printf("simpletron: unknown error.\n"); 148 exit(-1); 149 } 150 } /* end for (i) */ 151 152 return 0; 153 } /* E0F main */ 154 155 /* check if a "word" is correct */ 156 void checkword(int word, int size) 157 { 158 if(word < 0 || word > 9999 || word % 100 >= size) 159 printf("*** Simpletron: unkwown word: %d\n", word); 160 161 switch(word / 100) { 162 case READ: 163 case WRITE: 164 case LOAD: 165 case STORE: 166 case ADD: 167 case SUBTRACT: 168 case DIVIDE: 169 case MULTIPLY: 170 case BRANCH: 171 case BRANCHNEG: 172 case BRANCHZERO: 173 case HALT: 174 case 0: 175 break; 176 default: 177 printf("*** Simpletron: unknown word: %d\n", word); 178 exit(-1); 179 180 } /* end switch (word) */ 181 182 } /* eof checkword() */ 183