#include #include #include #include #include #include #include #include #include #define MAX_LABEL_LENGTH 20 #define MAX_SYMBOL_COUNT 255 #define MAX_LINE_LENGTH 255 #define VALID_INSTR_CNT 28 #define PSEUDO_OP_COUNT 3 const char valid_op_codes[VALID_INSTR_CNT][8] = { "add", "and", "brn", "brz", "brp", "brnz", "brnp", "brzp", "br", "brnzp", "halt", "jmp", "jsr", "jsrr", "ldb", "ldw", "lea", "nop", "not", "ret", "lshf", "rshfl", "rshfa", "rti", "stb", "stw", "trap", "xor" }; const char pseudo_op_codes[PSEUDO_OP_COUNT][8] = { ".orig", ".end", ".fill" }; enum { DONE, OK, EMPTY_LINE }; typedef struct{ uint16_t address; char label[MAX_LABEL_LENGTH + 1]; } TableEntry; TableEntry symbolTable[MAX_SYMBOL_COUNT]; /* ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// */ FILE* iFilePtr = NULL; FILE* oFilePtr = NULL; uint32_t curr_symbol_count = 0; /* ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// */ uint32_t make_pass_one (void); uint32_t make_pass_two (void); uint32_t process_instruction (char *, char *, char *, char *, char *, uint16_t); uint32_t readAndParse (FILE *, char *, char **, char **, char **, char **, char **, char **); uint32_t is_opcode(char *); /* ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// */ int main (int argc, char ** argv) { int retval=0; /* argc must be 3 for correct execution */ if ( argc != 3 ) { fprintf (stderr, "Usage: %s \n", argv[0]); retval = 4; exit (retval); } iFilePtr = fopen(argv[1], "r"); if (!iFilePtr) { fprintf(stderr, "Error: Cannot open input file %s\n", argv[1]); retval = 4; exit (retval); } oFilePtr = fopen(argv[2], "w"); if (!oFilePtr) { fprintf(stderr, "Error: Cannot open output file %s\n", argv[2]); retval = 4; exit (retval); } retval = make_pass_one (); if (retval) { exit (retval); } /* rewind input file after first pass */ if (fseek (iFilePtr, 0, SEEK_SET)) { retval = 4; exit (retval); } retval = make_pass_two (); if (retval) { exit (retval); } fclose (iFilePtr); fclose (oFilePtr); return (retval); } /* ////////////////////////////////////////////////////////////////////////// // Create Symbol Table in Pass 1 ////////////////////////////////////////////////////////////////////////// */ uint32_t make_pass_one (void) { uint32_t retval; /* write code here */ return retval; } /* ////////////////////////////////////////////////////////////////////////// // Generate Machine Instructions in Pass2 ////////////////////////////////////////////////////////////////////////// */ uint32_t make_pass_two (void) { uint32_t retval; /* write code here */ return retval; } /* ////////////////////////////////////////////////////////////////////////// // Write helper functions here (keep code modular) ////////////////////////////////////////////////////////////////////////// */