Wrote the first batch of directive handling code, so far just handling creating variables.
This commit is contained in:
+1
-28
@@ -1,34 +1,7 @@
|
||||
.include "./another_test.asm"
|
||||
;.include "./another_test.asm"
|
||||
|
||||
.db video_start 0xF37F
|
||||
.db msg "Hello, world!", 0
|
||||
.db NOTERM "No terminating byte here"
|
||||
.db null_byte 0
|
||||
|
||||
load r1, msg ; Because the lod* instructions can't load an address
|
||||
; from anything but a register, this becomes laa r1, [msg]
|
||||
|
||||
;Routine: string_length
|
||||
;In: String address in r1
|
||||
;Out: Length in R2
|
||||
|
||||
string_length:
|
||||
load byte r3, [r1] ; Load the byte from the address in R1, into R3
|
||||
load r2, 0 ; String length
|
||||
cmp r3, null_byte ; Is R3 a null byte?
|
||||
je end
|
||||
inc r2
|
||||
start:
|
||||
inc r1
|
||||
start:
|
||||
inc r1 ; next char
|
||||
loadb r3, [r1] ; Load the next character byte into R3
|
||||
cmp r3, 0 ; Null byte?
|
||||
je end
|
||||
inc r2 ; Nope, increment the length counter
|
||||
jmp start
|
||||
|
||||
end:
|
||||
|
||||
pop r8 ;pop the return address from the stack into register 8
|
||||
jmp [r8] ;jump to that address
|
||||
|
||||
+45
-49
@@ -1,11 +1,13 @@
|
||||
#include "../includes/parser.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const List* TokensList;
|
||||
List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
|
||||
void PrintSymbols(void);
|
||||
Instruction* HandleOperation(void);
|
||||
void RemoveCurrentToken(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
void AdvanceParser(void);
|
||||
@@ -32,9 +34,8 @@ IRState* ParseTokens(List* tokens) {
|
||||
|
||||
switch(t->Class) {
|
||||
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
||||
//HandleAssemblerDirective();
|
||||
RemoveItem(CurrentToken, tokens);
|
||||
if (CurrentToken > 0) CurrentToken--;
|
||||
HandleAssemblerDirective();
|
||||
RemoveCurrentToken(); // clear new line
|
||||
break;
|
||||
case MnemonicClass:
|
||||
//HandleOperation();
|
||||
@@ -61,7 +62,7 @@ IRState* ParseTokens(List* tokens) {
|
||||
break;
|
||||
}
|
||||
|
||||
AdvanceParser();
|
||||
RemoveCurrentToken();
|
||||
}
|
||||
|
||||
PrintSymbols();
|
||||
@@ -70,58 +71,47 @@ IRState* ParseTokens(List* tokens) {
|
||||
}
|
||||
|
||||
void HandleAssemblerDirective() {
|
||||
Token* token = PeekToken();
|
||||
char* symbolname;
|
||||
Token* directive = PeekToken();
|
||||
RemoveCurrentToken();
|
||||
|
||||
//ignore sanity checks
|
||||
AdvanceParser();
|
||||
switch(directive->Value.Directive){
|
||||
case DB:
|
||||
{
|
||||
Token* identifier = PeekToken();
|
||||
Symbol* symbol;
|
||||
|
||||
token = PeekToken();
|
||||
int found = TryGetSymbol(identifier->Lemexe, MachineState.SymbolsTable, &symbol);
|
||||
|
||||
if (token->Class != IdentifierClass) {
|
||||
fprintf(stderr, "[Error] Line %d: expected variable name.", token->LineNumber);
|
||||
IgnoreParserLine();
|
||||
return;
|
||||
}
|
||||
if (found && symbol->Resolved) {
|
||||
fprintf(stderr, "Redefinition of %s on line %d.\n", identifier->Lemexe, identifier->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
if (found) {
|
||||
symbol->Address = HeapSize;
|
||||
symbol->Resolved = 1;
|
||||
|
||||
symbolname = token->Lemexe;
|
||||
HeapSize += symbol->Length;
|
||||
}
|
||||
else {
|
||||
AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||
|
||||
AdvanceParser();
|
||||
RemoveCurrentToken();
|
||||
|
||||
token = PeekToken();
|
||||
identifier = PeekToken();
|
||||
|
||||
if (token->Class == NumberClass) {
|
||||
//Delcaring a numeric value.
|
||||
Symbol* s = AddSymbolToTable(symbolname, HeapSize, 1, MachineState.SymbolsTable);
|
||||
}
|
||||
else if (token->Class == CharacterClass) {
|
||||
Symbol* s = AddSymbolToTable(symbolname, HeapSize, 1, MachineState.SymbolsTable);
|
||||
char* string = token->Lemexe;
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == Comma) {
|
||||
AdvanceParser();
|
||||
|
||||
token = PeekToken();
|
||||
|
||||
if (token->Class != NumberClass) {
|
||||
fprintf(stderr, "[Error] Line %d: Expected terminating byte.\n", token->LineNumber);
|
||||
exit(1);
|
||||
if (identifier->Class == CharacterClass) {
|
||||
HeapSize += strlen(identifier->Lemexe);
|
||||
}
|
||||
else {
|
||||
HeapSize += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (token->Value.Number > 255) {
|
||||
fprintf(stderr, "[Error] Line %d: The terminating byte must be smaller than 256, got %d.\n", token->LineNumber, token->Value.Number);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//s->Value.String = CreateSymbolString(string, 1);
|
||||
//s->Value.String->TerminatingByte = token->Value.Number;
|
||||
}
|
||||
//else s->Value.String = CreateSymbolString(string, 0);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Synatx error on line %d.\n", directive->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
IgnoreParserLine();
|
||||
}
|
||||
|
||||
|
||||
@@ -132,7 +122,7 @@ void PrintSymbols(void) {
|
||||
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
||||
Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
|
||||
|
||||
printf("[%s] %s", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name);
|
||||
printf("[%s] %s [%d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address);
|
||||
|
||||
// switch(symbol->Type) {
|
||||
// case InstructionPointerSymbol:
|
||||
@@ -185,4 +175,10 @@ void IgnoreParserLine(void) {
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveCurrentToken(void) {
|
||||
RemoveItem(CurrentToken, TokensList);
|
||||
|
||||
if (CurrentToken > 0) CurrentToken--;
|
||||
}
|
||||
Reference in New Issue
Block a user