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
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
+2
View File
@@ -8,6 +8,7 @@
#include <string.h>
#include <sys/types.h>
#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 {
+2
View File
@@ -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
+1 -1
View File
@@ -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);
}
+9 -22
View File
@@ -1,4 +1,5 @@
#include "../includes/opcodes.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
@@ -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;
}
+21 -6
View File
@@ -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");