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 more_stuff "AA", 0
copy r7, 45 ; //
cmp r8, 45 ;1000 1111
cmp r8, 41 ;1000 1111
;copy r1, 45 ;//B0 = 10110000
;copy 2, 45
+29 -22
View File
@@ -1,4 +1,5 @@
#include "../includes/disass.h"
#include <stdlib.h>
const unsigned char* Image;
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 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]) {
Image = image;
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];
while(instruction != 0) {
//1110 0000
// printf("%d\n", position);
// printf("%#04X\n", instruction);
unsigned char masked = instruction & 0xE0;
if (masked) {
IsRegisterPattern(instruction & 0x07, lexeme);
IsRegisterPattern(instruction & 0x07, parameter1);
unsigned char parameter = instruction & 0x18;
if (!parameter) {
position++;
IsRegisterPattern(Image[position], lexem2);
IsRegisterPattern(Image[position], parameter2);
} else if (parameter == CONSTMASK) {
lexem2[0] = 4 >> Image[position] | 0x30;
lexem2[1] = Image[position] | 0x30;
position++;
snprintf(parameter2, sizeof(char) * 31, "%d", Image[position + 1] + Image[position + 2]);
position += 3;
}
else if (parameter == ADDRESSMASK) {
//snprintf(lexem2, sizeof lexem2, "%#04X", Image[position]);
lexem2[0] = 'X';
snprintf(parameter2, sizeof(char) * 31, "[%#04X]", Image[position + 1] + Image[position + 2]);
position += 3;
}
//If the top bits are set
if (masked & COPYMASKREG) {
printf("copy %s, %d\n", lexeme, Image[position]);
if ((masked & COPYMASKREG) == COPYMASKREG) {
printf("copy %s, %s\n", parameter1, parameter2);
}
else if (masked & COPYMASKADD) {
else if ((masked & COPYMASKADD) == COPYMASKADD) {
printf("CMP1\n");
}
else if (masked & ADDMASK) {
@@ -57,7 +59,7 @@ void Disassemble(unsigned char image[HIGHMEMORY]) {
printf("CMP3\n");
}
else if (masked & CMPMASK) {
printf("CMP\n");
printf("CMP %s, %s\n", parameter1, parameter2);
}
else {
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(lexeme, '\0', sizeof lexeme);
memset(parameter1, '\0', sizeof(char) * 32);
memset(parameter2, '\0', sizeof(char) * 32);
}
}
int IsRegisterPattern(unsigned char pattern, unsigned char lexeme[2]) {
if (pattern > 8) return 0;
int IsRegisterPattern(unsigned char pattern, char* lexeme) {
if (pattern > 8) {
lexeme[0] = '\0';
return 0;
}
//The registers' bit patterns are zero indexed, so 'r1' is '000'
//but 'r8' is '111' and so forth.
pattern++;
lexeme[0] = 'r';
lexeme[1] = pattern | 0x30;
lexeme[2] = '\0';
return 1;
}
/*
+5 -5
View File
@@ -68,11 +68,11 @@ unsigned char* ParseTokens(List* tokens) {
if (SymbolsTable->size > 0) PrintSymbols();
DestroyList(SymbolsTable);
// for(int i = 0; i < 33; i++) {
// if (i != 0 && i % 3 == 0) printf("\n");
// printf("%02X ", Memory[i] & 0xFF);
// }
// printf("\n");
for(int i = 0; i < 33; i++) {
if (i != 0 && i % 3 == 0) printf("\n");
printf("%02X ", Memory[i] & 0xFF);
}
printf("\n");
// printf("Program Counter: %d\n", ProgramCounter);
//printf("Heap Top: %#06X\n", HeapTop);
return Memory;