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.

This commit is contained in:
2023-07-06 19:39:03 -05:00
parent 3031fa8b9b
commit 9c8fe17c8f
6 changed files with 37 additions and 31 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
CC = gcc CC = gcc
CFLAGS=-g -Wall -DDEBUG -Wpedantic CFLAGS=-g -Wall -DDEBUG -Wpedantic -Wextra
SRCDIR=src SRCDIR=src
OBJDIR=obj OBJDIR=obj
SRCS=$(wildcard $(SRCDIR)/*.c) SRCS=$(wildcard $(SRCDIR)/*.c)
@@ -35,7 +35,7 @@ clean:
rm -rf $(BINDIR)/* $(OBJDIR)/* rm -rf $(BINDIR)/* $(OBJDIR)/*
test: test:
$(BIN) misc/test.asm $(BIN) misc/another_test.asm
disass: disass:
objdump -S --disassemble $(OBJDIR)/$(FILE).o > $(OBJDIR)/$(FILE).s objdump -S --disassemble $(OBJDIR)/$(FILE).o > $(OBJDIR)/$(FILE).s
+2
View File
@@ -8,6 +8,7 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "opcodes.h" #include "opcodes.h"
#include "token.h"
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128 #define SYMBOLSTABLE_DEFAULT_CAPACITY 128
@@ -16,6 +17,7 @@ typedef struct {
int Address; int Address;
int Resolved; int Resolved;
int Length; int Length;
Token* References;
} Symbol; } Symbol;
typedef struct { typedef struct {
+2
View File
@@ -23,6 +23,8 @@ draw_loop:
_end: _end:
jmp _end jmp _end
.db NewMsg "My message", 0
; Returns the length of a NULL terminated string ; Returns the length of a NULL terminated string
; Arguments: R1 - Pointer to the string ; Arguments: R1 - Pointer to the string
; Returns: R2 - Contains the length of the string ; Returns: R2 - Contains the length of the string
+1 -1
View File
@@ -38,7 +38,7 @@ int main(int argc, char* args[]) {
assemble(); assemble();
printf("\nBytes:\n"); 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"); if (i != 0 && i % 8 == 0) printf("\n");
printf("%02X ", mem[i] & 0xFF); printf("%02X ", mem[i] & 0xFF);
} }
+9 -22
View File
@@ -1,4 +1,5 @@
#include "../includes/opcodes.h" #include "../includes/opcodes.h"
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -67,7 +68,7 @@ void GetRegisterText(Registers reg, char buffer[3]) {
int IsOpcode(const char* text, Mnemonic* opcode) { int IsOpcode(const char* text, Mnemonic* opcode) {
if (!text) return 0; 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 (strcmp(instructions[i].Name, text) == 0) {
if (opcode) *opcode = instructions[i].Mnemonic; if (opcode) *opcode = instructions[i].Mnemonic;
return 1; return 1;
@@ -86,25 +87,11 @@ int IsRegister(const char* text, Registers* reg) {
if (length != 2) return 0; if (length != 2) return 0;
if (text[0] != 'r') return 0; if (text[0] != 'r') return 0;
switch(text[1]) { if (!isdigit(text[1])) return 0;
case '1':
r--; r = text[1] - 0x31;
case '2':
r--; if (reg) *reg = r;
case '3':
r--; return 1;
case '4':
r--;
case '5':
r--;
case '6':
r--;
case '7':
r--;
case '8':
if (reg) *reg = r;
return 1;
default:
return 0;
}
} }
+21 -6
View File
@@ -13,7 +13,7 @@ int CurrentToken = 0;
void PrintSymbols(void); void PrintSymbols(void);
void RemoveCurrentToken(void); void RemoveCurrentToken(void);
void HandleAssemblerDirective(void); void HandleAssemblerDirective(void);
void HandleOpcode(void); Token* ExpectMnemonic(void);
void AdvanceParser(void); void AdvanceParser(void);
void IgnoreParserLine(void); void IgnoreParserLine(void);
Token* PeekToken(void); Token* PeekToken(void);
@@ -46,7 +46,7 @@ SymbolTable* ParseTokens(List* tokens) {
HandleAssemblerDirective(); HandleAssemblerDirective();
break; break;
case MnemonicClass: case MnemonicClass:
HandleOpcode(); ExpectMnemonic();
break; break;
case LabelClass: case LabelClass:
{ {
@@ -58,7 +58,7 @@ SymbolTable* ParseTokens(List* tokens) {
exit(1); exit(1);
} }
if (!found) AddSymbolToTable(t->Lemexe, PC, 1, SymbolsTable); if (!found) symbol = AddSymbolToTable(t->Lemexe, PC, 1, SymbolsTable);
else else
{ {
symbol->Address = PC; symbol->Address = PC;
@@ -68,6 +68,8 @@ SymbolTable* ParseTokens(List* tokens) {
RemoveCurrentToken(); //label RemoveCurrentToken(); //label
ExpectLineEndOrFileEnd(RemoveExpected); ExpectLineEndOrFileEnd(RemoveExpected);
symbol->References = ExpectMnemonic();
} }
break; break;
default: default:
@@ -119,9 +121,15 @@ void HandleAssemblerDirective() {
} }
} }
void HandleOpcode(void) { Token* ExpectMnemonic(void) {
Token* opcode = PeekToken(); Token* opcode = PeekToken();
if (opcode->Class != MnemonicClass) {
fprintf(stderr, "Syntax error on line %d: expected mnemonic.\n", opcode->LineNumber);
exit(1);
}
AdvanceParser(); AdvanceParser();
switch(opcode->Value.Mnemonic) { switch(opcode->Value.Mnemonic) {
@@ -372,6 +380,8 @@ void HandleOpcode(void) {
} }
ExpectLineEndOrFileEnd(ForwardParser); ExpectLineEndOrFileEnd(ForwardParser);
return opcode;
} }
void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options) { void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options) {
@@ -479,13 +489,18 @@ void ExpectLineEndOrFileEnd(ExpectOptions options) {
} }
void PrintSymbols(void) { void PrintSymbols(void) {
//char mn[12]; char mn[12];
printf("-----SYMBOLS-----\n"); printf("-----SYMBOLS-----\n");
for(int i = 0; i < SymbolsTable->Size; i++) { for(int i = 0; i < SymbolsTable->Size; i++) {
Symbol* symbol = SymbolsTable->Symbols[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("\n");
} }
printf("-----SYMBOLS-----\n"); printf("-----SYMBOLS-----\n");