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 video_start 0xF37F
|
||||||
.db msg "Hello, world!", 0
|
.db msg "Hello, world!", 0
|
||||||
.db NOTERM "No terminating byte here"
|
.db NOTERM "No terminating byte here"
|
||||||
.db null_byte 0
|
.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
|
|
||||||
|
|||||||
+46
-50
@@ -1,11 +1,13 @@
|
|||||||
#include "../includes/parser.h"
|
#include "../includes/parser.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
const List* TokensList;
|
List* TokensList;
|
||||||
int CurrentToken = 0;
|
int CurrentToken = 0;
|
||||||
|
|
||||||
void PrintSymbols(void);
|
void PrintSymbols(void);
|
||||||
Instruction* HandleOperation(void);
|
Instruction* HandleOperation(void);
|
||||||
|
void RemoveCurrentToken(void);
|
||||||
void HandleAssemblerDirective(void);
|
void HandleAssemblerDirective(void);
|
||||||
void HandleAssemblerDirective(void);
|
void HandleAssemblerDirective(void);
|
||||||
void AdvanceParser(void);
|
void AdvanceParser(void);
|
||||||
@@ -32,9 +34,8 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
|
|
||||||
switch(t->Class) {
|
switch(t->Class) {
|
||||||
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
||||||
//HandleAssemblerDirective();
|
HandleAssemblerDirective();
|
||||||
RemoveItem(CurrentToken, tokens);
|
RemoveCurrentToken(); // clear new line
|
||||||
if (CurrentToken > 0) CurrentToken--;
|
|
||||||
break;
|
break;
|
||||||
case MnemonicClass:
|
case MnemonicClass:
|
||||||
//HandleOperation();
|
//HandleOperation();
|
||||||
@@ -61,7 +62,7 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvanceParser();
|
RemoveCurrentToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintSymbols();
|
PrintSymbols();
|
||||||
@@ -70,58 +71,47 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HandleAssemblerDirective() {
|
void HandleAssemblerDirective() {
|
||||||
Token* token = PeekToken();
|
Token* directive = PeekToken();
|
||||||
char* symbolname;
|
RemoveCurrentToken();
|
||||||
|
|
||||||
//ignore sanity checks
|
switch(directive->Value.Directive){
|
||||||
AdvanceParser();
|
case DB:
|
||||||
|
{
|
||||||
|
Token* identifier = PeekToken();
|
||||||
|
Symbol* symbol;
|
||||||
|
|
||||||
token = PeekToken();
|
int found = TryGetSymbol(identifier->Lemexe, MachineState.SymbolsTable, &symbol);
|
||||||
|
|
||||||
if (token->Class != IdentifierClass) {
|
if (found && symbol->Resolved) {
|
||||||
fprintf(stderr, "[Error] Line %d: expected variable name.", token->LineNumber);
|
fprintf(stderr, "Redefinition of %s on line %d.\n", identifier->Lemexe, identifier->LineNumber);
|
||||||
IgnoreParserLine();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
symbolname = token->Lemexe;
|
|
||||||
|
|
||||||
AdvanceParser();
|
|
||||||
|
|
||||||
token = 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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
if (found) {
|
||||||
|
symbol->Address = HeapSize;
|
||||||
|
symbol->Resolved = 1;
|
||||||
|
|
||||||
if (token->Value.Number > 255) {
|
HeapSize += symbol->Length;
|
||||||
fprintf(stderr, "[Error] Line %d: The terminating byte must be smaller than 256, got %d.\n", token->LineNumber, token->Value.Number);
|
}
|
||||||
|
else {
|
||||||
|
AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||||
|
|
||||||
|
RemoveCurrentToken();
|
||||||
|
|
||||||
|
identifier = PeekToken();
|
||||||
|
|
||||||
|
if (identifier->Class == CharacterClass) {
|
||||||
|
HeapSize += strlen(identifier->Lemexe);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
HeapSize += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Synatx error on line %d.\n", directive->LineNumber);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//s->Value.String = CreateSymbolString(string, 1);
|
|
||||||
//s->Value.String->TerminatingByte = token->Value.Number;
|
|
||||||
}
|
|
||||||
//else s->Value.String = CreateSymbolString(string, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
IgnoreParserLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -132,7 +122,7 @@ void PrintSymbols(void) {
|
|||||||
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
||||||
Symbol* symbol = MachineState.SymbolsTable->Symbols[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) {
|
// switch(symbol->Type) {
|
||||||
// case InstructionPointerSymbol:
|
// case InstructionPointerSymbol:
|
||||||
@@ -186,3 +176,9 @@ void IgnoreParserLine(void) {
|
|||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RemoveCurrentToken(void) {
|
||||||
|
RemoveItem(CurrentToken, TokensList);
|
||||||
|
|
||||||
|
if (CurrentToken > 0) CurrentToken--;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user