diff --git a/disassembler b/disassembler new file mode 100755 index 0000000..d8c1617 Binary files /dev/null and b/disassembler differ diff --git a/disassembler.c b/disassembler.c index 30f3cd3..d02703e 100644 --- a/disassembler.c +++ b/disassembler.c @@ -6,6 +6,9 @@ // How to compile? Like this! // gcc -o disassembler disassembler.c -lcjson +// How to run? Like this! +// ./disassembler snake.gb + // Large chunks of the code are from my Intel 8080 emulator typedef struct buffer { @@ -35,11 +38,10 @@ buffer* read_file(char** argv) { cJSON* read_cjson() { FILE *fs = fopen("Opcodes.json", "rb"); - if (fs = NULL) { + if (fs == NULL) { printf("ERROR: Unable to open the Opcodes.json file.\n"); return NULL; } - // read the file contents into a string fseek(fs, 0L, SEEK_END); int fsize = ftell(fs); @@ -62,11 +64,14 @@ cJSON* read_cjson() { cJSON_Delete(json); return NULL; } + free(b->buffer); // almost forgot that dataleak free(b); return json; } void print_buffer(buffer* b) { + // just a check + // equivalent to $ xxd snake.gb for (int i = 0; i < 40; i++) { printf("%02x \n", b->buffer[i]); } @@ -87,7 +92,7 @@ int disassemble8080(buffer* b, int pc, cJSON* json) { } cJSON *command = cJSON_GetObjectItem(unprefixed, code); if (command == NULL) { - printf("Error: Command with opcode %s not found.\n", opcode); + printf("Error: Command with opcode %s not found.\n", code); return 0; } cJSON *mnemonic_item = cJSON_GetObjectItem(command, "mnemonic"); @@ -99,11 +104,11 @@ int disassemble8080(buffer* b, int pc, cJSON* json) { } if (bytes_item != NULL && cJSON_IsNumber(bytes_item)) { - *bytes = bytes_item->valueint; + opbytes = bytes_item->valueint; } // Here ends the cJSON part - printf("%d\n", opbytes); - printf("%s\n", mnemonic); + printf("Number of Opbytes: %d ", opbytes); + printf("Mnemnoic: %s", mnemonic); if (opbytes > 5 || opbytes < 1) { opbytes = 1; @@ -123,7 +128,7 @@ int main (int argc, char** argv) { return 1; } print_buffer(buff); - JSON *json = read_cjson(); + cJSON *json = read_cjson(); if (json == NULL) { printf("main terminated\n");