diff --git a/misc/test.asm b/misc/test.asm index 71c01f2..4a98417 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -1,7 +1,7 @@ ;.org 0x100 -;.db msg "Hello, world!", 0 +.db msg "Hello, world!", 0 ;.db more_stuff "AA", 0 -copy r7, 45 ; // +copy r7, msg ; // cmp r8, 45 ;1000 1111 add r4, 30 sub r1, 69 @@ -17,4 +17,6 @@ jz 45 ;mov r1, 5 ; move the immediate value 5 into r1 ;add r1,5 ; add 5 into r1 ;int 21 ; maybe that will call some string drawing BIOS-like routine -;ret \ No newline at end of file +;ret +; 4 byte header +; | Address of First Opcode (2 bytes) | End of Binary (2 bytes) | \ No newline at end of file diff --git a/src/disass.c b/src/disass.c index 383758b..ac2ccb8 100644 --- a/src/disass.c +++ b/src/disass.c @@ -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 diff --git a/src/parser.c b/src/parser.c index cd54f1f..cc39ba2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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; } \ No newline at end of file