summaryrefslogtreecommitdiffstats
path: root/decode.cpp
blob: a5613ac0259fdf8ac08713236ca1e06783396bb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>


typedef struct field field;
struct field {
    const char *byte_name; 
    const char *word_name; 
   unsigned short bits[3];
};


const int FIELDS_COUNT = 8;
field fields[FIELDS_COUNT] {
    {"ax", "al", {0, 0, 0}},
    {"si", "cl", {0, 0, 1}},
    {"dx", "dl", {0, 1, 0}},
    {"bx", "bl", {0, 1, 1}},
    {"sp", "ah", {1, 0, 0}},
    {"bp", "ch", {1, 0, 1}},
    {"si", "dh", {1, 1, 0}},
    {"di", "bh", {1, 1, 1}},
};

const char *get_field(unsigned short bits[3], unsigned short w) {
    for(int index = 0; index < FIELDS_COUNT; ++index) {
	if(!w) { if(memcmp(bits, fields[index].bits, 3) == 0) { return fields[index].byte_name;} }
	else   { if(memcmp(bits, fields[index].bits, 3) == 0) { return fields[index].word_name;} }
    }
}

typedef struct inst inst;
struct inst {
    const char *instruction;
    unsigned short bits[8];
};

const int INST_COUNT = 3;
const inst instructions[INST_COUNT] {
    {"mov", {1, 0, 0, 0, 1, 0}},
    {"add", {}},
    {"sub", {}},
};

int main(int argc, char **argv) {

    if(argc <= 1) { return 1;}

    struct stat sb;
    if (lstat(argv[1], &sb) == -1) {
	perror("lstat");
	exit(EXIT_FAILURE);
    }
    size_t size = sb.st_size;

    char *buffer = (char *)malloc(size);

    int fd = open(argv[1], O_RDONLY);
    size_t read_count =  read(fd,  buffer, size);
    if(read_count != size) {
	printf("i think this is an error");
	return 1;
    }


    for(int index = 0; index != size; ++index) {

	char *line = (char *)malloc(8);
	unsigned char byte = (unsigned char)buffer[index];

	for(int pos = 7; pos >= 0; --pos) {
	    line[pos] = (byte >> pos) & 1 ? '1' : '0';
	}

	// check for mov instruction
	{
	    bool result = true;
	    for(int regular = 0, reverse = 5; regular < 5, reverse >= 0; ++regular, --reverse) {
		if(line[reverse] - '0' != instructions[0].bits[regular]) { result = false; } 
	    }
	    if(result) { printf("mov\n");}
	}

	// check for second 8 bits 
	{
	    bool result = true;
	    int field_index = 0;
	    for(int field_idx = 0; field_idx < FIELDS_COUNT; ++field_idx) {

		unsigned short d = 0;
		unsigned short w = 0;

		for(int regular = 0, reverse = 5; regular < 5, reverse >= 0; ++regular, --reverse) {
		    if(line[reverse] - '0' != fields[field_idx].bits[regular]) {
			result = false;
		    } 
		}
		if(result) { printf(" %s,", get_field(fields[field_idx], 2)); }
	    }
	}

	free(line);
    }

    free(buffer);
    close(fd);

    return 0;
}