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
+5 -3
View File
@@ -1,7 +1,7 @@
;.org 0x100 ;.org 0x100
;.db msg "Hello, world!", 0 .db msg "Hello, world!", 0
;.db more_stuff "AA", 0 ;.db more_stuff "AA", 0
copy r7, 45 ; // copy r7, msg ; //
cmp r8, 45 ;1000 1111 cmp r8, 45 ;1000 1111
add r4, 30 add r4, 30
sub r1, 69 sub r1, 69
@@ -17,4 +17,6 @@ jz 45
;mov r1, 5 ; move the immediate value 5 into r1 ;mov r1, 5 ; move the immediate value 5 into r1
;add r1,5 ; add 5 into r1 ;add r1,5 ; add 5 into r1
;int 21 ; maybe that will call some string drawing BIOS-like routine ;int 21 ; maybe that will call some string drawing BIOS-like routine
;ret ;ret
; 4 byte header
; | Address of First Opcode (2 bytes) | End of Binary (2 bytes) |
+6 -3
View File
@@ -17,14 +17,17 @@ void GetParameter(unsigned char instruction, char* text);
void Disassemble(unsigned char image[HIGHMEMORY]) { void Disassemble(unsigned char image[HIGHMEMORY]) {
Image = image; 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* parameter1 = calloc(32, sizeof(char));
char* parameter2 = calloc(32, sizeof(char)); char* parameter2 = calloc(32, sizeof(char));
unsigned char instruction = image[position]; unsigned char instruction = image[position];
while(instruction != 0) { while(end >= position) {//(instruction != 0) {
// printf("%d\n", position); // printf("%d\n", position);
// printf("%#04X\n", instruction); // printf("%#04X\n", instruction);
@@ -119,7 +122,7 @@ int IsRegisterPattern(unsigned char pattern, char* lexeme) {
return 1; return 1;
} }
/* /*
//REG XXX0 0XXX 1010 1111 //REG XXX0 0XXX
//Constant XXX0 1XXX //Constant XXX0 1XXX
//Address XXX1 0XXX //Address XXX1 0XXX
NOP - 0000 0000 NOP NOP - 0000 0000 NOP
+54 -14
View File
@@ -18,6 +18,8 @@ struct intr {
Symbol* CreateSymbol(Token* token, int address); Symbol* CreateSymbol(Token* token, int address);
const List* TokensList; const List* TokensList;
int CurrentToken = 0; 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 AddSymbol(Token*, int);
void PrintSymbols(void); void PrintSymbols(void);
@@ -30,13 +32,18 @@ int Expect(int, ...);
int ExpectTokenClass(int, ...); int ExpectTokenClass(int, ...);
const Symbol* GetSymbol(char*); const Symbol* GetSymbol(char*);
List* SymbolsTable; 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 int ProgramCounter = 0;
unsigned char Heap[HIGHMEMORY];
unsigned char Memory[HIGHMEMORY]; unsigned char Memory[HIGHMEMORY];
unsigned char* ParseTokens(List* tokens) { unsigned char* ParseTokens(List* tokens) {
if (!tokens) return NULL; if (!tokens) return NULL;
memset(Memory, 0, sizeof(Memory)); memset(Memory, 0, sizeof(Memory));
memset(Heap, 0, sizeof(Heap));
TokensList = tokens; TokensList = tokens;
@@ -68,14 +75,23 @@ unsigned char* ParseTokens(List* tokens) {
if (SymbolsTable->size > 0) PrintSymbols(); if (SymbolsTable->size > 0) PrintSymbols();
DestroyList(SymbolsTable); 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++) { for(int i = 0; i < 33; i++) {
if (i != 0 && i % 3 == 0) printf("\n"); if (i != 0 && i % 3 == 0) printf("\n");
printf("%02X ", Memory[i] & 0xFF); printf("%02X ", Heap[i] & 0xFF);
} }
printf("\n"); printf("\n");
// printf("Program Counter: %d\n", ProgramCounter); printf("Program Counter: %d\n", ProgramCounter);
//printf("Heap Top: %#06X\n", HeapTop); printf("Heap Top: %#06X\n", HeapTop);
return Memory;
return Heap;
} }
void HandleAssemblerDirective(void) { void HandleAssemblerDirective(void) {
@@ -86,21 +102,25 @@ void HandleAssemblerDirective(void) {
Token* identifier = PeekToken(); Token* identifier = PeekToken();
if (!Expect(1, IDENTIFIER)) { 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); exit(1);
} }
AddSymbol(identifier, ProgramCounter); AddSymbol(identifier, ProgramCounter);
switch(PeekToken()->type) { identifier = PeekToken();
case STRING:
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, PeekToken()->lexeme, ProgramCounter);
for(int i = 0; i < strlen(PeekToken()->lexeme); i++) { switch(identifier->type) {
Memory[ProgramCounter + i] = PeekToken()->lexeme[i]; 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(); AdvanceParser();
@@ -113,8 +133,10 @@ void HandleAssemblerDirective(void) {
} }
else { else {
//Add the NULL byte. //Add the NULL byte.
Memory[ProgramCounter] = '\0'; //Memory[ProgramCounter] = '\0';
ProgramCounter++; Heap[HeapTop] = '\0';
//ProgramCounter++;
HeapTop++;
return; return;
} }
} }
@@ -153,7 +175,7 @@ void HandleRegisterBasedOpcode(unsigned char instruction, unsigned char mask) {
token = PeekToken(); token = PeekToken();
if (token->token_class == IDENTIFIER || token->token_class == LABEL) { if (token->type == IDENTIFIER || token->type == LABEL) {
const Symbol* symbol = GetSymbol(token->lexeme); const Symbol* symbol = GetSymbol(token->lexeme);
if (!symbol) { if (!symbol) {
@@ -363,4 +385,22 @@ int ParserAtEnd(void) {
Token* PeekToken(void) { Token* PeekToken(void) {
return TokensList->content[CurrentToken]; 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;
} }