Fixed some disassembler issues. At least some instuctions are being disassembled correctly.

This commit is contained in:
2022-08-26 21:43:03 -05:00
parent 0785f21805
commit 8e9d1aefe8
3 changed files with 35 additions and 28 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
;.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, 45 ; //
cmp r8, 45 ;1000 1111 cmp r8, 41 ;1000 1111
;copy r1, 45 ;//B0 = 10110000 ;copy r1, 45 ;//B0 = 10110000
;copy 2, 45 ;copy 2, 45
+29 -22
View File
@@ -1,4 +1,5 @@
#include "../includes/disass.h" #include "../includes/disass.h"
#include <stdlib.h>
const unsigned char* Image; const unsigned char* Image;
const unsigned char COPYMASKREG = 0x20; const unsigned char COPYMASKREG = 0x20;
@@ -11,43 +12,44 @@ const unsigned char REGMASK = 0x00;//0x18; //0001 1000
const unsigned char CONSTMASK = 0x08; //0000 1000 const unsigned char CONSTMASK = 0x08; //0000 1000
const unsigned char ADDRESSMASK = 0x10; //0001 0000 const unsigned char ADDRESSMASK = 0x10; //0001 0000
int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]); int IsRegisterPattern(unsigned char pattern, char* lexeme);
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 = 0;
unsigned char lexeme[3] = { 0 };
unsigned char lexem2[8] = { 0 }; char* parameter1 = calloc(32, sizeof(char));
char* parameter2 = calloc(32, sizeof(char));
unsigned char instruction = image[position]; unsigned char instruction = image[position];
while(instruction != 0) { while(instruction != 0) {
//1110 0000 // printf("%d\n", position);
// printf("%#04X\n", instruction);
unsigned char masked = instruction & 0xE0; unsigned char masked = instruction & 0xE0;
if (masked) { if (masked) {
IsRegisterPattern(instruction & 0x07, lexeme); IsRegisterPattern(instruction & 0x07, parameter1);
unsigned char parameter = instruction & 0x18; unsigned char parameter = instruction & 0x18;
if (!parameter) { if (!parameter) {
position++; IsRegisterPattern(Image[position], parameter2);
IsRegisterPattern(Image[position], lexem2);
} else if (parameter == CONSTMASK) { } else if (parameter == CONSTMASK) {
snprintf(parameter2, sizeof(char) * 31, "%d", Image[position + 1] + Image[position + 2]);
lexem2[0] = 4 >> Image[position] | 0x30; position += 3;
lexem2[1] = Image[position] | 0x30;
position++;
} }
else if (parameter == ADDRESSMASK) { else if (parameter == ADDRESSMASK) {
//snprintf(lexem2, sizeof lexem2, "%#04X", Image[position]); snprintf(parameter2, sizeof(char) * 31, "[%#04X]", Image[position + 1] + Image[position + 2]);
lexem2[0] = 'X'; position += 3;
} }
//If the top bits are set //If the top bits are set
if (masked & COPYMASKREG) { if ((masked & COPYMASKREG) == COPYMASKREG) {
printf("copy %s, %d\n", lexeme, Image[position]); printf("copy %s, %s\n", parameter1, parameter2);
} }
else if (masked & COPYMASKADD) { else if ((masked & COPYMASKADD) == COPYMASKADD) {
printf("CMP1\n"); printf("CMP1\n");
} }
else if (masked & ADDMASK) { else if (masked & ADDMASK) {
@@ -57,7 +59,7 @@ void Disassemble(unsigned char image[HIGHMEMORY]) {
printf("CMP3\n"); printf("CMP3\n");
} }
else if (masked & CMPMASK) { else if (masked & CMPMASK) {
printf("CMP\n"); printf("CMP %s, %s\n", parameter1, parameter2);
} }
else { else {
fprintf(stderr, "[Error] Unknown opcode %#02X\n", masked); fprintf(stderr, "[Error] Unknown opcode %#02X\n", masked);
@@ -65,21 +67,26 @@ void Disassemble(unsigned char image[HIGHMEMORY]) {
} }
} }
instruction = Image[position++]; instruction = Image[position];
memset(lexem2, '\0', sizeof lexem2); memset(parameter1, '\0', sizeof(char) * 32);
memset(lexeme, '\0', sizeof lexeme); memset(parameter2, '\0', sizeof(char) * 32);
} }
} }
int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]) { int IsRegisterPattern(unsigned char pattern, char* lexeme) {
if (pattern > 8) return 0; if (pattern > 8) {
lexeme[0] = '\0';
return 0;
}
//The registers' bit patterns are zero indexed, so 'r1' is '000' //The registers' bit patterns are zero indexed, so 'r1' is '000'
//but 'r8' is '111' and so forth. //but 'r8' is '111' and so forth.
pattern++; pattern++;
lexeme[0] = 'r'; lexeme[0] = 'r';
lexeme[1] = pattern | 0x30; lexeme[1] = pattern | 0x30;
lexeme[2] = '\0';
return 1; return 1;
} }
/* /*
+5 -5
View File
@@ -68,11 +68,11 @@ unsigned char* ParseTokens(List* tokens) {
if (SymbolsTable->size > 0) PrintSymbols(); if (SymbolsTable->size > 0) PrintSymbols();
DestroyList(SymbolsTable); DestroyList(SymbolsTable);
// 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 ", Memory[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 Memory;