Advertisement
Guest User

part5.c

a guest
May 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6.  
  7. void filterDigit (char *s)
  8. {
  9. char *p = s;
  10. for (*p; *p; ++p)
  11. if (isdigit(*p))
  12. *s++ = *p;
  13. *s = '\0';
  14. }
  15.  
  16. void decToBin (char *value, char* output)
  17. {
  18. int v, i;
  19. v = atoi(value);
  20. if (v > 8 | v < 0)
  21. {
  22. output[5] = '\0';
  23. for (i = 4; i >= 0; --i, v >>= 1)
  24. {
  25. output[i] = (v & 1) + '0';
  26. }
  27. } else {
  28. output[3] = '\0';
  29. for (i = 2; i >= 0; --i, v >>= 1)
  30. {
  31. output[i] = (v & 1) + '0';
  32. }
  33. }
  34. }
  35.  
  36. void binToHex(const char *inStr, char *outStr) {
  37. static char hex[] = "0123456789abcdef";
  38. int len = strlen(inStr) / 4;
  39. int i = strlen(inStr) % 4;
  40. char current = 0;
  41. if(i)
  42. {
  43. while(i--)
  44. {
  45. current = (current << 1) + (*inStr - '0');
  46. inStr++;
  47. }
  48. *outStr = hex[current];
  49. ++outStr;
  50. }
  51. while(len--)
  52. {
  53. current = 0;
  54. for(i = 0; i < 4; ++i)
  55. {
  56. current = (current << 1) + (*inStr - '0');
  57. inStr++;
  58. }
  59. *outStr = hex[current];
  60. ++outStr;
  61. }
  62. *outStr = 0;}
  63.  
  64. bool isInt(const char *str)
  65. {
  66. if (*str == '-')
  67. ++str;
  68. if (!*str)
  69. return false;
  70. while (*str)
  71. {
  72. if (!isdigit(*str))
  73. return false;
  74. else
  75. ++str;
  76. }
  77.  
  78. return true;
  79. }
  80.  
  81. int main(int argc, char *argv[]) {
  82. int immediateAdd = 1, pc = (int)strtol(argv[3], NULL, 16);
  83. if (argc != 4)
  84. {
  85. printf("Usage: %s filename\nor\nUsage: %s filename sTableName address\n", argv[0]);
  86. } else {
  87. FILE *file, *sTable;
  88. char opCode[4], srcDest[2], src1[2], src2[5], instCode[20], line[25], sTableSymbol[5], sTableValue[5];
  89. file = fopen(argv[1], "r");
  90. sTable = fopen(argv[2], "r");
  91. while (fgets(line, 25, file) != NULL)
  92. {
  93. sscanf(line, "%s %[^,],%[^,],%s", opCode, srcDest, &src1, &src2);
  94. pc++;
  95.  
  96. if (strncmp(opCode, "br", 2) != 0)
  97. {
  98. filterDigit(srcDest);
  99. decToBin(srcDest, srcDest);
  100. }
  101.  
  102. if (strcmp(opCode, "ld") != 0)
  103. {
  104. filterDigit(src1);
  105. decToBin(src1, src1);
  106. }
  107.  
  108. if (!isInt(src2))
  109. {
  110. filterDigit(src2);
  111. immediateAdd = 0;
  112. }
  113.  
  114. decToBin(src2, src2);
  115. if (strncmp(opCode, "br", 2) == 0)
  116. {
  117. while (fscanf(sTable, "%s %s", sTableSymbol, sTableValue) != EOF)
  118. {
  119. int n = 0, z = 0, p = 0, i;
  120. const char *nCheck = "n", *zCheck = "z", *pCheck = "p";
  121. char *opcCheck = opCode;
  122. while (*opcCheck)
  123. {
  124. if (strchr(nCheck, *opcCheck))
  125. {
  126. n = 1;
  127. }
  128. if (strchr(zCheck, *opcCheck))
  129. {
  130. z = 1;
  131. }
  132. if (strchr(pCheck, *opcCheck))
  133. {
  134. p = 1;
  135. }
  136. opcCheck++;
  137. }
  138.  
  139. if (strncmp(srcDest, sTableSymbol, 1) == 0)
  140. {
  141. int offset = (int)strtol(sTableValue, NULL, 16), dif = 0;
  142. dif = offset - pc;
  143. int sta, en;
  144. int count = 0;
  145. char offsetCreate[9]= "";
  146. for(sta = 8; sta >= 0; sta--)
  147. {
  148. en = 1 << sta;
  149. if(en & dif)
  150. {
  151. offsetCreate[count] = '1';
  152. } else {
  153. offsetCreate[count] = '0';
  154. }
  155. count++;
  156. }
  157. offsetCreate[9] = 0;
  158. sprintf(instCode, "0000%d%d%d%s", n, z, p, offsetCreate);
  159. }
  160. }
  161. sTable = fopen(argv[2], "r");
  162. } else if (strcmp(opCode, "ld") == 0) {
  163. while (fscanf(sTable, "%s %s", sTableSymbol, sTableValue) != EOF)
  164. {
  165. if (strncmp(src1, sTableSymbol, 3) == 0)
  166. {
  167. int offset = (int)strtol(sTableValue, NULL, 16), dif = 0;
  168. dif = offset - pc;
  169. int sta, en;
  170. int count = 0;
  171. char offsetCreate[9]= "";
  172. for(sta = 8; sta >= 0; sta--)
  173. {
  174. en = 1 << sta;
  175. if(en & dif)
  176. {
  177. offsetCreate[count] = '1';
  178. } else {
  179. offsetCreate[count] = '0';
  180. }
  181. count++;
  182. }
  183. offsetCreate[9] = 0;
  184. sprintf(instCode, "0010%s%s", srcDest, offsetCreate);
  185. }
  186. }
  187. sTable = fopen(argv[2], "r");
  188. } else if (strcmp(opCode, "jmp") == 0) {
  189. sprintf(instCode, "1100000%s000000", srcDest);
  190. } else if (strcmp(opCode, "add") == 0) {
  191. if (immediateAdd == 0)
  192. {
  193. sprintf(instCode, "0001%s%s000%s", srcDest, src1, src2);
  194. } else {
  195. sprintf(instCode, "0001%s%s1%s", srcDest, src1, src2);
  196. }
  197. } else {
  198. if (immediateAdd == 0)
  199. {
  200. sprintf(instCode, "0101%s%s000%s", srcDest, src1, src2);
  201. } else {
  202. sprintf(instCode, "0101%s%s1%s", srcDest, src1, src2);
  203. }
  204. }
  205. immediateAdd = 1;
  206. binToHex(instCode, instCode);
  207. printf("%s\n", instCode);
  208. }
  209. fclose(file), fclose(sTable);
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement