From 9c8fe17c8fae77026759459337b650e0ca327a7e Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 6 Jul 2023 19:39:03 -0500 Subject: [PATCH] Minor code cleanup and added an extra flag to the compiler. If a symbol is a label it will now hold a reference to the opcode and by extension the offset into the file it points to. --- Makefile | 4 ++-- includes/symbols_table.h | 2 ++ misc/another_test.asm | 2 ++ src/main.c | 2 +- src/opcodes.c | 31 +++++++++---------------------- src/parser.c | 27 +++++++++++++++++++++------ 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 57d55ea..7dd9394 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS=-g -Wall -DDEBUG -Wpedantic +CFLAGS=-g -Wall -DDEBUG -Wpedantic -Wextra SRCDIR=src OBJDIR=obj SRCS=$(wildcard $(SRCDIR)/*.c) @@ -35,7 +35,7 @@ clean: rm -rf $(BINDIR)/* $(OBJDIR)/* test: - $(BIN) misc/test.asm + $(BIN) misc/another_test.asm disass: objdump -S --disassemble $(OBJDIR)/$(FILE).o > $(OBJDIR)/$(FILE).s \ No newline at end of file diff --git a/includes/symbols_table.h b/includes/symbols_table.h index 4e74cc7..f9f4fba 100644 --- a/includes/symbols_table.h +++ b/includes/symbols_table.h @@ -8,6 +8,7 @@ #include #include #include "opcodes.h" +#include "token.h" #define SYMBOLSTABLE_DEFAULT_CAPACITY 128 @@ -16,6 +17,7 @@ typedef struct { int Address; int Resolved; int Length; + Token* References; } Symbol; typedef struct { diff --git a/misc/another_test.asm b/misc/another_test.asm index fae43e5..a8c3843 100644 --- a/misc/another_test.asm +++ b/misc/another_test.asm @@ -23,6 +23,8 @@ draw_loop: _end: jmp _end +.db NewMsg "My message", 0 + ; Returns the length of a NULL terminated string ; Arguments: R1 - Pointer to the string ; Returns: R2 - Contains the length of the string diff --git a/src/main.c b/src/main.c index 4e2d284..16567d5 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,7 @@ int main(int argc, char* args[]) { assemble(); printf("\nBytes:\n"); - for(int i = 0; i < sizeof(mem); i++) { + for(unsigned long i = 0; i < sizeof(mem); i++) { if (i != 0 && i % 8 == 0) printf("\n"); printf("%02X ", mem[i] & 0xFF); } diff --git a/src/opcodes.c b/src/opcodes.c index 70ea440..5124792 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -1,4 +1,5 @@ #include "../includes/opcodes.h" +#include #include #include @@ -67,7 +68,7 @@ void GetRegisterText(Registers reg, char buffer[3]) { int IsOpcode(const char* text, Mnemonic* opcode) { if (!text) return 0; - for(int i = 0; i < sizeof(instructions) / sizeof(struct _instruction); i++) { + for(unsigned long i = 0; i < sizeof(instructions) / sizeof(struct _instruction); i++) { if (strcmp(instructions[i].Name, text) == 0) { if (opcode) *opcode = instructions[i].Mnemonic; return 1; @@ -86,25 +87,11 @@ int IsRegister(const char* text, Registers* reg) { if (length != 2) return 0; if (text[0] != 'r') return 0; - switch(text[1]) { - case '1': - r--; - case '2': - r--; - case '3': - r--; - case '4': - r--; - case '5': - r--; - case '6': - r--; - case '7': - r--; - case '8': - if (reg) *reg = r; - return 1; - default: - return 0; - } + if (!isdigit(text[1])) return 0; + + r = text[1] - 0x31; + + if (reg) *reg = r; + + return 1; } \ No newline at end of file diff --git a/src/parser.c b/src/parser.c index cb090ff..7ec47b8 100644 --- a/src/parser.c +++ b/src/parser.c @@ -13,7 +13,7 @@ int CurrentToken = 0; void PrintSymbols(void); void RemoveCurrentToken(void); void HandleAssemblerDirective(void); -void HandleOpcode(void); +Token* ExpectMnemonic(void); void AdvanceParser(void); void IgnoreParserLine(void); Token* PeekToken(void); @@ -46,7 +46,7 @@ SymbolTable* ParseTokens(List* tokens) { HandleAssemblerDirective(); break; case MnemonicClass: - HandleOpcode(); + ExpectMnemonic(); break; case LabelClass: { @@ -58,7 +58,7 @@ SymbolTable* ParseTokens(List* tokens) { exit(1); } - if (!found) AddSymbolToTable(t->Lemexe, PC, 1, SymbolsTable); + if (!found) symbol = AddSymbolToTable(t->Lemexe, PC, 1, SymbolsTable); else { symbol->Address = PC; @@ -68,6 +68,8 @@ SymbolTable* ParseTokens(List* tokens) { RemoveCurrentToken(); //label ExpectLineEndOrFileEnd(RemoveExpected); + + symbol->References = ExpectMnemonic(); } break; default: @@ -119,9 +121,15 @@ void HandleAssemblerDirective() { } } -void HandleOpcode(void) { +Token* ExpectMnemonic(void) { Token* opcode = PeekToken(); + if (opcode->Class != MnemonicClass) { + fprintf(stderr, "Syntax error on line %d: expected mnemonic.\n", opcode->LineNumber); + + exit(1); + } + AdvanceParser(); switch(opcode->Value.Mnemonic) { @@ -372,6 +380,8 @@ void HandleOpcode(void) { } ExpectLineEndOrFileEnd(ForwardParser); + + return opcode; } void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options) { @@ -479,13 +489,18 @@ void ExpectLineEndOrFileEnd(ExpectOptions options) { } void PrintSymbols(void) { - //char mn[12]; + char mn[12]; printf("-----SYMBOLS-----\n"); for(int i = 0; i < SymbolsTable->Size; i++) { Symbol* symbol = SymbolsTable->Symbols[i]; - printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length); + if (!symbol->References) + printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length); + else { + GetMnemonicText(symbol->References->Value.Mnemonic, mn); + printf("[%s] %s [%d] [Width: %d] -> [%s] Line %d", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length, mn, symbol->References->LineNumber); + } printf("\n"); } printf("-----SYMBOLS-----\n");