From 6c7b8d535620cdf97716c065294d3f25aea60881 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 29 Aug 2022 19:29:34 +0000 Subject: [PATCH] Fixed a bug where CMP REG, REG wouldn't get encoded and a disassembler bug related to said CMP bug. --- misc/test.asm | 6 +++--- src/disass.c | 5 +++-- src/main.c | 20 ++++++++++---------- src/parser.c | 11 ++++++++++- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/misc/test.asm b/misc/test.asm index 27a86e8..10712fb 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -2,9 +2,9 @@ .db msg "Hello, world!", 0 .db more_stuff "AA", 0 copy r7, msg ; // -cmp r8, 45 ;1000 1111 -add r4, 30 -sub r1, 69 +cmp r8, r3 ;1000 1111 +add r4, r1 +sub r1, r8 int 20 jz 45 jmp msg diff --git a/src/disass.c b/src/disass.c index b694215..1aae88f 100644 --- a/src/disass.c +++ b/src/disass.c @@ -32,8 +32,9 @@ void Disassemble(unsigned char image[HIGHMEMORY]) { IsRegisterPattern(instruction & 0x07, parameter1); unsigned char parameterType = instruction & 0x18; - if (!parameterType) { - IsRegisterPattern(Image[position], parameter2); + if (parameterType == REGMASK) { + IsRegisterPattern(Image[position + 1], parameter2); + position += 2; } else if (parameterType == CONSTMASK) { snprintf(parameter2, sizeof(char) * 31, "%d", Image[position + 1] + Image[position + 2]); position += 3; diff --git a/src/main.c b/src/main.c index 187ef3c..5e44a43 100644 --- a/src/main.c +++ b/src/main.c @@ -21,19 +21,19 @@ int main(int argc, char* args[]) { List* list = GenerateTokenList(source_code); - // for(int i = 0; i < list->size; i++) { - // Token* t = (Token*) list->content[i]; + for(int i = 0; i < list->size; i++) { + Token* t = (Token*) list->content[i]; - // if (t->type == LineEnd) { - // if (i - 1 >= 0 && ((Token*) list->content[i - 1])->type != LineEnd) - // printf("\n"); + if (t->type == LineEnd) { + if (i - 1 >= 0 && ((Token*) list->content[i - 1])->type != LineEnd) + printf("\n"); - // continue; - // } + continue; + } - // printf("[%i] '%s' [%i] ", t->type, t->lexeme, t->token_class); - // if (t->type == LABEL) printf("* "); - // } + printf("[T: %i][C: %i] '%s' ", t->type, t->token_class, t->lexeme); + if (t->type == LABEL) printf("* "); + } unsigned char* image = ParseTokens(list); Disassemble(image); diff --git a/src/parser.c b/src/parser.c index 3b37d4d..6043dce 100644 --- a/src/parser.c +++ b/src/parser.c @@ -199,7 +199,16 @@ void HandleRegisterBasedOpcode(unsigned char instruction, unsigned char mask) { Memory[ProgramCounter + 1] = *value & 0xFF; AdvanceParser(); - } else { + } + else if (token->token_class == Reg) { + Memory[ProgramCounter] = (PeekToken()->type - R1) & 0x07; + + AdvanceParser(); + + ProgramCounter++; + return; + } + else { fprintf(stderr, "[Error] Expected operand, got %s\n", token->lexeme); exit(1); }