#include "asl.h" int main(int argc, char* argv[]) { FILE *fd_src, *fd_dst; int i, r, lineNum; char input_buf[1024]; char *arg[20]; char tmp[1024] = "$"; char*** op = (char***)malloc(maxOpNum * sizeof(char**)); char*** reg = (char***)malloc(maxRegNum * sizeof(char**)); char*** label = (char***)malloc(maxLabelNum * sizeof(char**)); char** FLOAT_TABLE = (char**)malloc(maxFloatNum * sizeof(char*)); if (argc != 2) { printf("Usage: %s input_file_name\n", argv[0]); return -1; } source = (char*)malloc(sizeof(char) * 128); memset(source, '\0', 128); strcpy(source, argv[1]); dest = "output.txt"; opcode_file = "opcode.txt"; register_file = "register.txt"; labelNum = 0; fd_src = fopen(source, "r"); if (fd_src < 0) { printf("%s: File not found!\n", source); return 1; } fd_dst = fopen(dest, "w+"); if (fd_dst < 0) { printf("%s: File not created!\n", dest); fclose(fd_src); return 1; } opNum = load_table(opcode_file, op); regNum = load_table(register_file, reg); //register r1 -> $r1 (the way in which written in compiler's output file) for (i = 0; i < regNum; i++) { strcat(tmp, reg[i][1]); strcpy(reg[i][1], tmp); strcpy(tmp, "$"); printf("%s %s\n", reg[i][0], reg[i][1]); } //count number of instructions (num of Labels excluded!) lineNum = 0; neglectNum = 0; while (fgets(input_buf, 1024, fd_src) > 0) { r = parse(input_buf, arg); if (r == 0) continue; if (countInstructions(lineNum, arg, op) != 0) goto end; lineNum++; } TOTAL_LABELS_NUM = labelNum; printf("---------- This code has %d labels.\n", TOTAL_LABELS_NUM); TOTAL_CONST_FLOAT_NUM = constFloatNum; printf("---------- This code has %d float constants.\n", TOTAL_CONST_FLOAT_NUM); TOTAL_INSTRUCTIONS_NUM = lineNum - TOTAL_LABELS_NUM - TOTAL_CONST_FLOAT_NUM - neglectNum; printf("---------- This code has %d instructions.\n", TOTAL_INSTRUCTIONS_NUM); fclose(fd_src); //search for labels and save into a Label list fd_src = fopen(source, "r"); constFloatNum = 0; lineNum = 0; labelNum = 0; neglectNum = 0; while (fgets(input_buf, 1024, fd_src) > 0) { r = parse(input_buf, arg); if (r == 0) continue; if (getLabels(lineNum, arg, op, label, FLOAT_TABLE) != 0) goto end; lineNum++; } fclose(fd_src); for (i = 0; i < labelNum; i++) { printf("label[%d]: name = %s | value = %s\n", i, label[i][0], label[i][1]); } //decode into binary fd_src = fopen(source, "r"); lineNum = 0; while (fgets(input_buf, 1024, fd_src) > 0) { r = parse(input_buf, arg); if (r == 0 ) continue;//blank lines will be neglect if (assemble(lineNum, arg, r, op, reg, label, fd_dst) != 0) goto end; lineNum++; } /* for (i = 0; i < constFloatNum; i++) { printf("FLOAT_TABLE[%2d] = %s\n", i, FLOAT_TABLE[i]); }*/ printFloatTable(FLOAT_TABLE, fd_dst);//save Float Table in binary to output file end: free(op); free(reg); free(label); free(FLOAT_TABLE); fclose(fd_src); fclose(fd_dst); return 0; }