Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.98 KB | None | 0 0
  1. /*************************************************
  2. Title: Phase 3
  3. Author: Oscar Alvarez III
  4. Course: 3334 Systems Programming
  5. *************************************************/
  6.  
  7. /////////////////////////////////////////
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13.  
  14. #define WHITESPACE " \t\r\n\v\f"
  15.  
  16.  
  17.  
  18. //Function that parses the string recieved
  19. int prse(char in[], char *a, int size, int i);
  20. //Function that checks for a valid alphanumeric value
  21. int alphanum(int x);
  22. //Function that check for a space
  23. int space(int y);
  24.  
  25. void assemble(char str[]);
  26.  
  27. void pass1(char str[]);
  28.  
  29. void parse2(char line[], char lab[], char mnem[],char op[]);
  30.  
  31. void insertSym(char label[],int LOCCTR);
  32.  
  33. int findLabel(char *label);
  34.  
  35. int searchOp(char *mnemonic);
  36.  
  37. int getHex(char *mnemonic);
  38.  
  39. void pass2();
  40.  
  41. void parse3(char line[], char label[], char mnemonic[], char operand[]);
  42.  
  43. void parse4(char line[], char hex[]);
  44.  
  45. void getloca();
  46.  
  47. int count = 0;
  48.  
  49. typedef struct {
  50. char mn[5];
  51. char op[5];
  52. } OPCODE;
  53.  
  54. OPCODE Ops[] = {
  55.  
  56. { "ADD","0x18"}, {"AND","0x58"}, { "COMP","0x28"}, { "DIV","0x24"},{ "J","0x3C"},
  57. { "JEQ","0x30"}, { "JGT","0x34"},{ "JLT","0x38"},{ "JSUB","0x48"}, { "LDA","0x00"},
  58. { "LDCH","0x50"}, {"LDL","0x08"}, { "LDX","0x04"}, { "MUL","0x20"},{ "OR","0x44"},
  59. { "RD","0xD8"}, { "RSUB","0x4C"}, { "STA","0x0C"}, { "STCH","0x54"},{ "STL","0x14"},
  60. { "STX","0x10"}, { "SUB","0x1C"},{ "TD","0xE0"}, { "TIX","0x2C"}, { "WD","0xDC"}
  61.  
  62. };
  63.  
  64. struct symtab{
  65. char lab[7];
  66. int loca;
  67. }
  68. symtab1[500];
  69.  
  70. int global = 0;
  71.  
  72.  
  73. int main()
  74. {
  75. char str[100]; //input buffer
  76. char a[20]; //Commmand buffer
  77. char b[20]; //1st parameter buffer
  78. char c[20]; //2nd parameter buffer
  79. int len; //length of input string
  80. int i = 0;
  81.  
  82.  
  83.  
  84. printf("Welcome to my command interpreter! Please enter a command.\n");
  85.  
  86. while(1){
  87. i = 0;
  88. memset(a, '\0', 20);
  89. memset(b, '\0', 20);
  90. memset(c, '\0', 20);
  91. fgets(str,100,stdin);
  92.  
  93. len = strlen(str) - 1;
  94. if(str[len] == '\n'){
  95. str[len] = '\0';
  96. }
  97.  
  98. i = prse(str, a, len, i);
  99.  
  100. //checks for help command and outputs menu
  101. if (a[0] == 'h' && a[1] == 'e' && a[2] == 'l' && a[3] == 'p'){
  102. printf("\n");
  103. printf("load 'filename': Load the specified file.\n");
  104. printf("execute: Execute previous loaded program. \n");
  105. printf("debug: Execute in debug mode.\n");
  106. printf("dump 'start' 'end': Execute dump with start/end parameters in hex.\n");
  107. printf("help: Displays list of available commands.\n");
  108. printf("assemble 'filename': Assemble an SIC asl program into a load module.\n");
  109. printf("directory: List the files stored in the current directory.\n");
  110. printf("exit: Exit the command interpreter.\n");
  111. printf("clear: Clears the screen.\n");
  112. printf("\n");
  113. }
  114. //checks for load command and filename
  115. else if (a[0] == 'l' && a[1] == 'o' && a[2] == 'a' && a[3] == 'd'){
  116. if (alphanum(str[i])){
  117. i = prse(str, b, len, i);
  118. printf("You entered filename: %s\n", b);
  119. printf("This function is not yet implemented!\n");
  120. }
  121. else{
  122. printf("Please include the name of the file.\n");
  123. }
  124. }
  125. //checks for execute command
  126. else if (a[0] == 'e' && a[1] == 'x' && a[2] == 'e' && a[3] == 'c' && a[4] == 'u' && a[5] == 't' && a[6] == 'e'){
  127. printf("This function is not yet implemented!\n");
  128. }
  129. //checks for debug command
  130. else if (a[0] == 'd' && a[1] == 'e' && a[2] == 'b' && a[3] == 'u' && a[4] == 'g'){
  131. printf("This function is not yet implemented!\n");
  132. }
  133. //checks for dump command and start/end parameters
  134. else if (a[0] == 'd' && a[1] == 'u' && a[2] == 'm' && a[3] == 'p'){
  135. if (alphanum(str[i])){
  136. i = prse(str, b, len, i);
  137. printf("You entered start value: %s\n", b);
  138.  
  139. if (alphanum(str[i])){
  140. i = prse(str, c, len, i);
  141. printf("You entered end value: %s\n", c);
  142. printf("This function is not yet implemented!\n");
  143. }
  144. else{
  145. printf("Please include an end parameter in hexadecimal.\n");
  146. }
  147. }
  148. else{
  149. printf("Please include a start/end parameter in hexadecimal.\n");
  150. }
  151. }
  152. //checks for assemble command and filename
  153. else if (a[0] == 'a' && a[1] == 's' && a[2] == 's' && a[3] == 'e' && a[4] == 'm' && a[5] == 'b' && a[6] == 'l' && a[7] == 'e'){
  154. if (alphanum(str[i])){
  155. i = prse(str, b, len, i);
  156.  
  157. if( access( b, F_OK ) != -1 ) {
  158. printf("You entered filename: %s\n", b);
  159. assemble(b);
  160. }
  161. else {
  162. printf("File '%s' does not exist! Please re-enter your command with an existing file.\n", b);
  163. }
  164.  
  165.  
  166. }
  167. else{
  168. printf("Please include the name of the file.\n");
  169. }
  170. }
  171. //checks for directory command and displays items in current directory
  172. else if (a[0] == 'd' && a[1] == 'i' && a[2] == 'r' && a[3] == 'e' && a[4] == 'c' && a[5] == 't' && a[6] == 'o' && a[7] == 'r' && a[8] == 'y'){
  173. system("ls");
  174. }
  175. //checks for exit command and exits command interpreter
  176. else if (a[0] == 'e' && a[1] == 'x' && a[2] == 'i' && a[3] == 't'){
  177. printf("Goodbye!\n");
  178. exit(EXIT_SUCCESS);
  179. }
  180. //checks for clear command and clears screen
  181. else if (a[0] == 'c' && a[1] == 'l' && a[2] == 'e' && a[3] == 'a' && a[4] == 'r'){
  182. system("clear");
  183. }
  184. //if no valid input, returns error message
  185. else{
  186. printf("Invalid command! Please type 'help' for list of commands.\n");
  187. }
  188. }
  189. return 0;
  190. }
  191.  
  192. int prse(char in[], char *a, int size, int i){
  193. int j = 0;
  194. for(i; i < size; i++){
  195. a[j] = in[i];
  196.  
  197. if(space(in[i])){
  198. a[j] = '\0';
  199. while(space(in[i])){
  200. i++;
  201. }
  202. return i;
  203. }
  204. j++;
  205. }
  206. }
  207.  
  208. int space(int y){
  209. if (y == 32){
  210. return 1;
  211. }
  212. else{
  213. return 0;
  214. }
  215. }
  216.  
  217. int alphanum(int x){
  218. if (x <= 122 && x >= 48){
  219. return 1;
  220. }
  221. else{
  222. return 0;
  223. }
  224. }
  225.  
  226. void assemble(char str[]){
  227.  
  228. pass1(str);
  229. pass2();
  230.  
  231. }
  232.  
  233. void pass1(char str[]){
  234. int c;
  235. FILE *outfile;
  236. FILE *infile;
  237. char line[1000];
  238. char *backup = NULL;
  239. size_t len= 0;
  240. char read;
  241. char label[15];
  242. char mnemonic[15];
  243. char operand[15];
  244. int holder;
  245. int flag;
  246.  
  247. unsigned LOCCTR = 0x00;
  248. unsigned startingAddress;
  249.  
  250.  
  251. infile = fopen(str, "r");
  252. outfile = fopen("intermediate.txt", "w");
  253.  
  254. if(fgets (line, 1000, infile) != NULL){
  255.  
  256. backup = (char*)malloc(strlen(line)+1);
  257. memset(backup, 0, strlen(line) + 1);
  258. strncpy(backup, line, strlen(line));
  259.  
  260.  
  261.  
  262. parse2(line, label, mnemonic, operand);
  263.  
  264.  
  265. if(strncmp(mnemonic, "START", 15) == 0 && strlen(mnemonic) == 5){
  266. startingAddress = strtoul(operand, NULL, 16);
  267. LOCCTR = startingAddress;
  268. fprintf(outfile, "// %s", backup);
  269. fprintf(outfile, "%s ", mnemonic);
  270. fprintf(outfile, "%04X", LOCCTR);
  271. /////////////////////////////////////////////
  272. flag = getHex(mnemonic);
  273.  
  274. if (flag >= 0){
  275. fprintf(outfile, " %s", Ops[flag].op);
  276. }
  277. else
  278. {
  279. fprintf(outfile, " XXXX");
  280. }
  281. /////////////////////////////////////////////
  282. if(strncmp(mnemonic, "RSUB", 15) == 0 && strlen(mnemonic) == 4){
  283. fprintf(outfile, " XXXX\n");
  284. }
  285. else{
  286. fprintf(outfile, " %s\n", operand);
  287. }
  288.  
  289. }
  290.  
  291. else{
  292. startingAddress = 0;
  293. LOCCTR = startingAddress;
  294. }
  295.  
  296. if(backup){
  297. free(backup);
  298. }
  299.  
  300. fgets (line, 1000, infile);
  301. backup = (char*)malloc(strlen(line)+1);
  302. memset(backup, 0, strlen(line) + 1);
  303. strncpy(backup, line, strlen(line));
  304. parse2(line, label, mnemonic, operand);
  305. while (strncmp(mnemonic, "END", 15) != 0){
  306.  
  307. if(line[0] != '.'){
  308.  
  309. if(isalpha(label[0])){
  310.  
  311. holder = findLabel(label);
  312. if(holder == 0){
  313. insertSym(label, LOCCTR);
  314. global++;
  315. }
  316. else{
  317. //set error flag(duplicate symbol)
  318. }
  319. }
  320.  
  321. if(searchOp(mnemonic) == 1){
  322. LOCCTR = LOCCTR + 3;
  323. }
  324. else if(strncmp(mnemonic, "WORD", 15) == 0 && strlen(mnemonic) == 4){
  325. LOCCTR = LOCCTR + 3;
  326. }
  327. else if(strncmp(mnemonic, "RESW", 15) == 0 && strlen(mnemonic) == 4){
  328. LOCCTR = LOCCTR + (3 * strtoul(operand, NULL, 16));
  329. }
  330. else if(strncmp(mnemonic, "RESB", 15) == 0 && strlen(mnemonic) == 4){
  331. LOCCTR = LOCCTR + strtoul(operand, NULL, 10);
  332. }
  333. else if(strncmp(mnemonic, "BYTE", 15) == 0 && strlen(mnemonic) == 4){
  334. if(operand[0] == 'C'){
  335. LOCCTR = LOCCTR + (strlen(operand)-3);
  336. }
  337. else if(operand[0] == 'X'){
  338. LOCCTR = LOCCTR + (strlen(operand)-3)/2;
  339. }
  340. }
  341. else{
  342. //set error flag
  343. }
  344. }
  345. fprintf(outfile, "// %s", backup);
  346. fprintf(outfile, "%s ", mnemonic);
  347. fprintf(outfile, "%04X", LOCCTR);
  348. //////////////////////////////////////////////////
  349. flag = getHex(mnemonic);
  350.  
  351. if (flag >= 0){
  352. fprintf(outfile, " %s", Ops[flag].op);
  353. }
  354. else
  355. {
  356. fprintf(outfile, " XXXX");
  357. }
  358. //////////////////////////////////////////////////
  359. if(strncmp(mnemonic, "RSUB", 15) == 0 && strlen(mnemonic) == 4){
  360. fprintf(outfile, " XXXX\n");
  361. }
  362. else{
  363. fprintf(outfile, " %s\n", operand);
  364. }
  365.  
  366. if(backup){
  367. free(backup);
  368. }
  369.  
  370. fgets (line, 1000, infile);
  371. backup = (char*)malloc(strlen(line)+1);
  372. memset(backup, 0, strlen(line) + 1);
  373. strncpy(backup, line, strlen(line));
  374. parse2(line, label, mnemonic, operand);
  375. }
  376. fprintf(outfile, "// %s\n", backup);
  377. fprintf(outfile, "%s ", mnemonic);
  378. fprintf(outfile, "%04X", LOCCTR);
  379. ////////////////////////////////////////////////
  380. flag = getHex(mnemonic);
  381.  
  382. if (flag >= 0){
  383. fprintf(outfile, " %s", Ops[flag].op);
  384. }
  385. else
  386. {
  387. fprintf(outfile, " XXXX");
  388. }
  389. ////////////////////////////////////////////////
  390. if(strncmp(mnemonic, "RSUB", 15) == 0 && strlen(mnemonic) == 4){
  391. fprintf(outfile, " XXXX\n");
  392. }
  393. else{
  394. fprintf(outfile, " %s\n", operand);
  395. }
  396. fprintf(outfile, "%s", backup);
  397. if(backup){
  398. free(backup);
  399. }
  400.  
  401.  
  402. //save (LOCCTR - starting address_ as program length)
  403. }
  404.  
  405. fclose(infile);
  406. fclose(outfile);
  407. int i = 0;
  408.  
  409. while(symtab1[i].loca != 0){
  410. printf("%s", symtab1[i].lab);
  411. printf(" %X\n", symtab1[i].loca);
  412. i++;
  413.  
  414. }
  415.  
  416. }
  417.  
  418. void parse2(char line[], char lab[], char mnem[],char op[]){
  419. char * pch;
  420. char * ilab;
  421. char * imnem;
  422. char * iop;
  423. memset(lab, 0, 15);
  424. memset(mnem, 0, 15);
  425. memset(op, 0, 15);
  426.  
  427. //not a comment
  428. if(line[0] != '.'){
  429.  
  430. //3 relevant items
  431. if(!isspace(line[0])){
  432. ilab = strtok (line, WHITESPACE);
  433. strncpy(lab, ilab, 14);
  434. imnem = strtok (NULL, WHITESPACE);
  435. strncpy(mnem, imnem, 14);
  436. iop = strtok (NULL, WHITESPACE);
  437. strncpy(op, iop, 14);
  438.  
  439. }
  440. //2 relevant items
  441. else{
  442. imnem = strtok (line, WHITESPACE);
  443. strncpy(mnem, imnem, 14);
  444. iop = strtok (NULL, WHITESPACE);
  445. strncpy(op, iop, 14);
  446. }
  447. }
  448. }
  449.  
  450. //parses comment line in intermediate line
  451. void parse3(char line[], char label[], char mnemonic[], char operand[]){
  452. char * ilab;
  453. char * imnem;
  454. char * iop;
  455. memset(label, 0, 15);
  456. memset(mnemonic, 0, 15);
  457. memset(operand, 0, 15);
  458. imnem = strtok (line, WHITESPACE);
  459. ilab = strtok (NULL, WHITESPACE);
  460. imnem = strtok (NULL, WHITESPACE);
  461. iop = strtok (NULL, WHITESPACE);
  462. strncpy(mnemonic, imnem, 14);
  463. }
  464.  
  465. //parses everyother line that isn't commented
  466. void parse4(char line[], char hex[]){
  467. char * ihex;
  468. char x[2];
  469. memset(hex, 0, 15);
  470. ihex = strtok (line, WHITESPACE);
  471. ihex = strtok (NULL, WHITESPACE);
  472. x[0] = ihex[2];
  473. x[1] = ihex[3];
  474. puts(ihex);
  475. puts(x);
  476. strncpy(hex, x, 14);
  477. }
  478.  
  479. int findLabel(char *label){
  480. int i = 0;
  481. for(i = 0; i < global; i++){
  482. if(strcmp(symtab1[i].lab, label) == 0){
  483. return 1;
  484. }
  485. return 0;
  486. }
  487. }
  488.  
  489. void insertSym(char label[],int LOCCTR){
  490. //puts(label);
  491. strncpy(symtab1[count].lab, label,14);
  492. symtab1[count].loca = LOCCTR;
  493. count++;
  494. }
  495.  
  496. int searchOp(char *mnemonic){
  497. int i = 0;
  498. while(i < sizeof(Ops)){
  499. if(strcmp(Ops[i].mn, mnemonic) == 0){
  500. return 1;
  501. }
  502.  
  503. i++;
  504. }
  505. return 0;
  506. }
  507.  
  508. int getHex(char *mnemonic){
  509. int i = 0;
  510. while(i < sizeof(Ops)){
  511. if(strcmp(Ops[i].mn, mnemonic) == 0){
  512. return i;
  513. }
  514.  
  515. i++;
  516. }
  517. return -1;
  518. }
  519.  
  520. void getloca(){
  521.  
  522. }
  523.  
  524. void pass2(){
  525.  
  526. FILE *infile;
  527. FILE *outlist;
  528. FILE *outobj;
  529. char line[1000];
  530. char label[15];
  531. char mnemonic[15];
  532. char operand[15];
  533. char hex[15];
  534.  
  535. infile = fopen("intermediate.txt", "r");
  536. outlist = fopen("listing.txt", "w");
  537. outobj = fopen("object.txt", "w");
  538. int i = 0;
  539.  
  540. //read first input line from int file
  541. if(fgets (line, 1000, infile) != NULL){
  542. parse3(line, label, mnemonic, operand);
  543.  
  544. }
  545.  
  546. fgets (line, 1000, infile);
  547. parse4(line, hex);
  548.  
  549.  
  550. if(strncmp(mnemonic, "START", 15) == 0 && strlen(mnemonic) == 5){
  551. //write listing line
  552.  
  553. fprintf(outlist, "%s1000", hex);
  554. //read next input line
  555. }
  556.  
  557. //write header record to object program
  558. //initialize first Text record
  559.  
  560. /*while(OPCODE != 'END'){
  561. if (not a comment line){
  562. //seatch OPTAB for OPCODE
  563. if(found){
  564. if(there is a symbol in OPERAND field){
  565. //search SYMTAB for OPERAND
  566. if(found){
  567. //store symbol value as operand address
  568. }
  569. else{
  570. //store 0 as operand address
  571. //set error flag (undefined symbol)
  572. }
  573. }
  574. else{
  575. //store 0 as operand address
  576. }
  577. //assemble the object code instruction
  578. }
  579. else if{OPCODE = 'BYTE' or 'WORD'}{
  580. //convert constant to object code
  581. }
  582. if(//object code will not fit into the current Text record){
  583. //write Text record to object program
  584. //initialize new Text record
  585. }
  586. //add object code to Text record
  587. }
  588. //write listing line
  589. //read next input line
  590. }
  591. //write last Text record to object program
  592. //write End record to object program
  593. //write last listing line
  594. */ fclose(infile);
  595. fclose(outlist);
  596. fclose(outobj);
  597. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement