Getting closer

This commit is contained in:
Turingon 2024-08-28 18:02:40 +02:00
parent 19900f36ae
commit 70255ed9ef
2 changed files with 12 additions and 7 deletions

BIN
disassembler Executable file

Binary file not shown.

View file

@ -6,6 +6,9 @@
// How to compile? Like this! // How to compile? Like this!
// gcc -o disassembler disassembler.c -lcjson // 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 // Large chunks of the code are from my Intel 8080 emulator
typedef struct buffer { typedef struct buffer {
@ -35,11 +38,10 @@ buffer* read_file(char** argv) {
cJSON* read_cjson() { cJSON* read_cjson() {
FILE *fs = fopen("Opcodes.json", "rb"); FILE *fs = fopen("Opcodes.json", "rb");
if (fs = NULL) { if (fs == NULL) {
printf("ERROR: Unable to open the Opcodes.json file.\n"); printf("ERROR: Unable to open the Opcodes.json file.\n");
return NULL; return NULL;
} }
// read the file contents into a string // read the file contents into a string
fseek(fs, 0L, SEEK_END); fseek(fs, 0L, SEEK_END);
int fsize = ftell(fs); int fsize = ftell(fs);
@ -62,11 +64,14 @@ cJSON* read_cjson() {
cJSON_Delete(json); cJSON_Delete(json);
return NULL; return NULL;
} }
free(b->buffer); // almost forgot that dataleak
free(b); free(b);
return json; return json;
} }
void print_buffer(buffer* b) { void print_buffer(buffer* b) {
// just a check
// equivalent to $ xxd snake.gb
for (int i = 0; i < 40; i++) { for (int i = 0; i < 40; i++) {
printf("%02x \n", b->buffer[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); cJSON *command = cJSON_GetObjectItem(unprefixed, code);
if (command == NULL) { 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; return 0;
} }
cJSON *mnemonic_item = cJSON_GetObjectItem(command, "mnemonic"); 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)) { if (bytes_item != NULL && cJSON_IsNumber(bytes_item)) {
*bytes = bytes_item->valueint; opbytes = bytes_item->valueint;
} }
// Here ends the cJSON part // Here ends the cJSON part
printf("%d\n", opbytes); printf("Number of Opbytes: %d ", opbytes);
printf("%s\n", mnemonic); printf("Mnemnoic: %s", mnemonic);
if (opbytes > 5 || opbytes < 1) { if (opbytes > 5 || opbytes < 1) {
opbytes = 1; opbytes = 1;
@ -123,7 +128,7 @@ int main (int argc, char** argv) {
return 1; return 1;
} }
print_buffer(buff); print_buffer(buff);
JSON *json = read_cjson(); cJSON *json = read_cjson();
if (json == NULL) { if (json == NULL) {
printf("main terminated\n"); printf("main terminated\n");