Created a basic binary format to help guide the disassembler.

This commit is contained in:
2022-08-29 15:57:14 +00:00
parent 0d3f8a460e
commit 7385a807a5
3 changed files with 65 additions and 20 deletions
+6 -3
View File
@@ -17,14 +17,17 @@ void GetParameter(unsigned char instruction, char* text);
void Disassemble(unsigned char image[HIGHMEMORY]) {
Image = image;
int position = 0;
int position = image[0] + image[1];
int end = image[2] + image[3];
printf("Start: %d and End: %d\n", position, end);
char* parameter1 = calloc(32, sizeof(char));
char* parameter2 = calloc(32, sizeof(char));
unsigned char instruction = image[position];
while(instruction != 0) {
while(end >= position) {//(instruction != 0) {
// printf("%d\n", position);
// printf("%#04X\n", instruction);
@@ -119,7 +122,7 @@ int IsRegisterPattern(unsigned char pattern, char* lexeme) {
return 1;
}
/*
//REG XXX0 0XXX 1010 1111
//REG XXX0 0XXX
//Constant XXX0 1XXX
//Address XXX1 0XXX
NOP - 0000 0000 NOP
+54 -14
View File
@@ -18,6 +18,8 @@ struct intr {
Symbol* CreateSymbol(Token* token, int address);
const List* TokensList;
int CurrentToken = 0;
//int HeapStart = 4; // zero indexed, used for declared variables.
//int BinaryEnd = 4; // Zero indexed (binary starts with a 4 byte header)
void AddSymbol(Token*, int);
void PrintSymbols(void);
@@ -30,13 +32,18 @@ int Expect(int, ...);
int ExpectTokenClass(int, ...);
const Symbol* GetSymbol(char*);
List* SymbolsTable;
void WriteMemory(unsigned char value, int location);
void WriteMemoryORMask(unsigned char mask, int location);
unsigned int HeapTop = 4;
unsigned int ProgramCounter = 0;
unsigned char Heap[HIGHMEMORY];
unsigned char Memory[HIGHMEMORY];
unsigned char* ParseTokens(List* tokens) {
if (!tokens) return NULL;
memset(Memory, 0, sizeof(Memory));
memset(Heap, 0, sizeof(Heap));
TokensList = tokens;
@@ -68,14 +75,23 @@ unsigned char* ParseTokens(List* tokens) {
if (SymbolsTable->size > 0) PrintSymbols();
DestroyList(SymbolsTable);
memcpy(&Heap[HeapTop], Memory, ProgramCounter);
int totalSize = HeapTop + ProgramCounter;
Heap[0] = 2 >> HeapTop & 0xFF;
Heap[1] = HeapTop & 0xFF;
Heap[2] = 2 >> totalSize & 0xFF;
Heap[3] = totalSize & 0xFF;
for(int i = 0; i < 33; i++) {
if (i != 0 && i % 3 == 0) printf("\n");
printf("%02X ", Memory[i] & 0xFF);
printf("%02X ", Heap[i] & 0xFF);
}
printf("\n");
// printf("Program Counter: %d\n", ProgramCounter);
//printf("Heap Top: %#06X\n", HeapTop);
return Memory;
printf("Program Counter: %d\n", ProgramCounter);
printf("Heap Top: %#06X\n", HeapTop);
return Heap;
}
void HandleAssemblerDirective(void) {
@@ -86,21 +102,25 @@ void HandleAssemblerDirective(void) {
Token* identifier = PeekToken();
if (!Expect(1, IDENTIFIER)) {
fprintf(stderr, "Expected identifier on line %d\n", PeekToken()->line);
fprintf(stderr, "Expected identifier on line %d\n", identifier->line);
exit(1);
}
AddSymbol(identifier, ProgramCounter);
switch(PeekToken()->type) {
case STRING:
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, PeekToken()->lexeme, ProgramCounter);
identifier = PeekToken();
for(int i = 0; i < strlen(PeekToken()->lexeme); i++) {
Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
switch(identifier->type) {
case STRING:
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, identifier->lexeme, ProgramCounter);
for(int i = 0; i < strlen(identifier->lexeme); i++) {
//Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
Heap[HeapTop + i] = identifier->lexeme[i];
}
ProgramCounter += strlen(PeekToken()->lexeme);
//ProgramCounter += strlen(PeekToken()->lexeme);
HeapTop += strlen(identifier->lexeme);
AdvanceParser();
@@ -113,8 +133,10 @@ void HandleAssemblerDirective(void) {
}
else {
//Add the NULL byte.
Memory[ProgramCounter] = '\0';
ProgramCounter++;
//Memory[ProgramCounter] = '\0';
Heap[HeapTop] = '\0';
//ProgramCounter++;
HeapTop++;
return;
}
}
@@ -153,7 +175,7 @@ void HandleRegisterBasedOpcode(unsigned char instruction, unsigned char mask) {
token = PeekToken();
if (token->token_class == IDENTIFIER || token->token_class == LABEL) {
if (token->type == IDENTIFIER || token->type == LABEL) {
const Symbol* symbol = GetSymbol(token->lexeme);
if (!symbol) {
@@ -363,4 +385,22 @@ int ParserAtEnd(void) {
Token* PeekToken(void) {
return TokensList->content[CurrentToken];
}
void WriteMemory(unsigned char value, int location) {
if (location > HIGHMEMORY) {
fprintf(stderr, "[Error] Exceeded memmory size\n");
exit(1);
}
Memory[location] = value;
}
void WriteMemoryORMask(unsigned char mask, int location) {
if (location > HIGHMEMORY) {
fprintf(stderr, "[Error] Exceeded memmory size\n");
exit(1);
}
Memory[location] |= mask;
}