From 07e1e6b6e272740dd77810e64704119d06500f29 Mon Sep 17 00:00:00 2001 From: Daniil Rose Date: Thu, 30 Dec 2021 22:06:26 -0500 Subject: [PATCH] Add basic hex dump auto disassembler --- main.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index f26b97c..69300e3 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,94 @@ #include -int main() { - printf("Hello, World!\n"); - return 0; +int disassemble_8080_op(unsigned char *codebuffer, int pc); + +int main (int argc, char**argv) { + FILE *f= fopen(argv[1], "rb"); + + if (f==NULL) { + printf("error: Couldn't open %s\n", argv[1]); + exit(1); + } + + fseek(f, 0L, SEEK_END); + int fsize = ftell(f); + fseek(f, 0L, SEEK_SET); + + unsigned char *buffer=malloc(fsize); + + fread(buffer, fsize, 1, f); + fclose(f); + + int pc = 0; + + while (pc < fsize) { + pc += disassemble_8080_op(buffer, pc); + } + + return 0; +} + +/* + * *codebuffer is a valid pointer to 8080 assembly code + * pc is the current offset into the code + * + * returns the number of bytes of the op + */ + +/* + * TODO: Finish writing out 8080 opcodes + */ + +int disassemble_8080_op(unsigned char *codebuffer, int pc) { + unsigned char *code = &codebuffer[pc]; + int opbytes = 1; + + printf ("%04x ", pc); + + switch (*code) { + case 0x00: + printf("NOP"); + break; + case 0x01: + printf("LXI B,#$%02x%02x", code[2], code[1]); + opbytes=3; + break; + case 0x02: + printf("STAX B"); + break; + case 0x03: + printf("INX B"); + break; + case 0x04: + printf("INR B"); + break; + case 0x05: + printf("DCR B"); + break; + case 0x06: + printf("MVI B,#$%02x", code[1]); + opbytes=2; + break; + case 0x07: + printf("RLC"); + break; + case 0x08: + printf("NOP"); + break; + /* ........ */ + case 0x3e: + printf("MVI A,#0x%02x", code[1]); + opbytes = 2; + break; + /* ........ */ + case 0xc3: + printf("JMP $%02x%02x", code[2], code[1]); + opbytes = 3; + break; + /* ........ */ + } + + printf("\n"); + + return opbytes; } -- 2.45.2