diff --git a/asm/assembler.c b/asm/assembler.c index 7111cc5..337a03e 100644 --- a/asm/assembler.c +++ b/asm/assembler.c @@ -203,15 +203,42 @@ uint16_t *BinaryInstructionToBin(BinaryInstruction instruction) return bin; } +int WhereIn2DCharArray(int x, int y, char array[x][y], char *val) +{ + for (int i = 0; i < x; i++) + { + if (strcmp(array[i], val) == 0) + return i; + } + + return -1; +} + uint16_t *CompileAsembly(AsmInstructionArray *assembly, AsmLabelArray *labels) { uint16_t *ret = malloc(sizeof(uint16_t) * UINT16_MAX); - uint16_t cSize = 0, mSize = ARRAY_SNAP_SIZE; + uint16_t cSize = 0; for (int i = 0; i < assembly->length; i++) { BinaryInstruction binaryIns; + + int instruction = WhereIn2DCharArray(LEN(CpuInstructionsHumanReadable), + LEN(CpuInstructionsHumanReadable[0]), + CpuInstructionsHumanReadable, + assembly->instruction[i].instruction); + + if (instruction < 0) + { + fprintf(stderr, "INSTRUCTION NOT FOUND\n"); + exit(-1); + } + + binaryIns.instruction = instruction; + + cSize++; } + ret = realloc(ret, cSize); return ret; } diff --git a/asm/assembler.h b/asm/assembler.h index 21c82d2..4af32e6 100644 --- a/asm/assembler.h +++ b/asm/assembler.h @@ -7,6 +7,8 @@ #define ARRAY_SNAP_SIZE 32 +#define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0])) + char *FindNextChar(char *data, char toFind, bool careAboutNull); char *FindNextNonSpaceChar(char *data, bool careAboutNull); char *FindNextSpaceChar(char *data, bool careAboutNull); diff --git a/emu/cpu_human.h b/emu/cpu_human.h index 0d96e58..58173b8 100644 --- a/emu/cpu_human.h +++ b/emu/cpu_human.h @@ -47,7 +47,6 @@ char CpuInstructionsHumanReadable[25][4] = "OUT", "DIN", "DOT", - "MOV" -}; + "MOV"}; #endif \ No newline at end of file