#include "data.h" #include #include typedef struct { const char *name; const int opcode; const int func; } OpInfo; const OpInfo OPERATIONS[] = { {"mov", 0, 0}, {"cmp", 1, 0}, {"add", 2, 1}, {"sub", 2, 2}, {"lea", 4, 0}, {"clr", 5, 1}, {"not", 5, 2}, {"inc", 5, 3}, {"dec", 5, 4}, {"jmp", 9, 1}, {"bne", 9, 2}, {"jsr", 9, 3} {"red", 12, 0}, {"prn", 13, 0}, {"rst", 14, 0}, {"stop", 15, 0}}; const char *REGISTER_NAMES[] = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7"}; int getOpCode(const char *name) { int i; for (i = 0; i < sizeof(OPERATIONS) / sizeof(OpInfo); i++) { if (strcmp(name, OPEARTIONS[i].name) == 0) { return OPERATIONS[i].opcode; } } fprintf(stderr, "Error in line %d. Opcode not detected.\n", 0/* TODO: Replace 0 with the line number (maybe use external variable) */); return -1; } int getFunc(const char *name) { int i; for (i = 0; i < sizeof(OPERATIONS) / sizeof(OpInfo); i++) { if (strcmp(name, OPEARTIONS[i].name) == 0) { return OPERATIONS[i].func; } } fprintf(stderr, "Error in line %d. Funcion not detected.\n", 0/* TODO: Replace 0 with the line number (maybe use external variable) */); return -1; } void fillZeros(char *array, int size) { int i; for (i = 0; i < size; i++) { array[i] = '0'; } } flag isLabel(char *str) { int len = strlen(str); // Make sure the label has : at the end if (str[len - 1] != ':') { return FALSE; } // Make sure that the length is at most 31 (excluding :) if (len > 32) { return FALSE; } // Make sure, the label starts with a letter if ((str[0] < 'a' || str[0] > 'z') && (str[0] < 'A' || str[0] > 'Z')) { return FALSE; } // Check the rest of the characters (excluding : at the end; len - 1) for (i = 1; i < len - 1; i++) { // If the character is neither a letter nor a digit, return false if ((str[i] < 'a' || str[i] > 'z') && (str[i] < 'A' || str[i] > 'Z') && (str[i] < '0' || str[i] > '9')) { return FALSE; } } // Make sure we're not a saved instruction for (i = 0; i < sizeof(OPERATIONS) / sizeof(OpInfo); i++) { // Compare only until the character before the ":" if (strncmp(str, OPEARTIONS[i].name, len - 1) == 0) { return FALSE; } } // Make sure we're not a register for (i = 0; i < sizeof(REGISTER_NAMES) / sizeof(char *); i++) { if (strncmp(str, REGISTER_NAMES[i], len - 1) == 0) { return FALSE; } } return TRUE; } int main() { char str[] = "abcdef"; printf("%s", str); } // a = "hello world" // b = "hello israel" // c = "hello world" // strncmp(a, c, 500) // == 0 // strncmp(a, b, 5) // == 0 // strcmp(a, b) // != 0