From 8e9d1aefe8d9abd697d200c7f2900966f39edb00 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Fri, 26 Aug 2022 21:43:03 -0500 Subject: [PATCH] Fixed some disassembler issues. At least some instuctions are being disassembled correctly. --- misc/test.asm | 2 +- src/disass.c | 51 +++++++++++++++++++++++++++++---------------------- src/parser.c | 10 +++++----- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/misc/test.asm b/misc/test.asm index d870a1a..75a82d2 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -2,7 +2,7 @@ ;.db msg "Hello, world!", 0 ;.db more_stuff "AA", 0 copy r7, 45 ; // -cmp r8, 45 ;1000 1111 +cmp r8, 41 ;1000 1111 ;copy r1, 45 ;//B0 = 10110000 ;copy 2, 45 diff --git a/src/disass.c b/src/disass.c index 506a67f..2fef319 100644 --- a/src/disass.c +++ b/src/disass.c @@ -1,4 +1,5 @@ #include "../includes/disass.h" +#include const unsigned char* Image; const unsigned char COPYMASKREG = 0x20; @@ -11,43 +12,44 @@ const unsigned char REGMASK = 0x00;//0x18; //0001 1000 const unsigned char CONSTMASK = 0x08; //0000 1000 const unsigned char ADDRESSMASK = 0x10; //0001 0000 -int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]); +int IsRegisterPattern(unsigned char pattern, char* lexeme); +void GetParameter(unsigned char instruction, char* text); void Disassemble(unsigned char image[HIGHMEMORY]) { Image = image; int position = 0; - unsigned char lexeme[3] = { 0 }; - unsigned char lexem2[8] = { 0 }; + + char* parameter1 = calloc(32, sizeof(char)); + char* parameter2 = calloc(32, sizeof(char)); unsigned char instruction = image[position]; while(instruction != 0) { - //1110 0000 + // printf("%d\n", position); + // printf("%#04X\n", instruction); + unsigned char masked = instruction & 0xE0; if (masked) { - IsRegisterPattern(instruction & 0x07, lexeme); + IsRegisterPattern(instruction & 0x07, parameter1); unsigned char parameter = instruction & 0x18; if (!parameter) { - position++; - IsRegisterPattern(Image[position], lexem2); + IsRegisterPattern(Image[position], parameter2); } else if (parameter == CONSTMASK) { - - lexem2[0] = 4 >> Image[position] | 0x30; - lexem2[1] = Image[position] | 0x30; - position++; + snprintf(parameter2, sizeof(char) * 31, "%d", Image[position + 1] + Image[position + 2]); + position += 3; } else if (parameter == ADDRESSMASK) { - //snprintf(lexem2, sizeof lexem2, "%#04X", Image[position]); - lexem2[0] = 'X'; + snprintf(parameter2, sizeof(char) * 31, "[%#04X]", Image[position + 1] + Image[position + 2]); + position += 3; } //If the top bits are set - if (masked & COPYMASKREG) { - printf("copy %s, %d\n", lexeme, Image[position]); + if ((masked & COPYMASKREG) == COPYMASKREG) { + printf("copy %s, %s\n", parameter1, parameter2); } - else if (masked & COPYMASKADD) { + else if ((masked & COPYMASKADD) == COPYMASKADD) { printf("CMP1\n"); } else if (masked & ADDMASK) { @@ -57,7 +59,7 @@ void Disassemble(unsigned char image[HIGHMEMORY]) { printf("CMP3\n"); } else if (masked & CMPMASK) { - printf("CMP\n"); + printf("CMP %s, %s\n", parameter1, parameter2); } else { fprintf(stderr, "[Error] Unknown opcode %#02X\n", masked); @@ -65,21 +67,26 @@ void Disassemble(unsigned char image[HIGHMEMORY]) { } } - instruction = Image[position++]; + instruction = Image[position]; - memset(lexem2, '\0', sizeof lexem2); - memset(lexeme, '\0', sizeof lexeme); + memset(parameter1, '\0', sizeof(char) * 32); + memset(parameter2, '\0', sizeof(char) * 32); } } -int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]) { - if (pattern > 8) return 0; +int IsRegisterPattern(unsigned char pattern, char* lexeme) { + if (pattern > 8) { + lexeme[0] = '\0'; + return 0; + } //The registers' bit patterns are zero indexed, so 'r1' is '000' //but 'r8' is '111' and so forth. pattern++; lexeme[0] = 'r'; lexeme[1] = pattern | 0x30; + lexeme[2] = '\0'; + return 1; } /* diff --git a/src/parser.c b/src/parser.c index f97a4bc..39442b3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -68,11 +68,11 @@ unsigned char* ParseTokens(List* tokens) { if (SymbolsTable->size > 0) PrintSymbols(); DestroyList(SymbolsTable); - // for(int i = 0; i < 33; i++) { - // if (i != 0 && i % 3 == 0) printf("\n"); - // printf("%02X ", Memory[i] & 0xFF); - // } - // printf("\n"); + for(int i = 0; i < 33; i++) { + if (i != 0 && i % 3 == 0) printf("\n"); + printf("%02X ", Memory[i] & 0xFF); + } + printf("\n"); // printf("Program Counter: %d\n", ProgramCounter); //printf("Heap Top: %#06X\n", HeapTop); return Memory;